UNPKG

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