Refactor films/_id to use vuex

This commit is contained in:
2020-12-18 06:12:39 +01:00
parent 18be371f39
commit e1619de1cd
4 changed files with 42 additions and 41 deletions

View File

@@ -1,7 +1,6 @@
import { mount } from "@vue/test-utils"; import { mount, createLocalVue } from "@vue/test-utils";
import axios from "axios"; import Vuex from "vuex";
import Film from "./_id"; import Film from "./_id";
import mockFilms from "@/test/fake-films.json";
let $route = { let $route = {
path: "/films", path: "/films",
@@ -10,15 +9,24 @@ let $route = {
} }
}; };
const mockFilm = mockFilms.filter(o => o.id === $route.params.id); const localVue = createLocalVue();
localVue.use(Vuex);
jest.mock("axios", () => ({
$get: jest.fn(() => mockFilm)
}));
describe("Film page", () => { 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", () => { it("should render Film page instance", () => {
const wrapper = mount(Film); const wrapper = mount(Film, { localVue, store });
expect(wrapper.exists()).toBe(true); expect(wrapper.exists()).toBe(true);
}); });
@@ -36,31 +44,13 @@ describe("Film page", () => {
).toBe(false); ).toBe(false);
}); });
it("should get film from Ghibli API", async () => { it("should dispatch getFilm action", async () => {
let wrapper = mount(Film, { let wrapper = mount(Film, {
mocks: { localVue,
$route, store
$axios: axios
}
}); });
expect( await wrapper.vm.$options.asyncData({ store, params: $route.params });
( expect(actions.getFilm).toHaveBeenCalled();
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);
}); });
}); });

View File

@@ -5,6 +5,8 @@
</template> </template>
<script> <script>
import { mapState } from "vuex";
export default { export default {
name: "Film", name: "Film",
head() { head() {
@@ -18,14 +20,11 @@ export default {
); );
return uuid.test(params.id); return uuid.test(params.id);
}, },
async asyncData({ $axios, params }) { async asyncData({ params, store }) {
const film = await $axios.$get(`/api/films/${params.id}`); await store.dispatch("getFilm", params.id);
return { film };
}, },
data() { computed: {
return { ...mapState(["film"])
film: {}
};
} }
}; };
</script> </script>

View File

@@ -23,7 +23,7 @@ describe("Films page", () => {
expect(wrapper.find("img").exists()).toBe(true); 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, { let wrapper = shallowMount(Films, {
store, store,
localVue localVue

View File

@@ -1,10 +1,14 @@
export const state = () => ({ export const state = () => ({
films: [] films: [],
film: {}
}); });
export const mutations = { export const mutations = {
setFilms: (state, films) => { setFilms: (state, films) => {
state.films = films; state.films = films;
},
setFilm: (state, film) => {
state.film = film;
} }
}; };
@@ -16,6 +20,14 @@ export const actions = {
} catch (e) { } catch (e) {
throw Error("API Error occurred."); 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.");
}
} }
}; };