Skip to content

Commit 5698642

Browse files
committed
add tests for commercial queue
1 parent 3983eb4 commit 5698642

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { createCommercialQueue } from './guardian-commercial-queue';
2+
3+
window.guardian.config.page.adUnit = 'adUnit';
4+
5+
describe('createCommercialQueue', () => {
6+
it('should return an object with push and flush methods', () => {
7+
const queue = createCommercialQueue();
8+
expect(queue).toBeDefined();
9+
expect(typeof queue.push).toBe('function');
10+
expect(typeof queue.flush).toBe('function');
11+
});
12+
it('should buffer a function without executing it', () => {
13+
const queue = createCommercialQueue();
14+
const mockFn = jest.fn();
15+
queue.push(mockFn);
16+
expect(mockFn).not.toHaveBeenCalled();
17+
});
18+
it('should buffer multiple functions without executing them', () => {
19+
const queue = createCommercialQueue();
20+
const mockFn1 = jest.fn();
21+
const mockFn2 = jest.fn();
22+
const mockFn3 = jest.fn();
23+
queue.push(mockFn1, mockFn2, mockFn3);
24+
[mockFn1, mockFn2, mockFn3].forEach((fn) => {
25+
expect(fn).not.toHaveBeenCalled();
26+
});
27+
});
28+
it('flush method should execute all buffered functions in order', () => {
29+
const queue = createCommercialQueue();
30+
const mockFn1 = jest.fn();
31+
const mockFn2 = jest.fn();
32+
const mockFn3 = jest.fn();
33+
queue.push(mockFn1, mockFn2, mockFn3);
34+
queue.flush();
35+
36+
expect(mockFn1.mock.invocationCallOrder[0]!).toBeLessThan(
37+
mockFn2.mock.invocationCallOrder[0]!,
38+
);
39+
expect(mockFn2.mock.invocationCallOrder[0]!).toBeLessThan(
40+
mockFn3.mock.invocationCallOrder[0]!,
41+
);
42+
[mockFn1, mockFn2, mockFn3].forEach((fn) => {
43+
expect(fn).toHaveBeenLastCalledWith();
44+
});
45+
});
46+
});

bundle/src/lib/guardian-commercial-queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ export const createCommercialQueue = (): QueueArray => {
3636
}
3737
});
3838
buffer.length = 0;
39-
}
39+
},
4040
};
4141
};

core/src/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ type QueueFunction = () => void;
4545
interface QueueArray {
4646
push: (...items: QueueFunction[]) => number;
4747
flush: () => void;
48-
getAll: () => QueueFunction[];
4948
}
5049

5150
type ConnectionType =

0 commit comments

Comments
 (0)