From 18be371f39363998f0b87ad955350e2e2ca85669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Viricel?= Date: Fri, 18 Dec 2020 05:44:23 +0100 Subject: [PATCH] Test mutation + actions --- jest.config.js | 23 +++++++++---------- pages/films/index.spec.js | 47 +++++++++++++++++++-------------------- store/index.js | 8 +++++-- test/actions.spec.js | 35 +++++++++++++++++++++++++++++ test/mutations.spec.js | 9 ++++++++ 5 files changed, 83 insertions(+), 39 deletions(-) create mode 100644 test/actions.spec.js create mode 100644 test/mutations.spec.js diff --git a/jest.config.js b/jest.config.js index e2d6751..be3f8be 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,21 +1,18 @@ module.exports = { moduleNameMapper: { - '^@/(.*)$': '/$1', - '^~/(.*)$': '/$1', - '^vue$': 'vue/dist/vue.common.js' + "^@/(.*)$": "/$1", + "^~/(.*)$": "/$1", + "^vue$": "vue/dist/vue.common.js" }, - moduleFileExtensions: [ - 'js', - 'vue', - 'json' - ], + moduleFileExtensions: ["js", "vue", "json"], transform: { - '^.+\\.js$': 'babel-jest', - '.*\\.(vue)$': 'vue-jest' + "^.+\\.js$": "babel-jest", + ".*\\.(vue)$": "vue-jest" }, + coverageDirectory: "/coverage", collectCoverage: true, collectCoverageFrom: [ - '/components/**/*.vue', - '/pages/**/*.vue' + "/components/**/*.vue", + "/pages/**/*.vue" ] -} +}; diff --git a/pages/films/index.spec.js b/pages/films/index.spec.js index 1fd3f16..6e80121 100644 --- a/pages/films/index.spec.js +++ b/pages/films/index.spec.js @@ -1,37 +1,36 @@ -import { mount, shallowMount } from "@vue/test-utils"; -import axios from "axios"; -import FilmsView from "./"; -import mockFilms from "@/test/fake-films.json"; +import { mount, shallowMount, createLocalVue } from "@vue/test-utils"; +import Vuex from "vuex"; +import Films from "./"; -jest.mock("axios", () => ({ - $get: jest.fn(() => mockFilms) -})); +const localVue = createLocalVue(); +localVue.use(Vuex); describe("Films page", () => { + let state, actions, store; + + beforeEach(() => { + state = { + films: [] + }; + actions = { + getFilms: jest.fn() + }; + store = new Vuex.Store({ state, actions }); + }); + it("should render Films page instance", () => { - const wrapper = mount(FilmsView); + const wrapper = mount(Films, { store, localVue }); expect(wrapper.find("img").exists()).toBe(true); }); it("should get films from Ghibli API", async () => { - let wrapper = shallowMount(FilmsView, { - mocks: { - $axios: axios - } - }); - - expect((await wrapper.vm.$options.asyncData(wrapper.vm)).films).toEqual( - mockFilms - ); - - // Init page with mocked asyncData - wrapper = shallowMount(FilmsView, { - mocks: { - films: (await wrapper.vm.$options.asyncData(wrapper.vm)).films, - $axios: axios - } + let wrapper = shallowMount(Films, { + store, + localVue }); + await wrapper.vm.$options.asyncData({ store }); + expect(actions.getFilms).toHaveBeenCalled(); expect(wrapper.findComponent({ name: "Grid" }).exists()).toBe(true); }); }); diff --git a/store/index.js b/store/index.js index 5d2de3a..985bbdd 100644 --- a/store/index.js +++ b/store/index.js @@ -10,8 +10,12 @@ export const mutations = { export const actions = { async getFilms({ commit }) { - const films = await this.$axios.$get("/api/films"); - commit("setFilms", films); + try { + const films = await this.$axios.$get("/api/films"); + commit("setFilms", films); + } catch (e) { + throw Error("API Error occurred."); + } } }; diff --git a/test/actions.spec.js b/test/actions.spec.js new file mode 100644 index 0000000..427e64c --- /dev/null +++ b/test/actions.spec.js @@ -0,0 +1,35 @@ +import { actions, mutations } from "@/store"; +import axios from "axios"; +import mockFilms from "@/test/fake-films.json"; + +let url = ""; + +jest.mock("axios", () => ({ + $get: _url => { + return new Promise(resolve => { + url = _url; + resolve(mockFilms); + }); + } +})); + +describe("Vuex actions.", () => { + it("tests getFilms action.", async () => { + const commit = jest.fn(); + + actions.$axios = axios; + await actions.getFilms({ commit }); + + expect(url).toBe("/api/films"); + expect(commit).toHaveBeenCalledWith("setFilms", mockFilms); + }); + + it("catches errors.", async () => { + const commit = jest.fn(); + + actions.$axios = null; + await expect(actions.getFilms({ commit })).rejects.toThrow( + "API Error occurred." + ); + }); +}); diff --git a/test/mutations.spec.js b/test/mutations.spec.js new file mode 100644 index 0000000..eaee579 --- /dev/null +++ b/test/mutations.spec.js @@ -0,0 +1,9 @@ +import { state, mutations } from "@/store"; +import mockFilms from "./fake-films.json"; + +describe("Vuex mutations.", () => { + it("tests setFilms mutation.", () => { + mutations.setFilms(state, mockFilms); + expect(state.films[0].id).toBe(mockFilms[0].id); + }); +});