-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.js
202 lines (160 loc) · 5.27 KB
/
tests.js
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"use strict";
const {
assert,
spy,
stub,
mock,
createSandbox
} = require("sinon");
const database = require("./src/db");
const store = require("./src/store")(database);
// Sample user data
const harry = {id: 0, name: "Harry"};
const john = {id: 1, name: "John"};
/**
* Stubs:
*
* Replace calls to databases or network requests.
* Stubbing (replacing) *some* of the functionality.
*
* Pros:
* - Replace calls to db
* - Replace network requests with this
* - Force an error to test error handling
*
*/
it("very basic stub", () => {
// Stub database method
const getUsers = stub(database, "getUsers");
// Overrides database.getUsers() implementation
getUsers.onFirstCall().returns([harry, john]);
// `store.database.getUsers()` returns `[harry, john]`
assert.match(store.database.getUsers().length, 2)
// Put method back to normal 'remove stub'
getUsers.restore();
});
describe("stubs", () => {
// Cache the stubbed methods
let load; let save;
beforeEach(() => {
// Stub out the database method
load = stub(database, "getUsers");
// Overriding implementation
load.onFirstCall().returns([]);
// Stub out another database method,
// but don't override its result
save = stub(database, "setUsers");
});
afterEach(() => {
// Unset the stubbed methods
load.restore();
save.restore();
});
it("store starts empty", () => {
// Stubs the specific method
// This is an example of a pointless assertion
// The result is defined above:
// `load.onFirstCall().returns([]);`
// There is no need to check the length.
assert.match(store.database.getUsers().length, 0)
});
it("new users can be added to the store", () => {
const currentUsers = [john];
const newUser = harry;
// Can override the initial stubbed method
load.onFirstCall().returns(currentUsers);
// Call our method we are testing
store.addUser(newUser, null);
// Check that the function `database.setUsers()` was called with
// our newly added user
assert.calledWith(save, [...currentUsers, newUser], null);
});
});
describe("spies", () => {
/**
* Spies:
*
* Spy on functions and see if they have been called.
*
* Pros:
* - Check if method has been called with certain variables
* - Check if callbacks have been called, and how many times
*
*/
it("check that adding a user returns the updated list of users", () => {
// Spy on a variable
// Sinon is now watching this variable
const callback = spy();
// Use the variable in our function
store.addUser(john, callback);
// Now check that `callback([harry, john])` was called inside of `database.addUser()`
assert.calledWith(callback, [harry, john]);
// Now spy on our save method.
// Spies won't change the method, just tracks when `database.setUsers()` is called
let save = spy(database, "setUsers");
// This will now execute the database method
// Requiring the database to be setup first.
store.addUser(harry, () => {});
// Check that our `database.setUsers()` method, was called first.
assert.calledOnce(save);
// Revert our spy on `database.setUsers()`
save.restore();
});
});
describe("sandpit", () => {
/**
* Create a sandpit, and call everything using this pit
* then you don't have to call `x.restore()`.
*/
const sandbox = createSandbox();
let load; let save;
beforeEach(() => {
// Call same stubbing functions, but using `sandbox.`
load = sandbox.stub(database, "getUsers");
load.onFirstCall().returns([]);
save = sandbox.stub(database, "setUsers");
save.onFirstCall().returns(function() { return [harry] });
});
// Calls .restore() on load and save.
afterEach(() => sandbox.restore());
it("can add only users to the store", () => {
// Check our method is mocked
assert.match(database.getUsers().length, 0);
});
});
it("check that our sandpit works", () => {
// This will perform database tasks,
// instead of using our mocks because they are removed
store.addUser(harry, () => {});
assert.match(store.database.getUsers().length, 1);
});
/**
* Mocks:
*
* Use mocks to stub out whole objects.
*/
it("mocks", () => {
// Mock our whole database object
var db = mock(store.database);
// Setup our mock to replace functionality,
// if all we care about is that these methods
// were called once, we replace them with checks.
db.expects("setUsers").once();
// This needs to return something to continue with the method
// so we stub out the return value of `getUsers()`
db.expects("getUsers").once().returns([]);
store.addUser(harry, () => {});
// Verify that the mocked expects happened
// setUsers() was called once
// getUsers() was called once
db.verify();
});
/**
* WithArgs
*/
it.only("with args comparison", () => {
const fn = stub();
fn.withArgs({id: 1}).returns({name: "harry"});
const result = fn({id: 1});
assert.match("harry", result.name);
});