Test mutation + actions
This commit is contained in:
@@ -1,21 +1,18 @@
|
||||
module.exports = {
|
||||
moduleNameMapper: {
|
||||
'^@/(.*)$': '<rootDir>/$1',
|
||||
'^~/(.*)$': '<rootDir>/$1',
|
||||
'^vue$': 'vue/dist/vue.common.js'
|
||||
"^@/(.*)$": "<rootDir>/$1",
|
||||
"^~/(.*)$": "<rootDir>/$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: "<rootDir>/coverage",
|
||||
collectCoverage: true,
|
||||
collectCoverageFrom: [
|
||||
'<rootDir>/components/**/*.vue',
|
||||
'<rootDir>/pages/**/*.vue'
|
||||
"<rootDir>/components/**/*.vue",
|
||||
"<rootDir>/pages/**/*.vue"
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -10,8 +10,12 @@ export const mutations = {
|
||||
|
||||
export const actions = {
|
||||
async getFilms({ commit }) {
|
||||
try {
|
||||
const films = await this.$axios.$get("/api/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