UNPKG

8.99 kBJavaScriptView Raw
1
2var _ = require("underscore");
3var request = require("request");
4var qs = require("querystring");
5var uuid = require("uuid");
6var should = require("should");
7var sinon = require("sinon");
8var url = require("url");
9
10var ua = require("../lib/index.js");
11var utils = require("../lib/utils.js")
12var config = require("../lib/config.js")
13
14
15describe("ua", function () {
16
17
18
19 describe("#pageview", function () {
20 var _enqueue;
21
22 beforeEach(function () {
23 _enqueue = sinon.stub(ua.Visitor.prototype, "_enqueue", function () {
24 if (arguments.length === 3 && typeof arguments[2] === 'function') {
25 arguments[2]();
26 }
27 return this;
28 });
29 });
30
31 afterEach(function () {
32 _enqueue.restore()
33 });
34
35
36 it("should be available via the #pv shortcut", function () {
37 var visitor = ua()
38 visitor.pv.should.equal(visitor.pageview)
39 });
40
41
42 it("should accept arguments (path)", function () {
43 var path = "/" + Math.random()
44
45 var visitor = ua("UA-XXXXX-XX")
46
47 var result = visitor.pageview(path)
48
49 visitor._context = result._context;
50 result.should.eql(visitor, "should return a visitor that is identical except for the context");
51
52 result.should.be.instanceof(ua.Visitor);
53 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
54
55 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
56 _enqueue.args[0][0].should.equal("pageview");
57 _enqueue.args[0][1].should.have.keys("dp")
58 _enqueue.args[0][1].dp.should.equal(path);
59 });
60
61
62 it("should accept arguments (path, fn)", function () {
63 var path = "/" + Math.random();
64 var fn = sinon.spy();
65
66 var visitor = ua("UA-XXXXX-XX")
67
68 var result = visitor.pageview(path, fn)
69
70 visitor._context = result._context;
71 result.should.eql(visitor, "should return a visitor that is identical except for the context");
72
73 result.should.be.instanceof(ua.Visitor);
74 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
75
76 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
77 _enqueue.args[0][0].should.equal("pageview");
78 _enqueue.args[0][1].should.have.keys("dp")
79 _enqueue.args[0][1].dp.should.equal(path);
80
81 fn.calledOnce.should.equal(true, "callback should have been called once");
82 });
83
84
85 it("should accept arguments (params)", function () {
86 var params = {
87 dp: "/" + Math.random()
88 };
89
90 var visitor = ua("UA-XXXXX-XX")
91
92 var result = visitor.pageview(params)
93
94 visitor._context = result._context;
95 result.should.eql(visitor, "should return a visitor that is identical except for the context");
96
97 result.should.be.instanceof(ua.Visitor);
98 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
99
100 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
101 _enqueue.args[0][0].should.equal("pageview");
102 _enqueue.args[0][1].should.have.keys("dp")
103 _enqueue.args[0][1].dp.should.equal(params.dp);
104 });
105
106
107 it("should accept arguments (params, fn)", function () {
108 var params = {
109 dp: "/" + Math.random(),
110 empty: null // Should be removed
111 };
112 var json = JSON.stringify(params)
113 var fn = sinon.spy()
114
115 ua("UA-XXXXX-XX").pageview(params, fn)
116
117 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
118 _enqueue.args[0][0].should.equal("pageview");
119 _enqueue.args[0][1].should.have.keys("dp")
120 _enqueue.args[0][1].dp.should.equal(params.dp);
121
122 fn.calledOnce.should.equal(true, "callback should have been called once");
123
124 JSON.stringify(params).should.equal(json, "params should not have been modified")
125 });
126
127
128 it("should accept arguments (path, hostname)", function () {
129 var path = Math.random().toString();
130 var hostname = Math.random().toString();
131
132 ua("UA-XXXXX-XX").pageview(path, hostname);
133
134 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
135 _enqueue.args[0][0].should.equal("pageview");
136 _enqueue.args[0][1].should.have.keys("dp", "dh")
137 _enqueue.args[0][1].dp.should.equal(path);
138 _enqueue.args[0][1].dh.should.equal(hostname);
139 });
140
141
142 it("should accept arguments (path, hostname, fn)", function () {
143 var path = Math.random().toString();
144 var hostname = Math.random().toString();
145 var fn = sinon.spy()
146
147 ua("UA-XXXXX-XX").pageview(path, hostname, fn);
148
149 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
150 _enqueue.args[0][0].should.equal("pageview");
151 _enqueue.args[0][1].should.have.keys("dp", "dh")
152 _enqueue.args[0][1].dp.should.equal(path);
153 _enqueue.args[0][1].dh.should.equal(hostname);
154
155 fn.calledOnce.should.equal(true, "callback should have been called once");
156 });
157
158
159 it("should accept arguments (path, hostname, title)", function () {
160 var path = Math.random().toString();
161 var hostname = Math.random().toString();
162 var title = Math.random().toString();
163
164 ua("UA-XXXXX-XX").pageview(path, hostname, title);
165
166 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
167 _enqueue.args[0][0].should.equal("pageview");
168 _enqueue.args[0][1].should.have.keys("dp", "dh", "dt")
169 _enqueue.args[0][1].dp.should.equal(path);
170 _enqueue.args[0][1].dh.should.equal(hostname);
171 _enqueue.args[0][1].dt.should.equal(title);
172 });
173
174
175 it("should accept arguments (path, hostname, title, fn)", function () {
176 var path = Math.random().toString();
177 var hostname = Math.random().toString();
178 var title = Math.random().toString();
179 var fn = sinon.spy()
180
181 ua("UA-XXXXX-XX").pageview(path, hostname, title, fn);
182
183 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
184 _enqueue.args[0][0].should.equal("pageview");
185 _enqueue.args[0][1].should.have.keys("dp", "dh", "dt")
186 _enqueue.args[0][1].dp.should.equal(path);
187 _enqueue.args[0][1].dh.should.equal(hostname);
188 _enqueue.args[0][1].dt.should.equal(title);
189
190 fn.calledOnce.should.equal(true, "callback should have been called once");
191 });
192
193
194 it("should allow daisy-chaining and re-using parameters", function () {
195 var path = "/" + Math.random()
196 var title = Math.random().toString();
197
198 ua("UA-XXXXX-XX").pageview(path, null, title).pageview()
199
200 _enqueue.calledTwice.should.equal(true, "#_enqueue should have been called twice, once for each pageview");
201
202 _enqueue.args[0][0].should.equal(_enqueue.args[1][0]);
203 _enqueue.args[0][1].should.eql(_enqueue.args[1][1]);
204 })
205
206
207 it("should extend and overwrite params when daisy-chaining", function () {
208 var path = "/" + Math.random()
209 var path2 = "/" + Math.random()
210 var title = Math.random().toString();
211 var title2 = Math.random().toString();
212 var foo = Math.random()
213
214 ua("UA-XXXXX-XX")
215 .pageview(path, null, title)
216 .pageview({
217 dt: title2,
218 dp: path2,
219 foo: foo
220 }).pageview(path)
221
222 _enqueue.calledThrice.should.equal(true, "#_enqueue should have been called three times, once for each pageview");
223
224 _enqueue.args[0][1].should.have.keys("dp", "dt")
225 _enqueue.args[0][1].dp.should.equal(path);
226 _enqueue.args[0][1].dt.should.equal(title);
227
228 _enqueue.args[1][1].should.have.keys("dp", "dt", "foo")
229 _enqueue.args[1][1].dp.should.equal(path2);
230 _enqueue.args[1][1].dt.should.equal(title2);
231 _enqueue.args[1][1].foo.should.equal(foo);
232
233 _enqueue.args[2][1].should.have.keys("dp", "dt")
234 _enqueue.args[2][1].dp.should.equal(path);
235 _enqueue.args[2][1].dt.should.equal(title2);
236 })
237
238 it("should accept document location instead of page path", function () {
239 var params = {
240 dl: "http://www.google.com/" + Math.random()
241 };
242 var json = JSON.stringify(params)
243 var fn = sinon.spy()
244
245 ua("UA-XXXXX-XX").pageview(params, fn)
246
247 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
248 _enqueue.args[0][0].should.equal("pageview");
249 _enqueue.args[0][1].should.have.keys("dl")
250 _enqueue.args[0][1].dl.should.equal(params.dl);
251
252 fn.calledOnce.should.equal(true, "callback should have been called once");
253
254 JSON.stringify(params).should.equal(json, "params should not have been modified")
255 });
256
257 it("should fail without page path or document location", function () {
258 var fn = sinon.spy()
259 var visitor = ua()
260
261 var result = visitor.pageview(null, fn);
262
263 visitor._context = result._context;
264 result.should.eql(visitor, "should return a visitor that is identical except for the context");
265
266 result.should.be.instanceof(ua.Visitor);
267 result._context.should.eql({}, "the transaction params should not be persisted")
268
269 _enqueue.called.should.equal(false, "#_enqueue should have not been called once");
270 fn.calledOnce.should.equal(true, "callback should have been called once");
271 fn.args[0][0].should.be.instanceof(Error);
272 fn.thisValues[0].should.equal(visitor);
273 });
274
275 });
276
277});
278
279
280
281
282
283
284
285
286
287