Create vuex modules for Films/Vehicles/Locations/People + Test adaptation
This commit is contained in:
35
store/films/actions.spec.js
Normal file
35
store/films/actions.spec.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import { actions } from "./";
|
||||
import axios from "axios";
|
||||
import mockFilms from "@/test/fake-films.json";
|
||||
|
||||
let url = "";
|
||||
|
||||
jest.mock("axios", () => ({
|
||||
$get: _url => {
|
||||
return new Promise(resolve => {
|
||||
url = _url;
|
||||
resolve(mockFilms);
|
||||
});
|
||||
}
|
||||
}));
|
||||
|
||||
describe("Vuex actions.", () => {
|
||||
it("tests films/getList action.", async () => {
|
||||
const commit = jest.fn();
|
||||
|
||||
actions.$axios = axios;
|
||||
await actions.getList({ commit });
|
||||
|
||||
expect(url).toBe("/api/films");
|
||||
expect(commit).toHaveBeenCalledWith("setList", mockFilms);
|
||||
});
|
||||
|
||||
it("catches errors.", async () => {
|
||||
const commit = jest.fn();
|
||||
|
||||
actions.$axios = null;
|
||||
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);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user