From e1619de1cd85111c7ba36749f60ab4b4d83cd06e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Viricel?= Date: Fri, 18 Dec 2020 06:12:39 +0100 Subject: [PATCH] Refactor films/_id to use vuex --- pages/films/_id.spec.js | 54 ++++++++++++++++----------------------- pages/films/_id.vue | 13 +++++----- pages/films/index.spec.js | 2 +- store/index.js | 14 +++++++++- 4 files changed, 42 insertions(+), 41 deletions(-) diff --git a/pages/films/_id.spec.js b/pages/films/_id.spec.js index 18c23f7..7f975c2 100644 --- a/pages/films/_id.spec.js +++ b/pages/films/_id.spec.js @@ -1,7 +1,6 @@ -import { mount } from "@vue/test-utils"; -import axios from "axios"; +import { mount, createLocalVue } from "@vue/test-utils"; +import Vuex from "vuex"; import Film from "./_id"; -import mockFilms from "@/test/fake-films.json"; let $route = { path: "/films", @@ -10,15 +9,24 @@ let $route = { } }; -const mockFilm = mockFilms.filter(o => o.id === $route.params.id); - -jest.mock("axios", () => ({ - $get: jest.fn(() => mockFilm) -})); +const localVue = createLocalVue(); +localVue.use(Vuex); describe("Film page", () => { + let state, actions, store; + + beforeEach(() => { + state = { + film: {} + }; + actions = { + getFilm: jest.fn() + }; + store = new Vuex.Store({ state, actions }); + }); + it("should render Film page instance", () => { - const wrapper = mount(Film); + const wrapper = mount(Film, { localVue, store }); expect(wrapper.exists()).toBe(true); }); @@ -36,31 +44,13 @@ describe("Film page", () => { ).toBe(false); }); - it("should get film from Ghibli API", async () => { + it("should dispatch getFilm action", async () => { let wrapper = mount(Film, { - mocks: { - $route, - $axios: axios - } + localVue, + store }); - expect( - ( - await wrapper.vm.$options.asyncData({ - $axios: axios, - params: $route.params - }) - ).film - ).toEqual(mockFilm); - - // // Init page with mocked asyncData - // wrapper = shallowMount(Film, { - // mocks: { - // films: (await wrapper.vm.$options.asyncData(wrapper.vm)).films, - // $axios: axios - // } - // }); - - // expect(wrapper.findComponent({ name: "Grid" }).exists()).toBe(true); + await wrapper.vm.$options.asyncData({ store, params: $route.params }); + expect(actions.getFilm).toHaveBeenCalled(); }); }); diff --git a/pages/films/_id.vue b/pages/films/_id.vue index b5c19af..7c60859 100644 --- a/pages/films/_id.vue +++ b/pages/films/_id.vue @@ -5,6 +5,8 @@ diff --git a/pages/films/index.spec.js b/pages/films/index.spec.js index 6e80121..cb1bc49 100644 --- a/pages/films/index.spec.js +++ b/pages/films/index.spec.js @@ -23,7 +23,7 @@ describe("Films page", () => { expect(wrapper.find("img").exists()).toBe(true); }); - it("should get films from Ghibli API", async () => { + it("should dispatch getFilms action", async () => { let wrapper = shallowMount(Films, { store, localVue diff --git a/store/index.js b/store/index.js index 985bbdd..685a29e 100644 --- a/store/index.js +++ b/store/index.js @@ -1,10 +1,14 @@ export const state = () => ({ - films: [] + films: [], + film: {} }); export const mutations = { setFilms: (state, films) => { state.films = films; + }, + setFilm: (state, film) => { + state.film = film; } }; @@ -16,6 +20,14 @@ export const actions = { } catch (e) { throw Error("API Error occurred."); } + }, + async getFilm({ commit }, id) { + try { + const film = await this.$axios.$get(`/api/films/${id}`); + commit("setFilm", film); + } catch (e) { + throw Error("API Error occurred."); + } } };