request.test.js
2.96 KB
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
'use strict';
var request = require('../src/js/request');
describe('module:request', function() {
describe('imagePing', function() {
beforeEach(function() {
document.body.innerHTML = '';
});
it('should add tracking dom in body', function() {
var trackingElement = request.imagePing('https://www.google-analytics.com/collect', {
v: 1,
t: 'event',
tid: 'tracnkingid',
cid: 'cid',
dp: 'dp',
dh: 'dh'
});
expect(trackingElement.src).toBe('https://www.google-analytics.com/collect?v=1&t=event&tid=tracnkingid&cid=cid&dp=dp&dh=dh');
});
});
describe('sendHostname', function() {
beforeEach(function() {
window.tui = window.tui || {};
// can not spy on imagePing. spy on appendChild instead.
spyOn(document.body, 'appendChild');
spyOn(document.body, 'removeChild');
spyOn(Storage.prototype, 'getItem').and.returnValue(null);
});
it('should call appendChild', function(done) {
request.sendHostname('editor');
setTimeout(function() {
expect(document.body.appendChild).toHaveBeenCalled();
done();
}, 1500);
});
it('should not call appendChild', function(done) {
window.tui.usageStatistics = false;
request.sendHostname('editor');
setTimeout(function() {
expect(document.body.appendChild).not.toHaveBeenCalled();
done();
}, 1500);
});
});
describe('sendHostname with localstorage', function() {
beforeEach(function() {
window.tui = window.tui || {};
// can not spy on imagePing. spy on appendChild instead.
spyOn(document.body, 'appendChild');
spyOn(document.body, 'removeChild');
});
it('should not call appendChild within 7 days', function(done) {
var now = new Date().getTime();
var ms6days = 6 * 24 * 60 * 60 * 1000;
spyOn(Storage.prototype, 'getItem').and.returnValue(now - ms6days);
window.tui.usageStatistics = true;
request.sendHostname('editor');
setTimeout(function() {
expect(document.body.appendChild).not.toHaveBeenCalled();
done();
}, 1500);
});
it('should call appendChild after 7 days', function(done) {
var now = new Date().getTime();
var ms8days = 8 * 24 * 60 * 60 * 1000;
spyOn(Storage.prototype, 'getItem').and.returnValue(now - ms8days);
window.tui.usageStatistics = true;
request.sendHostname('editor');
setTimeout(function() {
expect(document.body.appendChild).toHaveBeenCalled();
done();
}, 1500);
});
});
});