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