Test asyncData method.

This commit is contained in:
2020-12-17 17:07:22 +01:00
parent 1a86713c00
commit 7b52deccd8
3 changed files with 504 additions and 3 deletions

37
test/index.spec.js Normal file
View File

@@ -0,0 +1,37 @@
import { mount } from "@vue/test-utils";
import axios from "axios";
import FilmsView from "@/pages/index";
import mockFilms from "./fake-films.json";
jest.mock("axios", () => ({
$get: jest.fn(() => mockFilms)
}));
describe("Films page", () => {
it("should render Films page instance", () => {
const wrapper = mount(FilmsView);
expect(wrapper.find("h1").text()).toBe("Ghibli films");
});
it("should get films from Ghibli API", async () => {
let wrapper = mount(FilmsView, {
mocks: {
$axios: axios
}
});
expect((await wrapper.vm.$options.asyncData(wrapper.vm)).films).toEqual(
mockFilms
);
// Init page with mocked asyncData
wrapper = mount(FilmsView, {
mocks: {
films: (await wrapper.vm.$options.asyncData(wrapper.vm)).films,
$axios: axios
}
});
expect(JSON.parse(wrapper.find(".films").text())).toEqual(mockFilms);
});
});