UNPKG

7.75 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("#exception", function () {
18 var _enqueue;
19
20 beforeEach(function () {
21 _enqueue = sinon.stub(ua.Visitor.prototype, "_enqueue", function () {
22 if (arguments.length === 3 && typeof arguments[2] === 'function') {
23 arguments[2]();
24 }
25 return this;
26 });
27 });
28
29 afterEach(function () {
30 _enqueue.restore()
31 });
32
33
34 it("should accept arguments (description)", function () {
35 var description = Math.random().toString();
36
37 var visitor = ua()
38
39 var result = visitor.exception(description);
40
41 visitor._context = result._context;
42 result.should.eql(visitor, "should return a visitor that is identical except for the context");
43
44 result.should.be.instanceof(ua.Visitor);
45 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
46
47 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
48 _enqueue.args[0][0].should.equal("exception");
49 _enqueue.args[0][1].should.have.keys("exd")
50 _enqueue.args[0][1].exd.should.equal(description);
51 });
52
53
54 it("should accept arguments (description, fn)", function () {
55 var description = Math.random().toString();
56 var fn = sinon.spy();
57
58 ua().exception(description, fn);
59
60 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
61 _enqueue.args[0][0].should.equal("exception");
62 _enqueue.args[0][1].should.have.keys("exd")
63 _enqueue.args[0][1].exd.should.equal(description);
64
65 fn.calledOnce.should.equal(true, "callback should have been called once")
66 });
67
68
69 it("should accept arguments (description, fatal)", function () {
70 var description = Math.random().toString();
71 var fatal = +!!(Math.random().toString()*2);
72
73 var visitor = ua()
74
75 var result = visitor.exception(description, fatal);
76
77 visitor._context = result._context;
78 result.should.eql(visitor, "should return a visitor that is identical except for the context");
79
80 result.should.be.instanceof(ua.Visitor);
81 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
82
83 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
84 _enqueue.args[0][0].should.equal("exception");
85 _enqueue.args[0][1].should.have.keys("exd", "exf")
86 _enqueue.args[0][1].exd.should.equal(description);
87 _enqueue.args[0][1].exf.should.equal(fatal);
88 });
89
90
91 it("should accept arguments (description, fatal, fn)", function () {
92 var description = Math.random().toString();
93 var fatal = +!!(Math.random().toString()*2);
94 var fn = sinon.spy();
95
96 var visitor = ua()
97
98 var result = visitor.exception(description, fatal, fn);
99
100 visitor._context = result._context;
101 result.should.eql(visitor, "should return a visitor that is identical except for the context");
102
103 result.should.be.instanceof(ua.Visitor);
104 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
105
106 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
107 _enqueue.args[0][0].should.equal("exception");
108 _enqueue.args[0][1].should.have.keys("exd", "exf")
109 _enqueue.args[0][1].exd.should.equal(description);
110 _enqueue.args[0][1].exf.should.equal(fatal);
111
112 fn.calledOnce.should.equal(true, "callback should have been called once")
113 });
114
115
116 it("should accept arguments (description, fatal, params)", function () {
117 var description = Math.random().toString();
118 var fatal = +!!(Math.random().toString()*2);
119 var params = {"p": "/" + Math.random()}
120
121 var visitor = ua()
122
123 var result = visitor.exception(description, fatal, params);
124
125 visitor._context = result._context;
126 result.should.eql(visitor, "should return a visitor that is identical except for the context");
127
128 result.should.be.instanceof(ua.Visitor);
129 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
130
131 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
132 _enqueue.args[0][0].should.equal("exception");
133 _enqueue.args[0][1].should.have.keys("exd", "exf", "p")
134 _enqueue.args[0][1].exd.should.equal(description);
135 _enqueue.args[0][1].exf.should.equal(fatal);
136 _enqueue.args[0][1].p.should.equal(params.p);
137 });
138
139
140 it("should accept arguments (description, fatal, params, fn)", function () {
141 var description = Math.random().toString();
142 var fatal = +!!(Math.random().toString()*2);
143 var params = {"p": "/" + Math.random()};
144 var fn = sinon.spy();
145
146 var visitor = ua()
147
148 var result = visitor.exception(description, fatal, params, fn);
149
150 visitor._context = result._context;
151 result.should.eql(visitor, "should return a visitor that is identical except for the context");
152
153 result.should.be.instanceof(ua.Visitor);
154 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
155
156 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
157 _enqueue.args[0][0].should.equal("exception");
158 _enqueue.args[0][1].should.have.keys("exd", "exf", "p")
159 _enqueue.args[0][1].exd.should.equal(description);
160 _enqueue.args[0][1].exf.should.equal(fatal);
161 _enqueue.args[0][1].p.should.equal(params.p);
162
163 fn.calledOnce.should.equal(true, "callback should have been called once")
164 });
165
166
167 it("should accept arguments (params)", function () {
168 var params = {
169 exd: Math.random().toString(),
170 exf: +!!(Math.random().toString()*2),
171 p: "/" + Math.random()
172 };
173
174 var visitor = ua()
175
176 var result = visitor.exception(params);
177
178 visitor._context = result._context;
179 result.should.eql(visitor, "should return a visitor that is identical except for the context");
180
181 result.should.be.instanceof(ua.Visitor);
182 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
183
184 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
185 _enqueue.args[0][0].should.equal("exception");
186 _enqueue.args[0][1].should.have.keys("exd", "exf", "p")
187 _enqueue.args[0][1].exd.should.equal(params.exd);
188 _enqueue.args[0][1].exf.should.equal(params.exf);
189 _enqueue.args[0][1].p.should.equal(params.p);
190 });
191
192
193 it("should accept arguments (params, fn)", function () {
194 var params = {
195 exd: Math.random().toString(),
196 exf: +!!(Math.random().toString()*2),
197 p: "/" + Math.random()
198 };
199 var fn = sinon.spy();
200
201 var visitor = ua()
202
203 var result = visitor.exception(params, fn);
204
205 visitor._context = result._context;
206 result.should.eql(visitor, "should return a visitor that is identical except for the context");
207
208 result.should.be.instanceof(ua.Visitor);
209 result._context.should.eql(_enqueue.args[0][1], "the pageview params should be persisted as the context of the visitor clone")
210
211 _enqueue.calledOnce.should.equal(true, "#_enqueue should have been called once");
212 _enqueue.args[0][0].should.equal("exception");
213 _enqueue.args[0][1].should.have.keys("exd", "exf", "p")
214 _enqueue.args[0][1].exd.should.equal(params.exd);
215 _enqueue.args[0][1].exf.should.equal(params.exf);
216 _enqueue.args[0][1].p.should.equal(params.p);
217
218 fn.calledOnce.should.equal(true, "callback should have been called once")
219 });
220
221 });
222
223});
224
225
226
227
228
229
230
231
232
233