Test getFruits action with fruit-api

This commit is contained in:
2020-12-04 02:26:50 +01:00
parent 4b1ac294bd
commit a027285357
12 changed files with 75 additions and 97 deletions

18
tests/unit/fruits.spec.js Normal file
View File

@@ -0,0 +1,18 @@
import { mount } from "@vue/test-utils";
// import axios from "axios";
import store from "@/store/index.js";
import Fruits from "@/views/Fruits.vue";
// jest.mock("axios", () => ({
// get: jest.fn(() => Promise.resolve({ data: {} }))
// }));
describe("Test Fruits page.", () => {
it("dispatches getFruits on mounted", async () => {
// axios.get.mockImplementationOnce(() => Promise.resolve({ data: { data: "value" } }));
const wrapper = mount(Fruits, { store });
await store.dispatch("getFruits");
expect(store.state.fruits.fruitCount).toBe(6);
});
});

View File

@@ -1,8 +1,5 @@
import mockAxios from "axios";
import { mount } from "@vue/test-utils";
import App from "../../src/App.vue";
import Fruits from "../../src/views/Fruits.vue";
import App from "@/App.vue";
describe("App", () => {
const wrapper = mount(App, { stubs: ["router-view"] });
@@ -15,23 +12,3 @@ describe("App", () => {
expect(wrapper.find("#app")).toBeTruthy();
});
});
// TODO: Move the following code to fruits.spec.js
jest.mock("axios", () => ({
get: jest.fn(() => Promise.resolve({ data: { data: "value" } }))
}));
it("should get fruits from fruit-api on mounted", async () => {
const wrapper = mount(Fruits, {
mocks: {
$http: mockAxios
}
});
expect(wrapper.vm.fruits).toEqual([]);
// We need to wait the nextTick to check API response (async).
await wrapper.vm.$nextTick(() => {
expect(wrapper.vm.fruits).toEqual("value");
});
});