donal
2018-04-16 78a403c450425ea86fad7df5737a4a53ed4c6714
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
a663e9 103 describe("updateTodo", () => {
D 104   beforeEach(() => {
105     state = {};
106     let mock = new MockAdapter(axios);
107     mock.onPut("http://localhost:9000/api/todos/1").reply(200, todos);
108   });
109   it("should call commit to the mutation function once", done => {
110     const commit = sinon.spy();
111     state.todos = todos;
112     actions.updateTodo({ commit, state }, { id: 1 }).then(() => {
113       expect(commit.calledOnce).toBe(true);
114       done();
115     });
116   });
117   it("should call MARK_TODO_COMPLETED", done => {
118     const commit = sinon.spy();
119     state.todos = todos;
120     actions.updateTodo({ commit, state }, { id: 1 }).then(() => {
121       // console.log(commit.firstCall.args[0])
122       expect(commit.firstCall.args[0]).toBe("MARK_TODO_COMPLETED");
123       done();
124     });
125   });
78a403 126   // TODO - test goes here!
D 127
a663e9 128 });