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