This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 400
/
Copy pathreporter-proxy.test.js
151 lines (121 loc) · 5.58 KB
/
reporter-proxy.test.js
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
import {addEvent, addTiming, FIVE_MINUTES_IN_MILLISECONDS, FakeReporter, incrementCounter, reporterProxy} from '../lib/reporter-proxy';
const pjson = require('../package.json');
const version = pjson.version;
const fakeReporter = new FakeReporter();
describe('reporterProxy', function() {
const event = {coAuthorCount: 2};
const eventType = 'commits';
const timingEventType = 'load';
const durationInMilliseconds = 42;
const counterName = 'push';
beforeEach(function() {
// let's not leak state between tests, dawg.
reporterProxy.events = [];
reporterProxy.timings = [];
reporterProxy.counters = [];
});
describe('before reporter has been set', function() {
it('adds event to queue when addEvent is called', function() {
addEvent(eventType, event);
const events = reporterProxy.events;
assert.lengthOf(events, 1);
const actualEvent = events[0];
assert.deepEqual(actualEvent.eventType, eventType);
assert.deepEqual(actualEvent.event, {coAuthorCount: 2, gitHubPackageVersion: version});
});
it('adds timing to queue when addTiming is called', function() {
addTiming(timingEventType, durationInMilliseconds);
const timings = reporterProxy.timings;
assert.lengthOf(timings, 1);
const timing = timings[0];
assert.deepEqual(timing.eventType, timingEventType);
assert.deepEqual(timing.durationInMilliseconds, durationInMilliseconds);
assert.deepEqual(timing.metadata, {gitHubPackageVersion: version});
});
it('adds counter to queue when incrementCounter is called', function() {
incrementCounter(counterName);
const counters = reporterProxy.counters;
assert.lengthOf(counters, 1);
assert.deepEqual(counters[0], counterName);
});
});
describe('if reporter is never set', function() {
it('sets the reporter to no op class once interval has passed', function() {
const clock = sinon.useFakeTimers();
const setReporterSpy = sinon.spy(reporterProxy, 'setReporter');
addEvent(eventType, event);
addTiming(timingEventType, durationInMilliseconds);
incrementCounter(counterName);
assert.lengthOf(reporterProxy.events, 1);
assert.lengthOf(reporterProxy.timings, 1);
assert.lengthOf(reporterProxy.counters, 1);
assert.isFalse(setReporterSpy.called);
assert.isNull(reporterProxy.reporter);
setTimeout(() => {
assert.isTrue(reporterProxy.reporter);
assert.isTrue(setReporterSpy.called);
assert.lengthOf(reporterProxy.events, 0);
assert.lengthOf(reporterProxy.timings, 0);
assert.lengthOf(reporterProxy.counters, 0);
}, FIVE_MINUTES_IN_MILLISECONDS);
clock.restore();
});
});
describe('after reporter has been set', function() {
let addCustomEventStub, addTimingStub, incrementCounterStub;
beforeEach(function() {
addCustomEventStub = sinon.stub(fakeReporter, 'addCustomEvent');
addTimingStub = sinon.stub(fakeReporter, 'addTiming');
incrementCounterStub = sinon.stub(fakeReporter, 'incrementCounter');
});
it('empties all queues', function() {
addEvent(eventType, event);
addTiming(timingEventType, durationInMilliseconds);
incrementCounter(counterName);
assert.isFalse(addCustomEventStub.called);
assert.isFalse(addTimingStub.called);
assert.isFalse(incrementCounterStub.called);
assert.lengthOf(reporterProxy.events, 1);
assert.lengthOf(reporterProxy.timings, 1);
assert.lengthOf(reporterProxy.counters, 1);
reporterProxy.setReporter(fakeReporter);
const addCustomEventArgs = addCustomEventStub.lastCall.args;
assert.deepEqual(addCustomEventArgs[0], eventType);
assert.deepEqual(addCustomEventArgs[1], {coAuthorCount: 2, gitHubPackageVersion: version});
const addTimingArgs = addTimingStub.lastCall.args;
assert.deepEqual(addTimingArgs[0], timingEventType);
assert.deepEqual(addTimingArgs[1], durationInMilliseconds);
assert.deepEqual(addTimingArgs[2], {gitHubPackageVersion: version});
assert.deepEqual(incrementCounterStub.lastCall.args, [counterName]);
assert.lengthOf(reporterProxy.events, 0);
assert.lengthOf(reporterProxy.timings, 0);
assert.lengthOf(reporterProxy.counters, 0);
});
it('calls addCustomEvent directly, bypassing queue', function() {
assert.isFalse(addCustomEventStub.called);
reporterProxy.setReporter(fakeReporter);
addEvent(eventType, event);
assert.lengthOf(reporterProxy.events, 0);
const addCustomEventArgs = addCustomEventStub.lastCall.args;
assert.deepEqual(addCustomEventArgs[0], eventType);
assert.deepEqual(addCustomEventArgs[1], {coAuthorCount: 2, gitHubPackageVersion: version});
});
it('calls addTiming directly, bypassing queue', function() {
assert.isFalse(addTimingStub.called);
reporterProxy.setReporter(fakeReporter);
addTiming(timingEventType, durationInMilliseconds);
assert.lengthOf(reporterProxy.timings, 0);
const addTimingArgs = addTimingStub.lastCall.args;
assert.deepEqual(addTimingArgs[0], timingEventType);
assert.deepEqual(addTimingArgs[1], durationInMilliseconds);
assert.deepEqual(addTimingArgs[2], {gitHubPackageVersion: version});
});
it('calls incrementCounter directly, bypassing queue', function() {
assert.isFalse(incrementCounterStub.called);
reporterProxy.setReporter(fakeReporter);
incrementCounter(counterName);
assert.lengthOf(reporterProxy.counters, 0);
assert.deepEqual(incrementCounterStub.lastCall.args, [counterName]);
});
});
});