1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var request = require("request");
var qs = require("querystring");
var uuid = require("uuid");
var should = require("should");
var sinon = require("sinon");
var url = require("url");
var ua = require("../lib/index.js");
var utils = require("../lib/utils.js")
var config = require("../lib/config.js")
describe("ua", function () {
it("should be usable as a function", function () {
ua("foo").should.be.instanceof(ua.Visitor);
});
it("should be usable as a constructor", function () {
new ua("foo").should.be.instanceof(ua.Visitor);
});
it("should accept arguments (tid, cid, options)", function () {
var tid = "UA-XXXXX-XX"
var cid = uuid.v4()
var options = {};
var visitor = ua(tid, cid, options)
visitor.tid.should.equal(tid)
visitor.cid.should.equal(cid)
visitor.options.should.equal(options)
});
it("should accept arguments (tid, cid)", function () {
var tid = "UA-XXXXX-XX"
var cid = uuid.v4()
var visitor = ua(tid, cid)
visitor.tid.should.equal(tid)
visitor.cid.should.equal(cid)
visitor.options.should.eql({}, "An empty options hash should've been created")
});
it("should accept arguments (tid, options)", function () {
var tid = Math.random().toString();
var options = {}
var visitor = ua(tid, options)
visitor.tid.should.equal(tid)
utils.isUuid(visitor.cid).should.equal(true, "A valid random UUID should have been generated")
visitor.options.should.eql(options)
});
it("should accept arguments (options)", function () {
var options = {}
var visitor = ua(options);
visitor.should.have.property('tid', undefined);
utils.isUuid(visitor.cid).should.equal(true, "A valid random UUID should have been generated")
visitor.options.should.eql(options)
});
it("should accept tid and cid via the options arguments", function () {
var options = {
tid: "UA-XXXXX-XX",
cid: uuid.v4()
};
var visitor = ua(options);
visitor.tid.should.equal(options.tid)
visitor.cid.should.equal(options.cid)
visitor.options.should.equal(options)
});
it("should generate new cid (UUID) if provided one is in wrong format", function () {
var options = {
tid: "UA-XXXXX-XX",
cid: "custom-format-cid"
};
var next = sinon.spy(uuid, 'v4')
var visitor = ua(options);
next.calledOnce.should.equal(true, "next() should've been called once")
var generatedCid = next.returnValues[0]
uuid.v4.restore()
visitor.cid.should.not.equal(options.cid)
visitor.cid.should.equal(generatedCid)
});
it("should accept custom cid format when strictCidFormat is false", function () {
var options = {
tid: "UA-XXXXX-XX",
cid: "custom-format-cid",
strictCidFormat: false
};
var next = sinon.spy(uuid, 'v4')
var visitor = ua(options);
next.called.should.equal(false, "next() should't be called")
uuid.v4.restore()
visitor.cid.should.equal(options.cid)
});
describe("params", function () {
var visitor;
before(function () {
var tid = "UA-XXXXX-XX";
var cid = uuid.v4();
visitor = ua(tid, cid);
});
it('should not translate params', function(){
var params = {
tid: 1,
cid: 1,
somefake: 1,
v: 'a'
};
visitor._translateParams(params).should.eql(params);
})
it('should match all parameters and each should be in the list of accepted', function(){
var res = visitor._translateParams(config.parametersMap);
for (var i in res) {
if (res.hasOwnProperty(i)) {
res[i].should.equal(i);
config.acceptedParameters.should.containEql(i);
}
}
})
});
});