Create vuex modules for Films/Vehicles/Locations/People + Test adaptation
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { actions } from "@/store";
|
||||
import { actions } from "./";
|
||||
import axios from "axios";
|
||||
import mockFilms from "@/test/fake-films.json";
|
||||
|
||||
@@ -14,21 +14,21 @@ jest.mock("axios", () => ({
|
||||
}));
|
||||
|
||||
describe("Vuex actions.", () => {
|
||||
it("tests getFilms action.", async () => {
|
||||
it("tests films/getList action.", async () => {
|
||||
const commit = jest.fn();
|
||||
|
||||
actions.$axios = axios;
|
||||
await actions.getFilms({ commit });
|
||||
await actions.getList({ commit });
|
||||
|
||||
expect(url).toBe("/api/films");
|
||||
expect(commit).toHaveBeenCalledWith("setFilms", mockFilms);
|
||||
expect(commit).toHaveBeenCalledWith("setList", mockFilms);
|
||||
});
|
||||
|
||||
it("catches errors.", async () => {
|
||||
const commit = jest.fn();
|
||||
|
||||
actions.$axios = null;
|
||||
await expect(actions.getFilms({ commit })).rejects.toThrow(
|
||||
await expect(actions.getList({ commit })).rejects.toThrow(
|
||||
"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 = () => ({
|
||||
films: [],
|
||||
film: {},
|
||||
location: {},
|
||||
person: {},
|
||||
vehicle: {}
|
||||
});
|
||||
export const state = () => ({});
|
||||
|
||||
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 mutations = {};
|
||||
|
||||
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 actions = {};
|
||||
|
||||
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