Create vuex modules for Films/Vehicles/Locations/People + Test adaptation
This commit is contained in:
@@ -13,7 +13,7 @@ const localVue = createLocalVue();
|
|||||||
localVue.use(Vuex);
|
localVue.use(Vuex);
|
||||||
|
|
||||||
describe("Film page", () => {
|
describe("Film page", () => {
|
||||||
let state, actions, store;
|
let state, actions, getters, store;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
state = {
|
state = {
|
||||||
@@ -22,7 +22,19 @@ describe("Film page", () => {
|
|||||||
actions = {
|
actions = {
|
||||||
getFilm: jest.fn()
|
getFilm: jest.fn()
|
||||||
};
|
};
|
||||||
store = new Vuex.Store({ state, actions });
|
getters = {
|
||||||
|
film: jest.fn()
|
||||||
|
};
|
||||||
|
store = new Vuex.Store({
|
||||||
|
modules: {
|
||||||
|
films: {
|
||||||
|
namespaced: true,
|
||||||
|
state,
|
||||||
|
actions,
|
||||||
|
getters
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should render Film page instance", () => {
|
it("should render Film page instance", () => {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { mapState } from "vuex";
|
import { mapGetters } from "vuex";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "Film",
|
name: "Film",
|
||||||
@@ -21,10 +21,12 @@ export default {
|
|||||||
return uuid.test(params.id);
|
return uuid.test(params.id);
|
||||||
},
|
},
|
||||||
async asyncData({ params, store }) {
|
async asyncData({ params, store }) {
|
||||||
await store.dispatch("getFilm", params.id);
|
await store.dispatch("films/getFilm", params.id);
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
...mapState(["film"])
|
...mapGetters({
|
||||||
|
film: "films/film"
|
||||||
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -6,31 +6,43 @@ const localVue = createLocalVue();
|
|||||||
localVue.use(Vuex);
|
localVue.use(Vuex);
|
||||||
|
|
||||||
describe("Films page", () => {
|
describe("Films page", () => {
|
||||||
let state, actions, store;
|
let state, actions, getters, store;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
state = {
|
state = {
|
||||||
films: []
|
list: []
|
||||||
};
|
};
|
||||||
actions = {
|
actions = {
|
||||||
getFilms: jest.fn()
|
getList: jest.fn()
|
||||||
};
|
};
|
||||||
store = new Vuex.Store({ state, actions });
|
getters = {
|
||||||
|
list: jest.fn()
|
||||||
|
};
|
||||||
|
store = new Vuex.Store({
|
||||||
|
modules: {
|
||||||
|
films: {
|
||||||
|
namespaced: true,
|
||||||
|
state,
|
||||||
|
actions,
|
||||||
|
getters
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should render Films page instance", () => {
|
it("should render Films page instance", () => {
|
||||||
const wrapper = mount(Films, { store, localVue });
|
const wrapper = shallowMount(Films, { store, localVue });
|
||||||
expect(wrapper.find("img").exists()).toBe(true);
|
expect(wrapper.find("img").exists()).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should dispatch getFilms action", async () => {
|
it("should dispatch films/getList action", async () => {
|
||||||
let wrapper = shallowMount(Films, {
|
let wrapper = shallowMount(Films, {
|
||||||
store,
|
store,
|
||||||
localVue
|
localVue
|
||||||
});
|
});
|
||||||
|
|
||||||
await wrapper.vm.$options.asyncData({ store });
|
await wrapper.vm.$options.asyncData({ store });
|
||||||
expect(actions.getFilms).toHaveBeenCalled();
|
expect(actions.getList).toHaveBeenCalled();
|
||||||
expect(wrapper.findComponent({ name: "Grid" }).exists()).toBe(true);
|
expect(wrapper.findComponent({ name: "Grid" }).exists()).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import Grid from "@/components/Grid";
|
import Grid from "@/components/Grid";
|
||||||
import { mapState } from "vuex";
|
import { mapGetters } from "vuex";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "Films",
|
name: "Films",
|
||||||
@@ -20,10 +20,12 @@ export default {
|
|||||||
titleTemplate: "%s - Films"
|
titleTemplate: "%s - Films"
|
||||||
},
|
},
|
||||||
async asyncData({ store }) {
|
async asyncData({ store }) {
|
||||||
await store.dispatch("getFilms");
|
if (!store.state.films.list.length) await store.dispatch("films/getList");
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
...mapState(["films"])
|
...mapGetters({
|
||||||
|
films: "films/list"
|
||||||
|
})
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { actions } from "@/store";
|
import { actions } from "./";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import mockFilms from "@/test/fake-films.json";
|
import mockFilms from "@/test/fake-films.json";
|
||||||
|
|
||||||
@@ -14,21 +14,21 @@ jest.mock("axios", () => ({
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
describe("Vuex actions.", () => {
|
describe("Vuex actions.", () => {
|
||||||
it("tests getFilms action.", async () => {
|
it("tests films/getList action.", async () => {
|
||||||
const commit = jest.fn();
|
const commit = jest.fn();
|
||||||
|
|
||||||
actions.$axios = axios;
|
actions.$axios = axios;
|
||||||
await actions.getFilms({ commit });
|
await actions.getList({ commit });
|
||||||
|
|
||||||
expect(url).toBe("/api/films");
|
expect(url).toBe("/api/films");
|
||||||
expect(commit).toHaveBeenCalledWith("setFilms", mockFilms);
|
expect(commit).toHaveBeenCalledWith("setList", mockFilms);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("catches errors.", async () => {
|
it("catches errors.", async () => {
|
||||||
const commit = jest.fn();
|
const commit = jest.fn();
|
||||||
|
|
||||||
actions.$axios = null;
|
actions.$axios = null;
|
||||||
await expect(actions.getFilms({ commit })).rejects.toThrow(
|
await expect(actions.getList({ commit })).rejects.toThrow(
|
||||||
"API Error occurred."
|
"API Error occurred."
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
37
store/films/index.js
Normal file
37
store/films/index.js
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
export const state = () => ({
|
||||||
|
list: [],
|
||||||
|
film: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
export const mutations = {
|
||||||
|
setList: (state, films) => {
|
||||||
|
state.list = films;
|
||||||
|
},
|
||||||
|
setFilm: (state, film) => {
|
||||||
|
state.film = film;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const actions = {
|
||||||
|
async getList({ commit }) {
|
||||||
|
try {
|
||||||
|
const films = await this.$axios.$get("/api/films");
|
||||||
|
commit("setList", films);
|
||||||
|
} 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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getters = {
|
||||||
|
list: state => state.list,
|
||||||
|
film: state => state.film
|
||||||
|
};
|
||||||
9
store/films/mutations.spec.js
Normal file
9
store/films/mutations.spec.js
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { state, mutations } from "./";
|
||||||
|
import mockFilms from "@/test/fake-films.json";
|
||||||
|
|
||||||
|
describe("Vuex mutations.", () => {
|
||||||
|
it("tests setFilms mutation.", () => {
|
||||||
|
mutations.setList(state, mockFilms);
|
||||||
|
expect(state.list[0].id).toBe(mockFilms[0].id);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -1,70 +1,7 @@
|
|||||||
export const state = () => ({
|
export const state = () => ({});
|
||||||
films: [],
|
|
||||||
film: {},
|
|
||||||
location: {},
|
|
||||||
person: {},
|
|
||||||
vehicle: {}
|
|
||||||
});
|
|
||||||
|
|
||||||
export const mutations = {
|
export const mutations = {};
|
||||||
setFilms: (state, films) => {
|
|
||||||
state.films = films;
|
|
||||||
},
|
|
||||||
setFilm: (state, film) => {
|
|
||||||
state.film = film;
|
|
||||||
},
|
|
||||||
setLocation: (state, location) => {
|
|
||||||
state.location = location;
|
|
||||||
},
|
|
||||||
setPerson: (state, person) => {
|
|
||||||
state.person = person;
|
|
||||||
},
|
|
||||||
setVehicle: (state, vehicle) => {
|
|
||||||
state.vehicle = vehicle;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {};
|
||||||
async getFilms({ commit }) {
|
|
||||||
try {
|
|
||||||
const films = await this.$axios.$get("/api/films");
|
|
||||||
commit("setFilms", films);
|
|
||||||
} 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.");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
async getLocation({ commit }, id) {
|
|
||||||
try {
|
|
||||||
const location = await this.$axios.$get(`/api/locations/${id}`);
|
|
||||||
commit("setLocation", location);
|
|
||||||
} catch (e) {
|
|
||||||
throw Error("API Error occurred.");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
async getPerson({ commit }, id) {
|
|
||||||
try {
|
|
||||||
const person = await this.$axios.$get(`/api/people/${id}`);
|
|
||||||
commit("setPerson", person);
|
|
||||||
} catch (e) {
|
|
||||||
throw Error("API Error occurred.");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
async getVehicle({ commit }, id) {
|
|
||||||
try {
|
|
||||||
const vehicle = await this.$axios.$get(`/api/vehicles/${id}`);
|
|
||||||
commit("setVehicle", vehicle);
|
|
||||||
} catch (e) {
|
|
||||||
throw Error("API Error occurred.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getters = {};
|
export const getters = {};
|
||||||
|
|||||||
25
store/locations/index.js
Normal file
25
store/locations/index.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
export const state = () => ({
|
||||||
|
location: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
export const mutations = {
|
||||||
|
setlocation: (state, location) => {
|
||||||
|
state.location = location;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const actions = {
|
||||||
|
async getLocation({ commit }, { id, callback = false }) {
|
||||||
|
try {
|
||||||
|
const location = await this.$axios.$get(`/api/locations/${id}`);
|
||||||
|
if (callback) return location;
|
||||||
|
commit("setLocation", location);
|
||||||
|
} catch (e) {
|
||||||
|
throw Error("API Error occurred.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getters = {
|
||||||
|
location: state => state.location
|
||||||
|
};
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
import { state, mutations } from "@/store";
|
|
||||||
import mockFilms from "@/test/fake-films.json";
|
|
||||||
|
|
||||||
describe("Vuex mutations.", () => {
|
|
||||||
it("tests setFilms mutation.", () => {
|
|
||||||
mutations.setFilms(state, mockFilms);
|
|
||||||
expect(state.films[0].id).toBe(mockFilms[0].id);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
25
store/people/index.js
Normal file
25
store/people/index.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
export const state = () => ({
|
||||||
|
person: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
export const mutations = {
|
||||||
|
setPerson: (state, person) => {
|
||||||
|
state.person = person;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const actions = {
|
||||||
|
async getPerson({ commit }, { id, callback = false }) {
|
||||||
|
try {
|
||||||
|
const person = await this.$axios.$get(`/api/people/${id}`);
|
||||||
|
if (callback) return person;
|
||||||
|
commit("setPerson", person);
|
||||||
|
} catch (e) {
|
||||||
|
throw Error("API Error occurred.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getters = {
|
||||||
|
person: state => state.person
|
||||||
|
};
|
||||||
25
store/vehicles/index.js
Normal file
25
store/vehicles/index.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
export const state = () => ({
|
||||||
|
vehicle: {}
|
||||||
|
});
|
||||||
|
|
||||||
|
export const mutations = {
|
||||||
|
setVehicle: (state, vehicle) => {
|
||||||
|
state.vehicle = vehicle;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const actions = {
|
||||||
|
async getvehicle({ commit }, { id, callback = false }) {
|
||||||
|
try {
|
||||||
|
const vehicle = await this.$axios.$get(`/api/vehicles/${id}`);
|
||||||
|
if (callback) return vehicle;
|
||||||
|
commit("setVehicle", vehicle);
|
||||||
|
} catch (e) {
|
||||||
|
throw Error("API Error occurred.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getters = {
|
||||||
|
vehicle: state => state.vehicle
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user