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 | it("should be usable as a function", function () {
|
18 | ua("foo").should.be.instanceof(ua.Visitor);
|
19 | });
|
20 |
|
21 | it("should be usable as a constructor", function () {
|
22 | new ua("foo").should.be.instanceof(ua.Visitor);
|
23 | });
|
24 |
|
25 | it("should accept arguments (tid, cid, options)", function () {
|
26 | var tid = "UA-XXXXX-XX"
|
27 | var cid = uuid.v4()
|
28 | var options = {};
|
29 |
|
30 | var visitor = ua(tid, cid, options)
|
31 |
|
32 | visitor.tid.should.equal(tid)
|
33 | visitor.cid.should.equal(cid)
|
34 | visitor.options.should.equal(options)
|
35 | });
|
36 |
|
37 | it("should accept arguments (tid, cid)", function () {
|
38 | var tid = "UA-XXXXX-XX"
|
39 | var cid = uuid.v4()
|
40 |
|
41 | var visitor = ua(tid, cid)
|
42 |
|
43 | visitor.tid.should.equal(tid)
|
44 | visitor.cid.should.equal(cid)
|
45 | visitor.options.should.eql({}, "An empty options hash should've been created")
|
46 | });
|
47 |
|
48 | it("should accept arguments (tid, options)", function () {
|
49 | var tid = Math.random().toString();
|
50 | var options = {}
|
51 |
|
52 | var visitor = ua(tid, options)
|
53 |
|
54 | visitor.tid.should.equal(tid)
|
55 | utils.isUuid(visitor.cid).should.equal(true, "A valid random UUID should have been generated")
|
56 | visitor.options.should.eql(options)
|
57 |
|
58 | });
|
59 |
|
60 | it("should accept arguments (options)", function () {
|
61 | var options = {}
|
62 |
|
63 | var visitor = ua(options);
|
64 |
|
65 | visitor.should.have.property('tid', undefined);
|
66 | utils.isUuid(visitor.cid).should.equal(true, "A valid random UUID should have been generated")
|
67 | visitor.options.should.eql(options)
|
68 | });
|
69 |
|
70 | it("should accept tid and cid via the options arguments", function () {
|
71 | var options = {
|
72 | tid: "UA-XXXXX-XX",
|
73 | cid: uuid.v4()
|
74 | };
|
75 |
|
76 | var visitor = ua(options);
|
77 |
|
78 | visitor.tid.should.equal(options.tid)
|
79 | visitor.cid.should.equal(options.cid)
|
80 | visitor.options.should.equal(options)
|
81 | });
|
82 |
|
83 | it("should generate new cid (UUID) if provided one is in wrong format", function () {
|
84 | var options = {
|
85 | tid: "UA-XXXXX-XX",
|
86 | cid: "custom-format-cid"
|
87 | };
|
88 |
|
89 | var next = sinon.spy(uuid, 'v4')
|
90 |
|
91 | var visitor = ua(options);
|
92 |
|
93 | next.calledOnce.should.equal(true, "next() should've been called once")
|
94 | var generatedCid = next.returnValues[0]
|
95 | uuid.v4.restore()
|
96 |
|
97 | visitor.cid.should.not.equal(options.cid)
|
98 | visitor.cid.should.equal(generatedCid)
|
99 | });
|
100 |
|
101 | it("should accept custom cid format when strictCidFormat is false", function () {
|
102 | var options = {
|
103 | tid: "UA-XXXXX-XX",
|
104 | cid: "custom-format-cid",
|
105 | strictCidFormat: false
|
106 | };
|
107 |
|
108 | var next = sinon.spy(uuid, 'v4')
|
109 |
|
110 | var visitor = ua(options);
|
111 |
|
112 | next.called.should.equal(false, "next() should't be called")
|
113 | uuid.v4.restore()
|
114 |
|
115 | visitor.cid.should.equal(options.cid)
|
116 | });
|
117 |
|
118 |
|
119 | describe("params", function () {
|
120 |
|
121 | var visitor;
|
122 |
|
123 | before(function () {
|
124 | var tid = "UA-XXXXX-XX";
|
125 | var cid = uuid.v4();
|
126 | visitor = ua(tid, cid);
|
127 | });
|
128 |
|
129 | it('should not translate params', function(){
|
130 | var params = {
|
131 | tid: 1,
|
132 | cid: 1,
|
133 | somefake: 1,
|
134 | v: 'a'
|
135 | };
|
136 |
|
137 | visitor._translateParams(params).should.eql(params);
|
138 | })
|
139 |
|
140 | it('should match all parameters and each should be in the list of accepted', function(){
|
141 | var res = visitor._translateParams(config.parametersMap);
|
142 | for (var i in res) {
|
143 | if (res.hasOwnProperty(i)) {
|
144 | res[i].should.equal(i);
|
145 | config.acceptedParameters.should.containEql(i);
|
146 | }
|
147 | }
|
148 | })
|
149 |
|
150 | });
|
151 |
|
152 | describe("#debug", function () {
|
153 |
|
154 | var log;
|
155 |
|
156 | before(function () {
|
157 | log = sinon.stub(ua.Visitor.prototype, "_log")
|
158 | });
|
159 |
|
160 | after(function () {
|
161 | log.restore();
|
162 | });
|
163 |
|
164 | it("should enable debugging when invoked without arguments", function () {
|
165 | var visitor = ua().debug()
|
166 |
|
167 | visitor.options.debug.should.equal(true);
|
168 |
|
169 | visitor.debug().should.equal(visitor, "should return itself")
|
170 |
|
171 | visitor.options.debug.should.equal(true, "A second #debug call should leave debugging enabled");
|
172 | });
|
173 |
|
174 | it("should toggle debugging when invoked with a boolean arguments", function () {
|
175 | var visitor = ua().debug(true)
|
176 |
|
177 | visitor.options.debug.should.equal(true);
|
178 |
|
179 | visitor.debug(false).should.equal(visitor, "should return itself")
|
180 |
|
181 | visitor.options.debug.should.equal(false);
|
182 | });
|
183 |
|
184 | });
|
185 |
|
186 | });
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|