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