-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
196 lines (155 loc) · 5.18 KB
/
test.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
describe('handshake', function () {
'use strict';
var assume = require('assume')
, Handshake = require('./')
, context = { hi: 'mom' }
, shake;
beforeEach(function () {
shake = new Handshake(context);
});
afterEach(function () {
shake.destroy();
});
it('can be constructed without new', function () {
assume(Handshake()).is.instanceOf(Handshake);
});
describe('#set', function () {
it('returns it self', function () {
assume(shake.set('foo', 'bar')).equals(shake);
});
it('throws an error when trying to set an undefined value', function () {
assume(function () {
shake.set('version', undefined);
}).throws(/Cannot set undefined as value for: version/);
});
});
describe('#update', function () {
it('updates the payload with the set values', function () {
assume(shake.payload.foo).equals(undefined);
shake.set('foo', 'bar').update();
assume(shake.payload.foo).equals('bar');
});
it('executes functions that were assigned using set', function () {
assume(shake.payload.foo).equals(undefined);
shake.set('foo', function foo() {
return 'bla bla';
}).update();
assume(shake.payload.foo).equals('bla bla');
});
it('creates a new object', function () {
shake.payload.boo = 'boo';
assume(shake.payload.boo).equals('boo');
shake.set('foo', 'bar').update();
shake.set('mom', function foo() {
return 'bla bla';
}).update();
assume(shake.payload.boo).equals(undefined);
assume(shake.payload.mom).equals('bla bla');
assume(shake.payload.foo).equals('bar');
});
it('calls the set function with the supplied context', function () {
assume(shake.payload.foo).equals(undefined);
shake.set('foo', function foo() {
return this.hi;
}).update();
assume(shake.payload.foo).equals('mom');
});
});
describe('#get', function () {
it('calls the modify function with a clone of the payload', function (done) {
shake.get(function (payload, next) {
assume(JSON.stringify(payload)).equals(JSON.stringify(shake.payload));
payload.foo = 'bar';
assume(shake.payload.foo).equals(undefined);
next();
}, done);
});
it('allows sync modify functions', function (done) {
shake.get(function (payload) {
assume(payload).is.a('object');
}, done);
});
it('sees returned errors as a failed operation for sync calls', function (done) {
shake.get(function (payload) {
return new Error('Im sorry');
}, function (err, data) {
assume(err.message).equals('Im sorry');
assume(data).includes('error=Im%20sorry');
done();
});
});
it('calls the callback with the encoded result', function (done) {
shake.set('foo', 'bar');
shake.set('hello', 1314);
shake.update();
shake.get(function () {
assume(this).equals(context);
}, function (err, data) {
assume(this).equals(context);
assume(data).equals('foo=bar&hello=1314');
done(err);
});
});
it('timesout of modification takes to damn long', function (done) {
shake.destroy();
shake = new Handshake(context, { 'handshake timeout': '100 ms' });
shake.get(function (payload, next) {
}, function (err) {
assume(err.message).includes('timely manner');
done();
});
});
it('can be configured with a custom stringify method', function (done) {
shake.destroy();
shake = new Handshake(context, { 'stringify': JSON.stringify });
shake.set('hello', 1314).update();
shake.get(function (payload, next) {
payload.foo = 'bar';
next();
}, function (err, data) {
data = JSON.parse(data);
assume(data).is.a('object');
assume(data.foo).equals('bar');
assume(data.hello).equals(1314);
done();
});
});
it('returns an error when stringify fails', function (done) {
shake.destroy();
shake = new Handshake(context, { 'stringify': JSON.stringify });
var foo = { bar: 'bar' };
foo.foo = foo;
shake.set('hello', 1314).update();
shake.get(function (payload, next) {
payload.foo = foo;
next();
}, function (err, data) {
assume(data).is.a('string');
data = JSON.parse(data);
assume(data).is.a('object');
assume(data.error).is.a('string');
assume(data.error).equals(err.message);
done();
});
});
});
describe('#destroy', function () {
it('cleans the timers', function () {
/* istanbul ignore next */
shake.timers.setTimeout(function () {
throw new Error('lol cakes');
}, 1);
shake.destroy();
});
it('returns true on first destruction', function () {
assume(shake.destroy()).equals(true);
});
it('returns false on second destruction', function () {
assume(shake.destroy()).equals(true);
assume(shake.destroy()).equals(false);
assume(shake.destroy()).equals(false);
assume(shake.destroy()).equals(false);
assume(shake.destroy()).equals(false);
});
});
});