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
var request = require("request");
var qs = require("querystring");
var uuid = require("uuid");
var should = require("should");
var sinon = require("sinon");
var url = require("url");
var ua = require("../lib/index.js");
var utils = require("../lib/utils.js")
var config = require("../lib/config.js")
describe("ua", function () {
describe("#_enqueue", function () {
var send;
beforeEach(function () {
send = sinon.stub(ua.Visitor.prototype, "send").callsArg(0);
});
afterEach(function () {
send.restore()
});
it("should accept arguments (type)", function () {
var tid = "UA-XXXXX-XX";
var cid = uuid.v4();
var type = Math.random().toString()
var visitor = ua(tid, cid)._enqueue(type);
send.called.should.equal(false, "#send should not have been called without a callback");
visitor._queue.length.should.equal(1, "1 tracking call should have been enqueued");
visitor._queue[0].should.have.keys("v", "tid", "cid", "t")
visitor._queue[0].tid.should.equal(tid)
visitor._queue[0].cid.should.equal(cid)
visitor._queue[0].t.should.equal(type)
});
it("should accept arguments (type, fn)", function () {
var tid = "UA-XXXXX-XX";
var cid = uuid.v4();
var type = Math.random().toString()
var fn = sinon.spy()
var visitor = ua(tid, cid)._enqueue(type, fn);
send.calledOnce.should.equal(true, "#send should have been called once");
visitor._queue.length.should.equal(1, "1 tracking call should have been enqueued");
visitor._queue[0].should.have.keys("v", "tid", "cid", "t")
visitor._queue[0].tid.should.equal(tid)
visitor._queue[0].cid.should.equal(cid)
visitor._queue[0].t.should.equal(type)
fn.calledOnce.should.equal(true, "callback should have been called once")
});
it("should accept arguments (type, params)", function () {
var tid = "UA-XXXXX-XX";
var cid = uuid.v4();
var type = Math.random().toString()
var params = {foo: Math.random().toString()}
var visitor = ua(tid, cid)._enqueue(type, params);
send.called.should.equal(false, "#send should not have been called without a callback");
visitor._queue.length.should.equal(1, "1 tracking call should have been enqueued");
visitor._queue[0].should.have.keys("v", "tid", "cid", "t", "foo")
visitor._queue[0].tid.should.equal(tid)
visitor._queue[0].cid.should.equal(cid)
visitor._queue[0].foo.should.equal(params.foo);
});
it("should add userId if present on the Visitor", function() {
var tid = "UA-XXXXX-XX";
var cid = uuid.v4();
var type = "type";
var uid = "user1";
var params = {}
var visitor = ua(tid, cid, { uid: uid})._enqueue(type, params);
visitor._queue[0].uid.should.equal(uid);
});
it("should accept arguments (type, params, fn)", function () {
var tid = "UA-XXXXX-XX";
var cid = uuid.v4();
var type = Math.random().toString()
var params = {foo: Math.random().toString()}
var fn = sinon.spy()
var visitor = ua(tid, cid)._enqueue(type, params, fn);
send.calledOnce.should.equal(true, "#send should have been called once");
visitor._queue.length.should.equal(1, "1 tracking call should have been enqueued");
visitor._queue[0].should.have.keys("v", "tid", "cid", "t", "foo")
visitor._queue[0].tid.should.equal(tid)
visitor._queue[0].cid.should.equal(cid)
visitor._queue[0].foo.should.equal(params.foo);
fn.calledOnce.should.equal(true, "callback should have been called once")
});
it("should continue adding to the queue", function () {
var tid = "UA-XXXXX-XX";
var cid = uuid.v4();
var type = Math.random().toString()
var visitor = ua(tid, cid)
visitor._enqueue(type);
visitor._enqueue(type);
visitor._enqueue(type);
visitor._enqueue(type);
visitor._queue.length.should.equal(4, "4 tracking calls should have been enqueued");
})
});
});