UNPKG

22.4 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("#item", function () {
19 var _enqueue;
20
21 beforeEach(function () {
22 _enqueue = sinon.stub(ua.Visitor.prototype, "_enqueue", function (type, params, fn) {
23 if (fn) {
24 (typeof fn).should.equal('function', "#_enqueue should receive a callback")
25 fn();
26 }
27 return this;
28 });
29 });
30
31 afterEach(function () {
32 _enqueue.restore()
33 });
34
35
36 it("should be available via the #i shortcut", function () {
37 var visitor = ua()
38 visitor.i.should.equal(visitor.item)
39 });
40
41
42 it("should accept arguments (price) when chained to transaction", function () {
43 var transaction = Math.random().toString();
44 var price = Math.random();
45
46 var visitor = ua().transaction(transaction);
47
48 var result = visitor.item(price);
49
50 visitor._context = result._context;
51 result.should.eql(visitor, "should return a visitor that is identical except for the context");
52 result.should.be.instanceof(ua.Visitor);
53 result._context.should.eql(_enqueue.args[1][1], "the transaction params should be persisted as the context of the visitor clone")
54
55 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for the transaction before");
56 _enqueue.args[0][0].should.equal("transaction");
57 _enqueue.args[1][0].should.equal("item");
58 _enqueue.args[1][1].should.have.keys("ti", "ip")
59 _enqueue.args[1][1].ti.should.equal(transaction);
60 _enqueue.args[1][1].ip.should.equal(price);
61 });
62
63
64 it("should accept arguments (price, fn) when chained to transaction", function () {
65 var transaction = Math.random().toString();
66 var price = Math.random();
67 var fn = sinon.spy()
68
69 var visitor = ua().transaction(transaction);
70
71 var result = visitor.item(price, fn);
72
73 visitor._context = result._context;
74 result.should.eql(visitor, "should return a visitor that is identical except for the context");
75 result.should.be.instanceof(ua.Visitor);
76 result._context.should.eql(_enqueue.args[1][1], "the transaction params should be persisted as the context of the visitor clone")
77
78 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for the transaction before");
79 _enqueue.args[0][0].should.equal("transaction");
80 _enqueue.args[1][0].should.equal("item");
81 _enqueue.args[1][1].should.have.keys("ti", "ip")
82 _enqueue.args[1][1].ti.should.equal(transaction);
83 _enqueue.args[1][1].ip.should.equal(price);
84
85 fn.calledOnce.should.equal(true, "callback should have been called once")
86 });
87
88
89 it("should accept arguments (price, quantity) when chained to transaction", function () {
90 var transaction = Math.random().toString();
91 var price = Math.random();
92 var quantity = Math.random();
93
94 var visitor = ua().transaction(transaction);
95
96 var result = visitor.item(price, quantity);
97
98 visitor._context = result._context;
99 result.should.eql(visitor, "should return a visitor that is identical except for the context");
100 result.should.be.instanceof(ua.Visitor);
101 result._context.should.eql(_enqueue.args[1][1], "the transaction params should be persisted as the context of the visitor clone")
102
103 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for the transaction before");
104 _enqueue.args[0][0].should.equal("transaction");
105 _enqueue.args[1][0].should.equal("item");
106 _enqueue.args[1][1].should.have.keys("ti", "ip", "iq")
107 _enqueue.args[1][1].ti.should.equal(transaction);
108 _enqueue.args[1][1].ip.should.equal(price);
109 _enqueue.args[1][1].iq.should.equal(quantity);
110 });
111
112
113 it("should accept arguments (price, quantity, fn) when chained to transaction", function () {
114 var transaction = Math.random().toString();
115 var price = Math.random();
116 var quantity = Math.random();
117 var fn = sinon.spy()
118
119 var visitor = ua().transaction(transaction);
120
121 var result = visitor.item(price, quantity, fn);
122
123 visitor._context = result._context;
124 result.should.eql(visitor, "should return a visitor that is identical except for the context");
125 result.should.be.instanceof(ua.Visitor);
126 result._context.should.eql(_enqueue.args[1][1], "the transaction params should be persisted as the context of the visitor clone")
127
128 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for the transaction before");
129 _enqueue.args[0][0].should.equal("transaction");
130 _enqueue.args[1][0].should.equal("item");
131 _enqueue.args[1][1].should.have.keys("ti", "ip", "iq")
132 _enqueue.args[1][1].ti.should.equal(transaction);
133 _enqueue.args[1][1].ip.should.equal(price);
134 _enqueue.args[1][1].iq.should.equal(quantity);
135
136 fn.calledOnce.should.equal(true, "callback should have been called once")
137 });
138
139
140 it("should accept arguments (price, quantity, sku) when chained to transaction", function () {
141 var transaction = Math.random().toString();
142 var price = Math.random();
143 var quantity = Math.random();
144 var sku = Math.random().toString();
145
146 var visitor = ua().transaction(transaction);
147
148 var result = visitor.item(price, quantity, sku);
149
150 visitor._context = result._context;
151 result.should.eql(visitor, "should return a visitor that is identical except for the context");
152 result.should.be.instanceof(ua.Visitor);
153 result._context.should.eql(_enqueue.args[1][1], "the transaction params should be persisted as the context of the visitor clone")
154
155 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for the transaction before");
156 _enqueue.args[0][0].should.equal("transaction");
157 _enqueue.args[1][0].should.equal("item");
158 _enqueue.args[1][1].should.have.keys("ti", "ip", "iq", "ic")
159 _enqueue.args[1][1].ti.should.equal(transaction);
160 _enqueue.args[1][1].ip.should.equal(price);
161 _enqueue.args[1][1].iq.should.equal(quantity);
162 _enqueue.args[1][1].ic.should.equal(sku);
163 });
164
165
166 it("should accept arguments (price, quantity, sku, fn) when chained to transaction", function () {
167 var transaction = Math.random().toString();
168 var price = Math.random();
169 var quantity = Math.random();
170 var sku = Math.random().toString();
171 var fn = sinon.spy()
172
173 var visitor = ua().transaction(transaction);
174
175 var result = visitor.item(price, quantity, sku, fn);
176
177 visitor._context = result._context;
178 result.should.eql(visitor, "should return a visitor that is identical except for the context");
179 result.should.be.instanceof(ua.Visitor);
180 result._context.should.eql(_enqueue.args[1][1], "the transaction params should be persisted as the context of the visitor clone")
181
182 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for the transaction before");
183 _enqueue.args[0][0].should.equal("transaction");
184 _enqueue.args[1][0].should.equal("item");
185 _enqueue.args[1][1].should.have.keys("ti", "ip", "iq", "ic")
186 _enqueue.args[1][1].ti.should.equal(transaction);
187 _enqueue.args[1][1].ip.should.equal(price);
188 _enqueue.args[1][1].iq.should.equal(quantity);
189 _enqueue.args[1][1].ic.should.equal(sku);
190
191 fn.calledOnce.should.equal(true, "callback should have been called once")
192 });
193
194
195 it("should accept arguments (price, quantity, sku, name) when chained to transaction", function () {
196 var transaction = Math.random().toString();
197 var price = Math.random();
198 var quantity = Math.random();
199 var sku = Math.random().toString();
200 var name = Math.random().toString();
201
202 var visitor = ua().transaction(transaction);
203
204 var result = visitor.item(price, quantity, sku, name);
205
206 visitor._context = result._context;
207 result.should.eql(visitor, "should return a visitor that is identical except for the context");
208 result.should.be.instanceof(ua.Visitor);
209 result._context.should.eql(_enqueue.args[1][1], "the transaction params should be persisted as the context of the visitor clone")
210
211 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for the transaction before");
212 _enqueue.args[0][0].should.equal("transaction");
213 _enqueue.args[1][0].should.equal("item");
214 _enqueue.args[1][1].should.have.keys("ti", "ip", "iq", "ic", "in")
215 _enqueue.args[1][1].ti.should.equal(transaction);
216 _enqueue.args[1][1].ip.should.equal(price);
217 _enqueue.args[1][1].iq.should.equal(quantity);
218 _enqueue.args[1][1].ic.should.equal(sku);
219 _enqueue.args[1][1].in.should.equal(name);
220 });
221
222
223 it("should accept arguments (price, quantity, sku, name, fn) when chained to transaction", function () {
224 var transaction = Math.random().toString();
225 var price = Math.random();
226 var quantity = Math.random();
227 var sku = Math.random().toString();
228 var name = Math.random().toString();
229 var fn = sinon.spy()
230
231 var visitor = ua().transaction(transaction);
232
233 var result = visitor.item(price, quantity, sku, name, fn);
234
235 visitor._context = result._context;
236 result.should.eql(visitor, "should return a visitor that is identical except for the context");
237 result.should.be.instanceof(ua.Visitor);
238 result._context.should.eql(_enqueue.args[1][1], "the transaction params should be persisted as the context of the visitor clone")
239
240 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for the transaction before");
241 _enqueue.args[0][0].should.equal("transaction");
242 _enqueue.args[1][0].should.equal("item");
243 _enqueue.args[1][1].should.have.keys("ti", "ip", "iq", "ic", "in")
244 _enqueue.args[1][1].ti.should.equal(transaction);
245 _enqueue.args[1][1].ip.should.equal(price);
246 _enqueue.args[1][1].iq.should.equal(quantity);
247 _enqueue.args[1][1].ic.should.equal(sku);
248 _enqueue.args[1][1].in.should.equal(name);
249
250 fn.calledOnce.should.equal(true, "callback should have been called once")
251 });
252
253
254 it("should accept arguments (price, quantity, sku, name, variation) when chained to transaction", function () {
255 var transaction = Math.random().toString();
256 var price = Math.random();
257 var quantity = Math.random();
258 var sku = Math.random().toString();
259 var name = Math.random().toString();
260 var variation = Math.random().toString();
261
262 var visitor = ua().transaction(transaction);
263
264 var result = visitor.item(price, quantity, sku, name, variation);
265
266 visitor._context = result._context;
267 result.should.eql(visitor, "should return a visitor that is identical except for the context");
268 result.should.be.instanceof(ua.Visitor);
269 result._context.should.eql(_enqueue.args[1][1], "the transaction params should be persisted as the context of the visitor clone")
270
271 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for the transaction before");
272 _enqueue.args[0][0].should.equal("transaction");
273 _enqueue.args[1][0].should.equal("item");
274 _enqueue.args[1][1].should.have.keys("ti", "ip", "iq", "ic", "in", "iv")
275 _enqueue.args[1][1].ti.should.equal(transaction);
276 _enqueue.args[1][1].ip.should.equal(price);
277 _enqueue.args[1][1].iq.should.equal(quantity);
278 _enqueue.args[1][1].ic.should.equal(sku);
279 _enqueue.args[1][1].in.should.equal(name);
280 _enqueue.args[1][1].iv.should.equal(variation);
281 });
282
283
284 it("should accept arguments (price, quantity, sku, name, variation, fn) when chained to transaction", function () {
285 var transaction = Math.random().toString();
286 var price = Math.random();
287 var quantity = Math.random();
288 var sku = Math.random().toString();
289 var name = Math.random().toString();
290 var variation = Math.random().toString();
291 var fn = sinon.spy()
292
293 var visitor = ua().transaction(transaction);
294
295 var result = visitor.item(price, quantity, sku, name, variation, fn);
296
297 visitor._context = result._context;
298 result.should.eql(visitor, "should return a visitor that is identical except for the context");
299 result.should.be.instanceof(ua.Visitor);
300 result._context.should.eql(_enqueue.args[1][1], "the transaction params should be persisted as the context of the visitor clone")
301
302 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for the transaction before");
303 _enqueue.args[0][0].should.equal("transaction");
304 _enqueue.args[1][0].should.equal("item");
305 _enqueue.args[1][1].should.have.keys("ti", "ip", "iq", "ic", "in", "iv")
306 _enqueue.args[1][1].ti.should.equal(transaction);
307 _enqueue.args[1][1].ip.should.equal(price);
308 _enqueue.args[1][1].iq.should.equal(quantity);
309 _enqueue.args[1][1].ic.should.equal(sku);
310 _enqueue.args[1][1].in.should.equal(name);
311 _enqueue.args[1][1].iv.should.equal(variation);
312
313 fn.calledOnce.should.equal(true, "callback should have been called once")
314 });
315
316
317 it("should accept arguments (price, quantity, sku, name, variation, params) when chained to transaction", function () {
318 var transaction = Math.random().toString();
319 var price = Math.random();
320 var quantity = Math.random();
321 var sku = Math.random().toString();
322 var name = Math.random().toString();
323 var variation = Math.random().toString();
324 var params = {foo: Math.random().toString()};
325
326 var visitor = ua().transaction(transaction);
327
328 var result = visitor.item(price, quantity, sku, name, variation, params);
329
330 visitor._context = result._context;
331 result.should.eql(visitor, "should return a visitor that is identical except for the context");
332 result.should.be.instanceof(ua.Visitor);
333 result._context.should.eql(_enqueue.args[1][1], "the transaction params should be persisted as the context of the visitor clone")
334
335 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for the transaction before");
336 _enqueue.args[0][0].should.equal("transaction");
337 _enqueue.args[1][0].should.equal("item");
338 _enqueue.args[1][1].should.have.keys("ti", "ip", "iq", "ic", "in", "iv", "foo")
339 _enqueue.args[1][1].ti.should.equal(transaction);
340 _enqueue.args[1][1].ip.should.equal(price);
341 _enqueue.args[1][1].iq.should.equal(quantity);
342 _enqueue.args[1][1].ic.should.equal(sku);
343 _enqueue.args[1][1].in.should.equal(name);
344 _enqueue.args[1][1].iv.should.equal(variation);
345 _enqueue.args[1][1].foo.should.equal(params.foo);
346 });
347
348
349 it("should accept arguments (price, quantity, sku, name, variation, params, fn) when chained to transaction", function () {
350 var transaction = Math.random().toString();
351 var price = Math.random();
352 var quantity = Math.random();
353 var sku = Math.random().toString();
354 var name = Math.random().toString();
355 var variation = Math.random().toString();
356 var params = {foo: Math.random().toString()};
357 var fn = sinon.spy()
358
359 var visitor = ua().transaction(transaction);
360
361 var result = visitor.item(price, quantity, sku, name, variation, params, fn);
362
363 visitor._context = result._context;
364 result.should.eql(visitor, "should return a visitor that is identical except for the context");
365 result.should.be.instanceof(ua.Visitor);
366 result._context.should.eql(_enqueue.args[1][1], "the transaction params should be persisted as the context of the visitor clone")
367
368 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for the transaction before");
369 _enqueue.args[0][0].should.equal("transaction");
370 _enqueue.args[1][0].should.equal("item");
371 _enqueue.args[1][1].should.have.keys("ti", "ip", "iq", "ic", "in", "iv", "foo")
372 _enqueue.args[1][1].ti.should.equal(transaction);
373 _enqueue.args[1][1].ip.should.equal(price);
374 _enqueue.args[1][1].iq.should.equal(quantity);
375 _enqueue.args[1][1].ic.should.equal(sku);
376 _enqueue.args[1][1].in.should.equal(name);
377 _enqueue.args[1][1].iv.should.equal(variation);
378 _enqueue.args[1][1].foo.should.equal(params.foo);
379
380 fn.calledOnce.should.equal(true, "callback should have been called once")
381 });
382
383
384 it("should accept arguments (price, quantity, sku, name, variation, params, fn) when params include transaction", function () {
385 var price = Math.random();
386 var quantity = Math.random();
387 var sku = Math.random().toString();
388 var name = Math.random().toString();
389 var variation = Math.random().toString();
390 var params = {ti: Math.random()};
391 var fn = sinon.spy()
392
393 var visitor = ua();
394
395 var result = visitor.item(price, quantity, sku, name, variation, params, fn);
396
397 visitor._context = result._context;
398 result.should.eql(visitor, "should return a visitor that is identical except for the context");
399 result.should.be.instanceof(ua.Visitor);
400 result._context.should.eql(_enqueue.args[0][1], "the transaction params should be persisted as the context of the visitor clone")
401
402 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
403 _enqueue.args[0][0].should.equal("item");
404 _enqueue.args[0][1].should.have.keys("ti", "ip", "iq", "ic", "in", "iv")
405 _enqueue.args[0][1].ip.should.equal(price);
406 _enqueue.args[0][1].iq.should.equal(quantity);
407 _enqueue.args[0][1].ic.should.equal(sku);
408 _enqueue.args[0][1].in.should.equal(name);
409 _enqueue.args[0][1].iv.should.equal(variation);
410 _enqueue.args[0][1].ti.should.equal(params.ti);
411
412 fn.calledOnce.should.equal(true, "callback should have been called once")
413 });
414
415
416 it("should accept arguments (params, fn) when params include transaction", function () {
417 var params = {
418 ip: Math.random(),
419 iq: Math.random(),
420 ic: Math.random().toString(),
421 in: Math.random().toString(),
422 iv: Math.random().toString(),
423 ti: Math.random()
424 };
425 var fn = sinon.spy()
426
427 var visitor = ua();
428
429 var result = visitor.item(params, fn);
430
431 visitor._context = result._context;
432 result.should.eql(visitor, "should return a visitor that is identical except for the context");
433 result.should.be.instanceof(ua.Visitor);
434 result._context.should.eql(_enqueue.args[0][1], "the transaction params should be persisted as the context of the visitor clone")
435
436 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
437 _enqueue.args[0][0].should.equal("item");
438 _enqueue.args[0][1].should.have.keys("ti", "ip", "iq", "ic", "in", "iv")
439 _enqueue.args[0][1].ip.should.equal(params.ip);
440 _enqueue.args[0][1].iq.should.equal(params.iq);
441 _enqueue.args[0][1].ic.should.equal(params.ic);
442 _enqueue.args[0][1].in.should.equal(params.in);
443 _enqueue.args[0][1].iv.should.equal(params.iv);
444 _enqueue.args[0][1].ti.should.equal(params.ti);
445
446 fn.calledOnce.should.equal(true, "callback should have been called once")
447 });
448
449
450 it("should accept arguments (params, fn) when chained to a transaction", function () {
451 var transaction = Math.random().toString();
452 var params = {
453 ip: Math.random(),
454 iq: Math.random(),
455 ic: Math.random().toString(),
456 in: Math.random().toString(),
457 iv: Math.random().toString()
458 };
459 var fn = sinon.spy()
460
461 var visitor = ua().transaction(transaction);
462
463 var result = visitor.item(params, fn);
464
465 visitor._context = result._context;
466 result.should.eql(visitor, "should return a visitor that is identical except for the context");
467 result.should.be.instanceof(ua.Visitor);
468 result._context.should.eql(_enqueue.args[1][1], "the transaction params should be persisted as the context of the visitor clone")
469
470 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for the transaction");
471 _enqueue.args[1][0].should.equal("item");
472 _enqueue.args[1][1].should.have.keys("ti", "ip", "iq", "ic", "in", "iv")
473 _enqueue.args[1][1].ip.should.equal(params.ip);
474 _enqueue.args[1][1].iq.should.equal(params.iq);
475 _enqueue.args[1][1].ic.should.equal(params.ic);
476 _enqueue.args[1][1].in.should.equal(params.in);
477 _enqueue.args[1][1].iv.should.equal(params.iv);
478 _enqueue.args[1][1].ti.should.equal(transaction);
479
480 fn.calledOnce.should.equal(true, "callback should have been called once")
481 });
482
483
484 it("should allow daisy-chaining to re-use arguments", function () {
485 var transaction = Math.random().toString();
486 var price = Math.random();
487 var quantity = Math.random();
488 var sku = Math.random().toString();
489 var name = Math.random().toString();
490 var name2 = Math.random().toString();
491 var variation = Math.random().toString();
492 var fn = sinon.spy()
493
494 var visitor = ua().transaction(transaction);
495
496 var result = visitor.item(price, quantity, sku, name, variation).item({in: name2}, fn);
497
498 _enqueue.calledThrice.should.equal(true, "#_enqueue should have been called thrice, once for the transaction before");
499 _enqueue.args[0][0].should.equal("transaction");
500
501 _enqueue.args[1][0].should.equal(_enqueue.args[2][0]);
502 _enqueue.args[1][1].ti.should.equal(_enqueue.args[2][1].ti, "ti should be equal on both");
503 _enqueue.args[1][1].ip.should.equal(_enqueue.args[2][1].ip, "ip should be equal on both");
504 _enqueue.args[1][1].iq.should.equal(_enqueue.args[2][1].iq, "iq should be equal on both");
505 _enqueue.args[1][1].ic.should.equal(_enqueue.args[2][1].ic, "ic should be equal on both");
506 _enqueue.args[1][1].iv.should.equal(_enqueue.args[2][1].iv, "iv should be equal on both");
507 _enqueue.args[2][1].in.should.equal(name2, "name should have changed on second item");
508
509 fn.calledOnce.should.equal(true, "callback should have been called once")
510 });
511
512
513 it("should fail without transaction ID", function () {
514 var params = {
515 ip: Math.random(),
516 iq: Math.random(),
517 ic: Math.random().toString(),
518 in: Math.random().toString(),
519 iv: Math.random().toString()
520 };
521 var fn = sinon.spy()
522
523 var visitor = ua();
524
525 var result = visitor.item(params, fn);
526
527 visitor._context = result._context;
528 result.should.eql(visitor, "should return a visitor that is identical except for the context");
529
530 result.should.be.instanceof(ua.Visitor);
531 result._context.should.eql({}, "the transaction params should not be persisted")
532
533 _enqueue.called.should.equal(false, "#_enqueue should have not been called once");
534 fn.calledOnce.should.equal(true, "callback should have been called once");
535 fn.args[0][0].should.be.instanceof(Error);
536 fn.thisValues[0].should.equal(visitor);
537 });
538
539 });
540
541});
542
543
544
545
546
547
548
549
550
551