Install axios + Test App rendering + Test API connection

This commit is contained in:
2020-12-03 23:05:00 +01:00
parent dc3a764aa2
commit 69e0f90c5a
7 changed files with 63 additions and 15 deletions

View File

@@ -1,12 +0,0 @@
import { shallowMount } from '@vue/test-utils';
import HelloWorld from '@/components/HelloWorld.vue';
describe('HelloWorld.vue', () => {
it('renders props.msg when passed', () => {
const msg = 'new message';
const wrapper = shallowMount(HelloWorld, {
propsData: { msg },
});
expect(wrapper.text()).toMatch(msg);
});
});

35
tests/unit/global.spec.js Normal file
View File

@@ -0,0 +1,35 @@
import { mount } from "@vue/test-utils";
import mockAxios from "axios";
import App from "../../src/App.vue";
import Fruits from "../../src/views/Fruits.vue";
describe("App", () => {
const wrapper = mount(App, { stubs: ["router-view"] });
it("should be a Vue instance", () => {
expect(wrapper.vm).toBeTruthy();
});
it("renders correctly", () => {
expect(wrapper.find("#app")).toBeTruthy();
});
});
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");
});
});