Test mutation + actions
This commit is contained in:
35
test/actions.spec.js
Normal file
35
test/actions.spec.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import { actions, mutations } from "@/store";
|
||||
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 getFilms action.", async () => {
|
||||
const commit = jest.fn();
|
||||
|
||||
actions.$axios = axios;
|
||||
await actions.getFilms({ commit });
|
||||
|
||||
expect(url).toBe("/api/films");
|
||||
expect(commit).toHaveBeenCalledWith("setFilms", mockFilms);
|
||||
});
|
||||
|
||||
it("catches errors.", async () => {
|
||||
const commit = jest.fn();
|
||||
|
||||
actions.$axios = null;
|
||||
await expect(actions.getFilms({ commit })).rejects.toThrow(
|
||||
"API Error occurred."
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user