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