UNPKG

13 kBJavaScriptView Raw
1
2var _ = require("underscore");
3var request = require("request");
4var qs = require("querystring");
5var uuid = require("uuid");
6var should = require("should");
7var sinon = require("sinon");
8var url = require("url");
9
10var ua = require("../lib/index.js");
11var utils = require("../lib/utils.js")
12var config = require("../lib/config.js")
13
14
15describe("ua", function () {
16
17
18 describe("#timing", function () {
19 var _enqueue;
20
21 beforeEach(function () {
22 _enqueue = sinon.stub(ua.Visitor.prototype, "_enqueue", function () {
23 if (arguments.length === 3 && typeof arguments[2] === 'function') {
24 arguments[2]();
25 }
26 return this;
27 });
28 });
29
30 afterEach(function () {
31 _enqueue.restore()
32 });
33
34
35 it("should accept arguments (category)", function () {
36 var category = Math.random().toString();
37
38 var visitor = ua()
39
40 var result = visitor.timing(category);
41
42 visitor._context = result._context;
43 result.should.eql(visitor, "should return a visitor that is identical except for the context");
44
45 result.should.be.instanceof(ua.Visitor);
46 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
47
48 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
49 _enqueue.args[0][0].should.equal("timing");
50 _enqueue.args[0][1].should.have.keys("utc")
51 _enqueue.args[0][1].utc.should.equal(category);
52 });
53
54
55 it("should accept arguments (category, fn)", function () {
56 var category = Math.random().toString();
57 var fn = sinon.spy();
58
59 var visitor = ua()
60
61 var result = visitor.timing(category, fn);
62
63 visitor._context = result._context;
64 result.should.eql(visitor, "should return a visitor that is identical except for the context");
65
66 result.should.be.instanceof(ua.Visitor);
67 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
68
69 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
70 _enqueue.args[0][0].should.equal("timing");
71 _enqueue.args[0][1].should.have.keys("utc")
72 _enqueue.args[0][1].utc.should.equal(category);
73
74 fn.calledOnce.should.equal(true, "callback should have been called once")
75 });
76
77
78 it("should accept arguments (category, variable)", function () {
79 var category = Math.random().toString();
80 var variable = Math.random().toString();
81
82 var visitor = ua()
83
84 var result = visitor.timing(category, variable);
85
86 visitor._context = result._context;
87 result.should.eql(visitor, "should return a visitor that is identical except for the context");
88
89 result.should.be.instanceof(ua.Visitor);
90 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
91
92 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
93 _enqueue.args[0][0].should.equal("timing");
94 _enqueue.args[0][1].should.have.keys("utc", "utv")
95 _enqueue.args[0][1].utc.should.equal(category);
96 _enqueue.args[0][1].utv.should.equal(variable);
97 });
98
99
100 it("should accept arguments (category, variable, fn)", function () {
101 var category = Math.random().toString();
102 var variable = Math.random().toString();
103 var fn = sinon.spy();
104
105 var visitor = ua()
106
107 var result = visitor.timing(category, variable, fn);
108
109 visitor._context = result._context;
110 result.should.eql(visitor, "should return a visitor that is identical except for the context");
111
112 result.should.be.instanceof(ua.Visitor);
113 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
114
115 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
116 _enqueue.args[0][0].should.equal("timing");
117 _enqueue.args[0][1].should.have.keys("utc", "utv")
118 _enqueue.args[0][1].utc.should.equal(category);
119 _enqueue.args[0][1].utv.should.equal(variable);
120
121 fn.calledOnce.should.equal(true, "callback should have been called once")
122 });
123
124
125 it("should accept arguments (category, variable, time)", function () {
126 var category = Math.random().toString();
127 var variable = Math.random().toString();
128 var time = Math.random();
129
130 var visitor = ua()
131
132 var result = visitor.timing(category, variable, time);
133
134 visitor._context = result._context;
135 result.should.eql(visitor, "should return a visitor that is identical except for the context");
136
137 result.should.be.instanceof(ua.Visitor);
138 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
139
140 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
141 _enqueue.args[0][0].should.equal("timing");
142 _enqueue.args[0][1].should.have.keys("utc", "utv", "utt")
143 _enqueue.args[0][1].utc.should.equal(category);
144 _enqueue.args[0][1].utv.should.equal(variable);
145 _enqueue.args[0][1].utt.should.equal(time);
146 });
147
148
149 it("should accept arguments (category, variable, time)", function () {
150 var category = Math.random().toString();
151 var variable = Math.random().toString();
152 var time = Math.random();
153 var fn = sinon.spy()
154
155 var visitor = ua()
156
157 var result = visitor.timing(category, variable, time, fn);
158
159 visitor._context = result._context;
160 result.should.eql(visitor, "should return a visitor that is identical except for the context");
161
162 result.should.be.instanceof(ua.Visitor);
163 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
164
165 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
166 _enqueue.args[0][0].should.equal("timing");
167 _enqueue.args[0][1].should.have.keys("utc", "utv", "utt")
168 _enqueue.args[0][1].utc.should.equal(category);
169 _enqueue.args[0][1].utv.should.equal(variable);
170 _enqueue.args[0][1].utt.should.equal(time);
171
172 fn.calledOnce.should.equal(true, "callback should have been called once")
173 });
174
175
176 it("should accept arguments (category, variable, time, label)", function () {
177 var category = Math.random().toString();
178 var variable = Math.random().toString();
179 var time = Math.random();
180 var label = Math.random().toString();
181
182 var visitor = ua()
183
184 var result = visitor.timing(category, variable, time, label);
185
186 visitor._context = result._context;
187 result.should.eql(visitor, "should return a visitor that is identical except for the context");
188
189 result.should.be.instanceof(ua.Visitor);
190 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
191
192 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
193 _enqueue.args[0][0].should.equal("timing");
194 _enqueue.args[0][1].should.have.keys("utc", "utv", "utt", "utl")
195 _enqueue.args[0][1].utc.should.equal(category);
196 _enqueue.args[0][1].utv.should.equal(variable);
197 _enqueue.args[0][1].utt.should.equal(time);
198 _enqueue.args[0][1].utl.should.equal(label);
199 });
200
201
202 it("should accept arguments (category, variable, time, label, fn)", function () {
203 var category = Math.random().toString();
204 var variable = Math.random().toString();
205 var time = Math.random();
206 var label = Math.random().toString();
207 var fn = sinon.spy()
208
209 var visitor = ua()
210
211 var result = visitor.timing(category, variable, time, label, fn);
212
213 visitor._context = result._context;
214 result.should.eql(visitor, "should return a visitor that is identical except for the context");
215
216 result.should.be.instanceof(ua.Visitor);
217 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
218
219 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
220 _enqueue.args[0][0].should.equal("timing");
221 _enqueue.args[0][1].should.have.keys("utc", "utv", "utt", "utl")
222 _enqueue.args[0][1].utc.should.equal(category);
223 _enqueue.args[0][1].utv.should.equal(variable);
224 _enqueue.args[0][1].utt.should.equal(time);
225 _enqueue.args[0][1].utl.should.equal(label);
226
227 fn.calledOnce.should.equal(true, "callback should have been called once")
228 });
229
230
231 it("should accept arguments (category, variable, time, label, params)", function () {
232 var category = Math.random().toString();
233 var variable = Math.random().toString();
234 var time = Math.random();
235 var label = Math.random().toString();
236 var params = {p: Math.random().toString()}
237
238 var visitor = ua()
239
240 var result = visitor.timing(category, variable, time, label, params);
241
242 visitor._context = result._context;
243 result.should.eql(visitor, "should return a visitor that is identical except for the context");
244
245 result.should.be.instanceof(ua.Visitor);
246 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
247
248 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
249 _enqueue.args[0][0].should.equal("timing");
250 _enqueue.args[0][1].should.have.keys("utc", "utv", "utt", "utl", "p")
251 _enqueue.args[0][1].utc.should.equal(category);
252 _enqueue.args[0][1].utv.should.equal(variable);
253 _enqueue.args[0][1].utt.should.equal(time);
254 _enqueue.args[0][1].utl.should.equal(label);
255 _enqueue.args[0][1].p.should.equal(params.p);
256 });
257
258
259 it("should accept arguments (category, variable, time, label, params, fn)", function () {
260 var category = Math.random().toString();
261 var variable = Math.random().toString();
262 var time = Math.random();
263 var label = Math.random().toString();
264 var params = {p: Math.random().toString()}
265 var fn = sinon.spy()
266
267 var visitor = ua()
268
269 var result = visitor.timing(category, variable, time, label, params, fn);
270
271 visitor._context = result._context;
272 result.should.eql(visitor, "should return a visitor that is identical except for the context");
273
274 result.should.be.instanceof(ua.Visitor);
275 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
276
277 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
278 _enqueue.args[0][0].should.equal("timing");
279 _enqueue.args[0][1].should.have.keys("utc", "utv", "utt", "utl", "p")
280 _enqueue.args[0][1].utc.should.equal(category);
281 _enqueue.args[0][1].utv.should.equal(variable);
282 _enqueue.args[0][1].utt.should.equal(time);
283 _enqueue.args[0][1].utl.should.equal(label);
284 _enqueue.args[0][1].p.should.equal(params.p);
285
286 fn.calledOnce.should.equal(true, "callback should have been called once")
287 });
288
289
290 it("should accept arguments (category, variable, time, label, params)", function () {
291 var params = {
292 utc: Math.random().toString(),
293 utv: Math.random().toString(),
294 utt: Math.random(),
295 utl: Math.random().toString(),
296 p: Math.random().toString()
297 }
298
299 var visitor = ua()
300
301 var result = visitor.timing(params);
302
303 visitor._context = result._context;
304 result.should.eql(visitor, "should return a visitor that is identical except for the context");
305
306 result.should.be.instanceof(ua.Visitor);
307 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
308
309 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
310 _enqueue.args[0][0].should.equal("timing");
311 _enqueue.args[0][1].should.have.keys("utc", "utv", "utt", "utl", "p")
312 _enqueue.args[0][1].utc.should.equal(params.utc);
313 _enqueue.args[0][1].utv.should.equal(params.utv);
314 _enqueue.args[0][1].utt.should.equal(params.utt);
315 _enqueue.args[0][1].utl.should.equal(params.utl);
316 _enqueue.args[0][1].p.should.equal(params.p);
317 });
318
319
320 it("should accept arguments (category, variable, time, label, params)", function () {
321 var params = {
322 utc: Math.random().toString(),
323 utv: Math.random().toString(),
324 utt: Math.random(),
325 utl: Math.random().toString(),
326 p: Math.random().toString()
327 }
328 var fn = sinon.spy()
329
330 var visitor = ua()
331
332 var result = visitor.timing(params, fn);
333
334 visitor._context = result._context;
335 result.should.eql(visitor, "should return a visitor that is identical except for the context");
336
337 result.should.be.instanceof(ua.Visitor);
338 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
339
340 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
341 _enqueue.args[0][0].should.equal("timing");
342 _enqueue.args[0][1].should.have.keys("utc", "utv", "utt", "utl", "p")
343 _enqueue.args[0][1].utc.should.equal(params.utc);
344 _enqueue.args[0][1].utv.should.equal(params.utv);
345 _enqueue.args[0][1].utt.should.equal(params.utt);
346 _enqueue.args[0][1].utl.should.equal(params.utl);
347 _enqueue.args[0][1].p.should.equal(params.p);
348
349 fn.calledOnce.should.equal(true, "callback should have been called once")
350 });
351
352 });
353
354});
355
356
357
358
359
360
361
362
363
364