UNPKG

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