-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
110 lines (86 loc) · 2.73 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
/* istanbul ignore next */
describe('handshaking', function () {
'use strict';
var shake
, context = { hi: 'mom' }
, assume = require('assume')
, Handshake = require('./');
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', Number)).equals(shake);
assume(shake.configuration.foo).equals(Number);
});
});
describe('#parse', function () {
it('can use type constructors to parse each value', function (next) {
shake
.set('version', Number)
.set('bool', Boolean);
var calls = 2;
function parser(err, data) {
if (err) return next(err);
assume(data).is.a('object');
assume(data.version).is.a('number');
assume(data.bool).is.a('boolean');
assume(this).deep.equals(context);
if (!(calls--)) next();
}
shake.parse('version=1&bool=false', parser);
shake.parse('version=0&bool=true', parser);
shake.parse('version=0&bool=0', parser);
});
it('calls the completion callback on parse error', function (next) {
shake.destroy();
shake = new Handshake(context, { parse: JSON.parse });
shake.parse({}, function (err, data) {
assume(err).is.instanceOf(Error);
assume(this).deep.equals(context);
next();
});
});
it('calls with error when theres an error property', function (next) {
shake.parse('error=shit', function (err) {
assume(err).is.instanceOf(Error);
assume(err.message).equals('shit');
assume(this).deep.equals(context);
next();
});
});
it('saves the id when we receive an id', function (next) {
shake.parse('id=foo', function (err, data) {
assume(data.id).equals('foo');
assume(shake.data.id).equals('foo');
next();
});
});
});
describe('#get', function () {
it('retrieves the data which was storred in the handshake', function (next) {
shake.parse('foo=bar&bar=foo', function () {
assume(shake.get('foo')).equals('bar');
assume(shake.get('bar')).equals('foo');
assume(shake.get('pez')).equals(undefined);
next();
});
});
});
describe('#destroy', function () {
it('returns true', function () {
assume(shake.destroy()).is.true();
});
it('returns false on second attempt', function () {
assume(shake.destroy()).is.true();
assume(shake.destroy()).is.false();
assume(shake.destroy()).is.false();
});
});
});