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