donal
2018-04-16 a663e942df46f05082f504c7d1d8c29d3af941d3
commit | author | age
efef31 1 import actions from "@/store/actions";
D 2 import * as all from "../setup.js";
0517f2 3 import axios from "axios";
D 4 import MockAdapter from "axios-mock-adapter";
efef31 5 import sinon from "sinon";
D 6
7 const todos = [
a663e9 8   { _id: 1, title: "learn testing", completed: true },
D 9   { _id: 2, title: "learn testing 2", completed: false }
efef31 10 ];
cacd92 11 let state;
efef31 12
0517f2 13 describe("loadTodos", () => {
D 14   beforeEach(() => {
efef31 15     let mock = new MockAdapter(axios);
0517f2 16     mock.onGet("http://localhost:9000/api/todos").reply(200, todos);
4e5008 17   });
0517f2 18   it("should call commit to the mutation function twice", done => {
4e5008 19     const commit = sinon.spy();
efef31 20     actions.loadTodos({ commit }).then(() => {
D 21       // console.log(commit)
22       expect(commit.calledTwice).toBe(true);
23       done();
24     });
25   });
0517f2 26   it("should first call SET_LOADING", done => {
efef31 27     const commit = sinon.spy();
D 28     actions.loadTodos({ commit }).then(() => {
29       // console.log(commit.firstCall.args[0])
30       expect(commit.firstCall.args[0]).toBe("SET_TODOS");
31       done();
32     });
33   });
0517f2 34   it("should second call SET_TODOS", done => {
efef31 35     const commit = sinon.spy();
D 36     actions.loadTodos({ commit }).then(() => {
37       // console.log(commit)
38       expect(commit.secondCall.args[0]).toBe("SET_LOADING");
39       done();
40     });
41   });
0517f2 42 });
efef31 43
0517f2 44 describe("addTodos", () => {
D 45   beforeEach(() => {
46     state = {};
47     let mock = new MockAdapter(axios);
48     // mock.onPost(/http:\/\/localhost:9000\/api\/todos\/.*/, {})
49     mock.onPost("http://localhost:9000/api/todos").reply(200, todos);
50   });
51   it("should call commit to the mutation function once", done => {
52     const commit = sinon.spy();
53     state.newTodo = "Learn some mocking";
54     actions.addTodo({ commit, state }).then(() => {
55       // console.log(commit)
56       expect(commit.calledOnce).toBe(true);
57       done();
58     });
59   });
60   it("should first call ADD_TODO", done => {
61     const commit = sinon.spy();
62     state.newTodo = "Learn some mocking";
63     actions.addTodo({ commit, state }).then(() => {
64       // console.log(commit.firstCall.args[0])
65       expect(commit.firstCall.args[0]).toBe("ADD_TODO");
66       done();
67     });
68   });
efef31 69 });
cacd92 70
D 71 describe("setNewTodo", () => {
72   it("should call SET_NEW_TODO", () => {
73     const commit = sinon.spy();
74     actions.setNewTodo({ commit, todo: "learn stuff about mockin" });
75     expect(commit.firstCall.args[0]).toBe("SET_NEW_TODO");
76   });
77 });
78
79 describe("clearNewTodo", () => {
80   it("should call CLEAR_NEW_TODO", () => {
81     const commit = sinon.spy();
82     actions.clearNewTodo({ commit });
83     expect(commit.firstCall.args[0]).toBe("CLEAR_NEW_TODO");
84   });
85 });
86
87 describe("clearTodos", () => {
88   it("should call CLEAR_ALL_TODOS when all is true", () => {
89     const commit = sinon.spy();
90     state.todos = todos;
91     actions.clearTodos({ commit, state }, true);
92     expect(commit.firstCall.args[0]).toBe("CLEAR_ALL_TODOS");
93   });
94
95   it("should call CLEAR_ALL_DONE_TODOS when all is not passed", () => {
96     const commit = sinon.spy();
97     state.todos = todos;
98     actions.clearTodos({ commit, state });
99     expect(commit.firstCall.args[0]).toBe("CLEAR_ALL_DONE_TODOS");
100   });
101 });
102
103 /* 
a663e9 104   updateTodo({ commit, state }, { id, important }) {
cacd92 105     let i = state.todos.findIndex(todo => todo._id === id);
D 106     // todo - add back end
107     return axios
108       .put(config.todoEndpoint + "/" + state.todos[i]._id, state.todos[i])
109       .then(data => {
110         console.info("INFO - item " + id + " updated", data);
111         commit("MARK_TODO_COMPLETED", i);
112       });
113   }
114 */
a663e9 115 describe("updateTodo", () => {
D 116   beforeEach(() => {
117     state = {};
118     let mock = new MockAdapter(axios);
119     mock.onPut("http://localhost:9000/api/todos/1").reply(200, todos);
120   });
121   it("should call commit to the mutation function once", done => {
122     const commit = sinon.spy();
123     state.todos = todos;
124     actions.updateTodo({ commit, state }, { id: 1 }).then(() => {
125       expect(commit.calledOnce).toBe(true);
126       done();
127     });
128   });
129   it("should call MARK_TODO_COMPLETED", done => {
130     const commit = sinon.spy();
131     state.todos = todos;
132     actions.updateTodo({ commit, state }, { id: 1 }).then(() => {
133       // console.log(commit.firstCall.args[0])
134       expect(commit.firstCall.args[0]).toBe("MARK_TODO_COMPLETED");
135       done();
136     });
137   });
138 });