UNPKG

3.87 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("#_enqueue", function () {
18
19 var send;
20
21 beforeEach(function () {
22 send = sinon.stub(ua.Visitor.prototype, "send").callsArg(0);
23 });
24
25 afterEach(function () {
26 send.restore()
27 });
28
29 it("should accept arguments (type)", function () {
30 var tid = "UA-XXXXX-XX";
31 var cid = uuid.v4();
32 var type = Math.random().toString()
33
34 var visitor = ua(tid, cid)._enqueue(type);
35
36 send.called.should.equal(false, "#send should not have been called without a callback");
37
38 visitor._queue.length.should.equal(1, "1 tracking call should have been enqueued");
39
40 visitor._queue[0].should.have.keys("v", "tid", "cid", "t")
41 visitor._queue[0].tid.should.equal(tid)
42 visitor._queue[0].cid.should.equal(cid)
43 visitor._queue[0].t.should.equal(type)
44 });
45
46 it("should accept arguments (type, fn)", function () {
47 var tid = "UA-XXXXX-XX";
48 var cid = uuid.v4();
49 var type = Math.random().toString()
50 var fn = sinon.spy()
51
52 var visitor = ua(tid, cid)._enqueue(type, fn);
53
54 send.calledOnce.should.equal(true, "#send should have been called once");
55
56 visitor._queue.length.should.equal(1, "1 tracking call should have been enqueued");
57
58 visitor._queue[0].should.have.keys("v", "tid", "cid", "t")
59 visitor._queue[0].tid.should.equal(tid)
60 visitor._queue[0].cid.should.equal(cid)
61 visitor._queue[0].t.should.equal(type)
62
63 fn.calledOnce.should.equal(true, "callback should have been called once")
64 });
65
66 it("should accept arguments (type, params)", function () {
67 var tid = "UA-XXXXX-XX";
68 var cid = uuid.v4();
69 var type = Math.random().toString()
70 var params = {foo: Math.random().toString()}
71
72 var visitor = ua(tid, cid)._enqueue(type, params);
73
74 send.called.should.equal(false, "#send should not have been called without a callback");
75
76 visitor._queue.length.should.equal(1, "1 tracking call should have been enqueued");
77
78 visitor._queue[0].should.have.keys("v", "tid", "cid", "t", "foo")
79 visitor._queue[0].tid.should.equal(tid)
80 visitor._queue[0].cid.should.equal(cid)
81 visitor._queue[0].foo.should.equal(params.foo);
82 });
83
84 it("should add userId if present on the Visitor", function() {
85 var tid = "UA-XXXXX-XX";
86 var cid = uuid.v4();
87 var type = "type";
88 var uid = "user1";
89 var params = {}
90
91 var visitor = ua(tid, cid, { uid: uid})._enqueue(type, params);
92
93 visitor._queue[0].uid.should.equal(uid);
94 });
95
96 it("should accept arguments (type, params, fn)", function () {
97 var tid = "UA-XXXXX-XX";
98 var cid = uuid.v4();
99 var type = Math.random().toString()
100 var params = {foo: Math.random().toString()}
101 var fn = sinon.spy()
102
103 var visitor = ua(tid, cid)._enqueue(type, params, fn);
104
105 send.calledOnce.should.equal(true, "#send should have been called once");
106
107 visitor._queue.length.should.equal(1, "1 tracking call should have been enqueued");
108
109 visitor._queue[0].should.have.keys("v", "tid", "cid", "t", "foo")
110 visitor._queue[0].tid.should.equal(tid)
111 visitor._queue[0].cid.should.equal(cid)
112 visitor._queue[0].foo.should.equal(params.foo);
113
114 fn.calledOnce.should.equal(true, "callback should have been called once")
115 });
116
117 it("should continue adding to the queue", function () {
118 var tid = "UA-XXXXX-XX";
119 var cid = uuid.v4();
120 var type = Math.random().toString()
121
122 var visitor = ua(tid, cid)
123
124 visitor._enqueue(type);
125 visitor._enqueue(type);
126 visitor._enqueue(type);
127 visitor._enqueue(type);
128
129 visitor._queue.length.should.equal(4, "4 tracking calls should have been enqueued");
130 })
131
132 });
133
134});
135
136
137
138
139
140
141
142
143
144