-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
220 lines (175 loc) · 6.24 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* istanbul ignore next */
describe('stingray', function () {
'use strict';
//
// Introduce some globals as this library should run in browsers only unless
// we polyfill/mock some browser things.
//
global.Image = require('beacons/image');
global.navigator = {
cookieEnabled: true,
language: 'en-US',
productSub: '20030107',
product: 'Gecko',
appCodeName: 'Mozilla',
mimeTypes: { length: 0 },
vendorSub: '',
vendor: 'Apple Computer, Inc.',
platform: 'MacIntel',
appName: 'Netscape',
appVersion: '5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.8 Safari/534.34',
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.8 Safari/534.34',
plugins: { length: 0 },
onLine: false
};
global.document = {
charset: 'UTF-8',
domain: 'localhost',
inputEncoding: 'UTF-8',
readyState: 'complete',
referrer: '',
URL: 'http://localhost:52091/?brout',
visibility: undefined
};
var qs = require('querystringify')
, parse = require('url-parse')
, assume = require('assume')
, Stingray = require('./')
, server
, stingray;
beforeEach(function () {
server = 'http://example.com';
stingray = new Stingray(server);
});
afterEach(function () {
stingray.destroy();
});
it('is exported a function', function () {
assume(Stingray).is.a('function');
});
it('can be constructed without new', function () {
assume(Stingray()).is.instanceof(Stingray);
});
describe('construction', function () {
it('accepts a custom limit', function () {
var u = new Stingray(server, { limit : 500 });
assume(u.limit).does.not.equal(stingray.limit);
assume(u.limit).equals(500);
});
});
describe('#set', function () {
it('sets items to the internal data set', function () {
assume(stingray.dataset).is.a('object');
assume(stingray.dataset.foo).equals(undefined);
stingray.set('foo', 'bar');
assume(stingray.dataset.foo).equals('bar');
});
it('overrides existing values', function () {
stingray.set('foo', 'bar');
assume(stingray.dataset.foo).equals('bar');
stingray.set('foo', 'foo');
assume(stingray.dataset.foo).equals('foo');
});
it('chains', function () {
assume(stingray.set('foo', 'bar')).equals(stingray);
});
});
describe('#remove', function () {
it('can remove added keys', function () {
stingray.set('foo', 'bar').set('bar', 'foo');
assume(stingray.dataset.foo).equals('bar');
assume(stingray.dataset.bar).equals('foo');
stingray.remove('foo');
assume(stingray.dataset.foo).equals(undefined);
assume(stingray.dataset.bar).equals('foo');
});
it('can remove multiple keys', function () {
stingray.set('foo', 'bar').set('bar', 'foo');
assume(stingray.dataset.foo).equals('bar');
assume(stingray.dataset.bar).equals('foo');
stingray.remove('foo', 'bar');
assume(stingray.dataset.foo).equals(undefined);
assume(stingray.dataset.bar).equals(undefined);
});
it('can remove multiple keys if first arg is space/comma separated', function () {
stingray.set('foo', 'bar').set('bar', 'foo');
assume(stingray.dataset.foo).equals('bar');
assume(stingray.dataset.bar).equals('foo');
stingray.remove('foo, bar');
assume(stingray.dataset.foo).equals(undefined);
assume(stingray.dataset.bar).equals(undefined);
});
it('chains', function () {
assume(stingray.remove('foo', 'bar')).equals(stingray);
});
});
describe('#payload', function () {
it('returns an object', function () {
var data = stingray.payload();
assume(data).is.a('object');
});
it('returns an object that can be transformed in to a querystring', function () {
var data = stingray.payload()
, string = qs.stringify(data);
assume(string).is.a('string');
assume(string.length).is.below(stingray.limit);
assume(string).contains('=');
assume(string).contains('&');
//
// Before we can compare we need to transform all the values in the object
// to strings so we can have a fair chance at matching
//
for (var key in data) {
if (data.hasOwnProperty(key)) {
data[key] = data[key].toString();
}
}
assume(qs.parse(string)).deep.equals(data);
});
it('merges data from the initally supplied dataset', function () {
var u = new Stingray(server, { dataset: { foo: 'bar' }})
, data = u.payload();
assume(data.foo).equals('bar');
});
it('cannot override values that are previously set', function () {
var u = new Stingray(server, { dataset: { domain: 'bar' }})
, data = u.payload();
assume(data.domain).equals('bar');
assume(stingray.payload().domain).does.not.equal(data.domain);
});
it('setss the custom values from the .set command', function () {
stingray.set('domain', 'bar');
assume(stingray.payload().domain).equals('bar');
stingray.set('foo', 'bar');
assume(stingray.payload().foo).equals('bar');
});
it('includes performance information when available', function () {
var perf = global.performance;
global.performance = { timing: { shizzle: 'mynizzle' }, memory: { leak: 'it' }};
var data = stingray.payload();
assume(data.shizzle).equals('mynizzle');
assume(data.leak).equals('it');
global.performance = perf;
});
});
describe('#write', function () {
it('returns false when the generate url is to damn large', function () {
var u = new Stingray(server, { limit: 500 })
, url = u.server + qs.stringify(u.payload(), true);
assume(url.length).is.above(500);
assume(u.write()).is.false();
});
it('returns true if its successfully written', function () {
assume(stingray.write()).is.true();
});
});
describe('#destroy', function () {
it('returns true on first destroy', function () {
assume(stingray.destroy()).is.true();
});
it('returns false on second destroy', function () {
assume(stingray.destroy()).is.true();
assume(stingray.destroy()).is.false();
});
});
});