forked from InteractiveAdvertisingBureau/iabtcf-es
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReportedIssues.test.ts
More file actions
292 lines (170 loc) · 7.75 KB
/
ReportedIssues.test.ts
File metadata and controls
292 lines (170 loc) · 7.75 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import * as stub from '@iabtechlabtcf/stub';
import * as sinon from 'sinon';
import {API_KEY, CmpApi} from '../src/';
import {CmpApiModel} from '../src/CmpApiModel';
import {Disabled, Response, TCData, InAppTCData} from '../src/response';
import {EventStatus} from '../src/status/EventStatus';
import {TCFCommand} from '../src/command/TCFCommand';
import {TestUtils} from './TestUtils';
import {expect} from 'chai';
import {makeRandomInt, TCStringFactory} from '@iabtechlabtcf/testing';
describe('Reported issues', (): void => {
const removeStub = (): void =>{
// clean up that junk
if (typeof window[API_KEY] === 'function') {
delete window[API_KEY];
}
const iframes = document.querySelectorAll('iframe');
for (let i = 0; i < iframes.length; i++) {
const frame: HTMLElement = iframes[i];
if (frame !== null && frame.parentNode) {
frame.parentNode.removeChild(frame);
}
}
};
beforeEach((): void => {
stub.default();
CmpApiModel.reset();
});
afterEach((): void => {
removeStub();
});
it('Issue 96 CmpApi should respond to addEventListener call with an error object when in an Error state', (done: () => void): void => {
const cmpApi = TestUtils.getCmpApi();
cmpApi.update(TCStringFactory.base());
cmpApi.disable();
window[API_KEY](TCFCommand.ADD_EVENT_LISTENER, 2, (response: Response): void => {
expect(response instanceof Disabled, 'response instanceof Disabled').to.be.true;
done();
});
});
it('Should not throw an error if getTCData is called before update is called', (done: () => void): void => {
const cmpApi = TestUtils.getCmpApi();
const callDatFunc = (): void => {
window[API_KEY](TCFCommand.GET_TC_DATA, 2, (response: Response): void => {
expect(response instanceof TCData, 'response instanceof TCData').to.be.true;
done();
});
};
expect(callDatFunc, 'call getTCData').not.to.throw();
cmpApi.update(TCStringFactory.base());
});
it('126 Should not throw an error tc string is set as ""', (done: () => void): void => {
const cmpId = makeRandomInt(2, Math.pow(2, 6));
const cmpVersion = makeRandomInt(2, Math.pow(2, 6));
const cmpApi = new CmpApi(cmpId, cmpVersion);
const emptyString = '';
expect((): void => {
cmpApi.update(emptyString);
}).not.to.throw();
expect(CmpApiModel.tcModel, 'tcModel').not.to.be.undefined;
expect(CmpApiModel.tcModel, 'tcModel').not.to.be.null;
expect(CmpApiModel.tcString, 'tcString').to.equal(emptyString);
expect(CmpApiModel.tcModel.cmpId, 'tcModel.cmpId').to.equal(cmpId);
expect(CmpApiModel.tcModel.cmpVersion, 'tcModel.cmpVersion').to.equal(cmpVersion);
window[API_KEY](TCFCommand.GET_TC_DATA, 2, (tcData: TCData): void => {
expect(tcData.tcString, 'tcData.tcString').to.equal(emptyString);
expect(tcData.cmpId, 'tcData.tcModel.cmpId').to.equal(cmpId);
expect(tcData.cmpVersion, 'tcData.cmpVersion').to.equal(cmpVersion);
done();
});
});
it('120 AddEventListener: \'cmpuishown\' isn\'t being triggered - test toggles update UIVisible', (done: () => void): void => {
const cmpId = makeRandomInt(2, Math.pow(2, 6));
const cmpVersion = makeRandomInt(2, Math.pow(2, 6));
const numTimes = 4;
let count = 0;
window[API_KEY](TCFCommand.ADD_EVENT_LISTENER, 2, (tcData: TCData, success: boolean): void => {
count++;
let eventStatus: EventStatus;
if (count % 2) {
eventStatus = EventStatus.CMP_UI_SHOWN;
} else {
eventStatus = EventStatus.USER_ACTION_COMPLETE;
}
expect(success, `success #${count}`).to.be.true;
expect(tcData, `tcData #${count}`).not.to.be.null;
expect(tcData.eventStatus, `evenStatus #${count}`).to.equal(eventStatus);
if (count === numTimes) {
done();
}
});
const cmpApi = new CmpApi(cmpId, cmpVersion);
for (let i = 0; i < numTimes; i++) {
cmpApi.update(TCStringFactory.base(), !(i%2));
}
});
it('164 Queued \'getTCData\' failing when vendorIds is set', (done: () => void): void => {
let callNum = 0;
const callback = (tcData: TCData, success: boolean): void => {
callNum++;
expect(success, `success call #${callNum}`).to.be.true;
expect(tcData, `tcData call #${callNum}`).not.to.be.null;
if (callNum === 2) {
done();
}
};
window[API_KEY](TCFCommand.GET_TC_DATA, null, callback);
window[API_KEY](TCFCommand.GET_TC_DATA, null, callback, [9]);
const cmpApi = TestUtils.getCmpApi();
cmpApi.update(TCStringFactory.base());
});
it('195 getInAppTCData no longer returns a success argument', (): void => {
const callback = (inAppTCData: InAppTCData, success: boolean): void => {
expect(inAppTCData, 'InAppTCData').not.to.be.null;
expect(success, 'success').to.be.true;
};
window[API_KEY](TCFCommand.GET_IN_APP_TC_DATA, null, callback);
const cmpApi = TestUtils.getCmpApi();
cmpApi.update(TCStringFactory.base());
});
it('196 should not call callback twice on addEventListener', (): void => {
const callback = sinon.fake();
window[API_KEY](TCFCommand.ADD_EVENT_LISTENER, null, callback);
expect(callback.callCount, 'callback.callCount :: Before CMP API Created').to.equal(0);
const cmpApi = TestUtils.getCmpApi();
expect(callback.callCount, 'callback.callCount :: After CMP API Created').to.equal(0);
cmpApi.update(TCStringFactory.base());
expect(callback.callCount, 'callback.callCount :: After CmpApi.update()').to.equal(1);
});
it('208 Custom Commands no longer get called before update', (): void => {
const callback = sinon.fake();
const command = sinon.stub();
const commandName = 'makeASandwich';
command.callsFake(callback);
// call custom command to the stub
window[API_KEY](commandName, null, callback);
// expect the callback not to be called yet because it's still the stub
expect(callback.callCount, 'callback.callCount :: Before CMP API Created').to.equal(0);
const cmpApi = TestUtils.getCmpApi({
[commandName]: command,
});
// expect the callback to be called because it should be called on creation
expect(callback.callCount, 'callback.callCount :: After CMP API Created').to.equal(1);
cmpApi.update(TCStringFactory.base());
// expect the callback not to be called again on update, should equal the last time
expect(callback.callCount, 'callback.callCount :: After CmpApi.update()').to.equal(1);
});
it('220 Events are not triggered in addEventListener() after update() in invoked with uiVisible=false', (): void => {
const spy = sinon.spy();
const tcString = 'CO5dPi4O5dPx2B7DvCENA2CgAAAAAAAAAAAAGewAAM8gAAAA.YAAAAAAAAAAA';
const cmpApi = new CmpApi(makeRandomInt(2, 100), makeRandomInt(2, 100), true);
window[API_KEY]('addEventListener', 2, spy);
cmpApi.update(tcString, false);
cmpApi.update(tcString, true);
cmpApi.update(tcString, false);
cmpApi.update(tcString, true);
cmpApi.update(tcString, false);
expect(spy.callCount, 'spy callCount').to.equal(5);
let tcData = spy.getCall(0).args[0];
expect(tcData.eventStatus, `call 0 eventStatus`).to.equal(EventStatus.TC_LOADED);
tcData = spy.getCall(1).args[0];
expect(tcData.eventStatus, `call 1 eventStatus`).to.equal(EventStatus.CMP_UI_SHOWN);
tcData = spy.getCall(2).args[0];
expect(tcData.eventStatus, `call 2 eventStatus`).to.equal(EventStatus.USER_ACTION_COMPLETE);
tcData = spy.getCall(3).args[0];
expect(tcData.eventStatus, `call 3 eventStatus`).to.equal(EventStatus.CMP_UI_SHOWN);
tcData = spy.getCall(4).args[0];
expect(tcData.eventStatus, `call 4 eventStatus`).to.equal(EventStatus.USER_ACTION_COMPLETE);
});
});