Skip to content

Commit 5f76199

Browse files
committed
refactored tests to include subscription declaration and cleanup in each test
1 parent dc51092 commit 5f76199

1 file changed

Lines changed: 37 additions & 16 deletions

File tree

core/src/global-ad-events.spec.ts

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,14 @@ import {
66
} from './global-ad-events';
77

88
describe('globalAdEvents', () => {
9-
let subscription: { remove: () => void } | undefined;
10-
119
afterEach(() => {
12-
subscription?.remove();
1310
_resetHistory();
1411
jest.clearAllMocks();
1512
});
1613

1714
it('calls handler when a matching status event "rendered" is dispatched', () => {
1815
const handler = jest.fn();
19-
subscription = globalAdEvents('rendered', handler);
16+
const subscription = globalAdEvents('rendered', handler);
2017

2118
document.dispatchEvent(
2219
new CustomEvent('commercial:adStatusChange', {
@@ -37,11 +34,17 @@ describe('globalAdEvents', () => {
3734
slotName: 'top-above-nav',
3835
status: true,
3936
});
37+
38+
subscription.remove();
4039
});
4140
it('calls handler when a matching status and slotName is dispatched', () => {
4241
const handler = jest.fn();
4342

44-
subscription = globalAdEvents('loading', handler, 'top-above-nav');
43+
const subscription = globalAdEvents(
44+
'loading',
45+
handler,
46+
'top-above-nav',
47+
);
4548

4649
document.dispatchEvent(
4750
new CustomEvent('commercial:adStatusChange', {
@@ -62,10 +65,12 @@ describe('globalAdEvents', () => {
6265
slotName: 'top-above-nav',
6366
status: true,
6467
});
68+
69+
subscription.remove();
6570
});
6671
it('calls handler multiple times for multiple matching events', () => {
6772
const handler = jest.fn();
68-
subscription = globalAdEvents('rendered', handler);
73+
const subscription = globalAdEvents('rendered', handler);
6974

7075
document.dispatchEvent(
7176
new CustomEvent('commercial:adStatusChange', {
@@ -87,11 +92,12 @@ describe('globalAdEvents', () => {
8792
);
8893

8994
expect(handler).toHaveBeenCalledTimes(2);
95+
subscription.remove();
9096
});
9197
it('calls handler multiple times for multiple status arguments', () => {
9298
const handler = jest.fn();
9399

94-
subscription = globalAdEvents(
100+
const subscription = globalAdEvents(
95101
['rendered', 'loaded'],
96102
handler,
97103
'top-above-nav',
@@ -135,11 +141,12 @@ describe('globalAdEvents', () => {
135141
},
136142
}),
137143
);
144+
subscription.remove();
138145
});
139146

140147
it('calls handler only for matching status arguments', () => {
141148
const handler = jest.fn();
142-
subscription = globalAdEvents(
149+
const subscription = globalAdEvents(
143150
['rendered', 'loaded'],
144151
handler,
145152
'top-above-nav',
@@ -197,10 +204,10 @@ describe('globalAdEvents', () => {
197204
},
198205
}),
199206
);
207+
subscription.remove();
200208
});
201209

202210
it('replays matching historical events for new listeners', () => {
203-
// Add a historical event
204211
eventHistory.push(
205212
new CustomEvent('commercial:adStatusChange', {
206213
detail: {
@@ -212,7 +219,11 @@ describe('globalAdEvents', () => {
212219
);
213220

214221
const handler = jest.fn();
215-
subscription = globalAdEvents('rendered', handler, 'top-above-nav');
222+
const subscription = globalAdEvents(
223+
'rendered',
224+
handler,
225+
'top-above-nav',
226+
);
216227

217228
expect(handler).toHaveBeenCalledTimes(1);
218229
expect(handler).toHaveBeenCalledWith(
@@ -224,12 +235,13 @@ describe('globalAdEvents', () => {
224235
},
225236
}),
226237
);
238+
subscription.remove();
227239
});
228240

229241
it('does not call handler when event status "rendering" does not match subscription status "loading"', () => {
230242
const handler = jest.fn();
231243

232-
subscription = globalAdEvents('loading', handler);
244+
const subscription = globalAdEvents('loading', handler);
233245

234246
document.dispatchEvent(
235247
new CustomEvent('commercial:adStatusChange', {
@@ -242,11 +254,12 @@ describe('globalAdEvents', () => {
242254
);
243255

244256
expect(handler).not.toHaveBeenCalled();
257+
subscription.remove();
245258
});
246-
it('handler not called when slotName does not matching', () => {
259+
it('handler not called when slotName does not match', () => {
247260
const handler = jest.fn();
248261

249-
subscription = globalAdEvents('loading', handler, 'inline1');
262+
const subscription = globalAdEvents('loading', handler, 'inline1');
250263

251264
document.dispatchEvent(
252265
new CustomEvent('commercial:adStatusChange', {
@@ -259,11 +272,16 @@ describe('globalAdEvents', () => {
259272
);
260273

261274
expect(handler).not.toHaveBeenCalled();
275+
subscription.remove();
262276
});
263277
it('handler not called when custom event name does not match', () => {
264278
const handler = jest.fn();
265279

266-
subscription = globalAdEvents('loading', handler, 'top-above-nav');
280+
const subscription = globalAdEvents(
281+
'loading',
282+
handler,
283+
'top-above-nav',
284+
);
267285

268286
document.dispatchEvent(
269287
new CustomEvent('statusChange', {
@@ -275,9 +293,10 @@ describe('globalAdEvents', () => {
275293
}),
276294
);
277295
expect(handler).not.toHaveBeenCalled();
296+
subscription.remove();
278297
});
279298
it('adds events to eventHistory', () => {
280-
subscription = globalAdEvents('rendered', jest.fn());
299+
const subscription = globalAdEvents('rendered', jest.fn());
281300
document.dispatchEvent(
282301
new CustomEvent('commercial:adStatusChange', {
283302
detail: {
@@ -294,10 +313,11 @@ describe('globalAdEvents', () => {
294313
name: 'rendered',
295314
status: true,
296315
});
316+
subscription.remove();
297317
});
298318
it('does not call handler after remove is called', () => {
299319
const handler = jest.fn();
300-
subscription = globalAdEvents('rendered', handler);
320+
const subscription = globalAdEvents('rendered', handler);
301321

302322
subscription.remove();
303323

@@ -312,5 +332,6 @@ describe('globalAdEvents', () => {
312332
);
313333

314334
expect(handler).not.toHaveBeenCalled();
335+
subscription.remove();
315336
});
316337
});

0 commit comments

Comments
 (0)