Fix people details page + pass test

This commit is contained in:
2020-12-21 22:53:04 +01:00
parent c3bb17f863
commit 3a8c052d86
3 changed files with 30 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
import { mount, createLocalVue } from "@vue/test-utils"; import { mount, createLocalVue } from "@vue/test-utils";
import Vuex from "vuex"; import Vuex from "vuex";
import Person from "./_id"; import Person from "./_id";
import mockPeople from "@/test/fake-people.json";
let $route = { let $route = {
path: "/people", path: "/people",
@@ -22,11 +23,25 @@ describe("Person page", () => {
actions = { actions = {
getPerson: jest.fn() getPerson: jest.fn()
}; };
store = new Vuex.Store({ state, actions }); store = new Vuex.Store({
modules: {
people: {
namespaced: true,
state,
actions
}
}
});
}); });
it("should render Person page instance", () => { it("should render Person page instance", () => {
const wrapper = mount(Person, { localVue, store }); const wrapper = mount(Person, {
localVue,
store,
computed: {
person: () => mockPeople[0]
}
});
expect(wrapper.exists()).toBe(true); expect(wrapper.exists()).toBe(true);
}); });
@@ -47,7 +62,10 @@ describe("Person page", () => {
it("should dispatch getPerson action", async () => { it("should dispatch getPerson action", async () => {
let wrapper = mount(Person, { let wrapper = mount(Person, {
localVue, localVue,
store store,
computed: {
person: () => mockPeople[0]
}
}); });
await wrapper.vm.$options.asyncData({ store, params: $route.params }); await wrapper.vm.$options.asyncData({ store, params: $route.params });

View File

@@ -5,7 +5,7 @@
</template> </template>
<script> <script>
import { mapState } from "vuex"; import { mapGetters } from "vuex";
export default { export default {
name: "Person", name: "Person",
@@ -21,10 +21,13 @@ export default {
return uuid.test(params.id); return uuid.test(params.id);
}, },
async asyncData({ params, store }) { async asyncData({ params, store }) {
await store.dispatch("getPerson", params.id); if (!store.state.people.person.id !== params.id)
await store.dispatch("people/getPerson", params.id);
}, },
computed: { computed: {
...mapState(["person"]) ...mapGetters({
person: "people/person"
})
} }
}; };
</script> </script>

View File

@@ -25,7 +25,9 @@ export const actions = {
}, },
async getPerson({ commit }, id) { async getPerson({ commit }, id) {
try { try {
const person = await this.$axios.$get(`/api/people/${id}`); const person = await this.$axios.$get(
`/api/people/${id}?fields=id,name,gender,age,eye_color,hair_color,films`
);
commit("setPerson", person); commit("setPerson", person);
} catch (e) { } catch (e) {
throw Error(`API Error occurred: ${e.message}`); throw Error(`API Error occurred: ${e.message}`);