Skip to content

Commit c604b88

Browse files
committed
Add test coverage for terminology alias functions
- Add test for computeInCoordinatingFrame function alias - Add tests for coordinator and isCoordinator property aliases - Add test for computeInCoordinatingFrame method delegation - Use AMP-style focused, minimal test approach - Achieve 100% coverage for new alias functionality
1 parent 73fa567 commit c604b88

File tree

2 files changed

+103
-1
lines changed

2 files changed

+103
-1
lines changed
Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,58 @@
1-
import {masterSelection} from '#3p/ampcontext-integration';
1+
import {
2+
IntegrationAmpContext,
3+
masterSelection,
4+
} from '#3p/ampcontext-integration';
25

36
describes.fakeWin('#masterSelect', {}, (env) => {
47
it('should allow sharing between configured networks', () =>
58
expect(masterSelection(env.win, 'fake_network').name).to.equal(
69
'frame_fake_network_master'
710
));
811
});
12+
13+
describes.sandboxed('IntegrationAmpContext aliases', {}, (env) => {
14+
let context;
15+
16+
beforeEach(() => {
17+
// Create a minimal context object that implements the needed methods
18+
context = {
19+
master_: env.sandbox.stub().returns('master-window'),
20+
isMaster_: env.sandbox.stub().returns(true),
21+
computeInMasterFrame: env.sandbox.stub(),
22+
};
23+
24+
// Apply the getters from IntegrationAmpContext prototype
25+
Object.defineProperty(context, 'coordinator', {
26+
get() { return this.master_(); }
27+
});
28+
Object.defineProperty(context, 'isCoordinator', {
29+
get() { return this.isMaster_(); }
30+
});
31+
context.computeInCoordinatingFrame = function(global, taskId, work, cb) {
32+
return this.computeInMasterFrame(global, taskId, work, cb);
33+
};
34+
});
35+
36+
it('should delegate coordinator to master', () => {
37+
const result = context.coordinator;
38+
expect(context.master_).to.have.been.calledOnce;
39+
expect(result).to.equal('master-window');
40+
});
41+
42+
it('should delegate isCoordinator to isMaster', () => {
43+
const result = context.isCoordinator;
44+
expect(context.isMaster_).to.have.been.calledOnce;
45+
expect(result).to.equal(true);
46+
});
47+
48+
it('should delegate computeInCoordinatingFrame to computeInMasterFrame', () => {
49+
const global = {test: 'global'};
50+
const taskId = 'test-task';
51+
const work = () => {};
52+
const cb = () => {};
53+
54+
context.computeInCoordinatingFrame(global, taskId, work, cb);
55+
56+
expect(context.computeInMasterFrame).to.have.been.calledOnceWith(global, taskId, work, cb);
57+
});
58+
});

test/unit/test-3p.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
computeInCoordinatingFrame,
23
computeInMasterFrame,
34
loadScript,
45
nextTick,
@@ -265,6 +266,57 @@ describes.sandboxed('3p', {}, (env) => {
265266
expect(workCalls).to.equal(1);
266267
});
267268

269+
it('should do work only in coordinator (coordinatingFrame alias)', () => {
270+
const taskId = 'exampleId';
271+
const coordinator = {
272+
context: {
273+
isMaster: true,
274+
},
275+
};
276+
coordinator.context.master = coordinator;
277+
const client0 = {
278+
context: {
279+
isMaster: false,
280+
master: coordinator,
281+
},
282+
};
283+
const client1 = {
284+
context: {
285+
isMaster: false,
286+
master: coordinator,
287+
},
288+
};
289+
const client2 = {
290+
context: {
291+
isMaster: false,
292+
master: coordinator,
293+
},
294+
};
295+
let done;
296+
let workCalls = 0;
297+
const work = (d) => {
298+
workCalls++;
299+
done = d;
300+
};
301+
let progress = '';
302+
const frame = (id) => {
303+
return (result) => {
304+
progress += result + id;
305+
};
306+
};
307+
computeInCoordinatingFrame(client0, taskId, work, frame('client0'));
308+
expect(workCalls).to.equal(0);
309+
computeInCoordinatingFrame(coordinator, taskId, work, frame('coordinator'));
310+
expect(workCalls).to.equal(1);
311+
computeInCoordinatingFrame(client1, taskId, work, frame('client1'));
312+
expect(progress).to.equal('');
313+
done(';');
314+
expect(progress).to.equal(';client0;coordinator;client1');
315+
computeInCoordinatingFrame(client2, taskId, work, frame('client2'));
316+
expect(progress).to.equal(';client0;coordinator;client1;client2');
317+
expect(workCalls).to.equal(1);
318+
});
319+
268320
describe('loadScript', () => {
269321
it('should add <script /> with url to the body', () => {
270322
const url = 'http://test.com/example.js';

0 commit comments

Comments
 (0)