Test mutation + actions
This commit is contained in:
@@ -1,21 +1,18 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
moduleNameMapper: {
|
moduleNameMapper: {
|
||||||
'^@/(.*)$': '<rootDir>/$1',
|
"^@/(.*)$": "<rootDir>/$1",
|
||||||
'^~/(.*)$': '<rootDir>/$1',
|
"^~/(.*)$": "<rootDir>/$1",
|
||||||
'^vue$': 'vue/dist/vue.common.js'
|
"^vue$": "vue/dist/vue.common.js"
|
||||||
},
|
},
|
||||||
moduleFileExtensions: [
|
moduleFileExtensions: ["js", "vue", "json"],
|
||||||
'js',
|
|
||||||
'vue',
|
|
||||||
'json'
|
|
||||||
],
|
|
||||||
transform: {
|
transform: {
|
||||||
'^.+\\.js$': 'babel-jest',
|
"^.+\\.js$": "babel-jest",
|
||||||
'.*\\.(vue)$': 'vue-jest'
|
".*\\.(vue)$": "vue-jest"
|
||||||
},
|
},
|
||||||
|
coverageDirectory: "<rootDir>/coverage",
|
||||||
collectCoverage: true,
|
collectCoverage: true,
|
||||||
collectCoverageFrom: [
|
collectCoverageFrom: [
|
||||||
'<rootDir>/components/**/*.vue',
|
"<rootDir>/components/**/*.vue",
|
||||||
'<rootDir>/pages/**/*.vue'
|
"<rootDir>/pages/**/*.vue"
|
||||||
]
|
]
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -1,37 +1,36 @@
|
|||||||
import { mount, shallowMount } from "@vue/test-utils";
|
import { mount, shallowMount, createLocalVue } from "@vue/test-utils";
|
||||||
import axios from "axios";
|
import Vuex from "vuex";
|
||||||
import FilmsView from "./";
|
import Films from "./";
|
||||||
import mockFilms from "@/test/fake-films.json";
|
|
||||||
|
|
||||||
jest.mock("axios", () => ({
|
const localVue = createLocalVue();
|
||||||
$get: jest.fn(() => mockFilms)
|
localVue.use(Vuex);
|
||||||
}));
|
|
||||||
|
|
||||||
describe("Films page", () => {
|
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", () => {
|
it("should render Films page instance", () => {
|
||||||
const wrapper = mount(FilmsView);
|
const wrapper = mount(Films, { store, localVue });
|
||||||
expect(wrapper.find("img").exists()).toBe(true);
|
expect(wrapper.find("img").exists()).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should get films from Ghibli API", async () => {
|
it("should get films from Ghibli API", async () => {
|
||||||
let wrapper = shallowMount(FilmsView, {
|
let wrapper = shallowMount(Films, {
|
||||||
mocks: {
|
store,
|
||||||
$axios: axios
|
localVue
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await wrapper.vm.$options.asyncData({ store });
|
||||||
|
expect(actions.getFilms).toHaveBeenCalled();
|
||||||
expect(wrapper.findComponent({ name: "Grid" }).exists()).toBe(true);
|
expect(wrapper.findComponent({ name: "Grid" }).exists()).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -10,8 +10,12 @@ export const mutations = {
|
|||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
async getFilms({ commit }) {
|
async getFilms({ commit }) {
|
||||||
|
try {
|
||||||
const films = await this.$axios.$get("/api/films");
|
const films = await this.$axios.$get("/api/films");
|
||||||
commit("setFilms", films);
|
commit("setFilms", films);
|
||||||
|
} catch (e) {
|
||||||
|
throw Error("API Error occurred.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
35
test/actions.spec.js
Normal file
35
test/actions.spec.js
Normal file
@@ -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."
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
9
test/mutations.spec.js
Normal file
9
test/mutations.spec.js
Normal file
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user