Skip to content

Commit a198e0c

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 a198e0c

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

test/unit/3p/test-ampcontext-integration.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,59 @@ describes.fakeWin('#masterSelect', {}, (env) => {
66
'frame_fake_network_master'
77
));
88
});
9+
10+
describes.sandboxed('IntegrationAmpContext aliases', {}, (env) => {
11+
let context;
12+
13+
beforeEach(() => {
14+
// Create a minimal context object that implements the needed methods
15+
context = {
16+
master_: env.sandbox.stub().returns('master-window'),
17+
isMaster_: env.sandbox.stub().returns(true),
18+
computeInMasterFrame: env.sandbox.stub(),
19+
};
20+
21+
// Apply the getters from IntegrationAmpContext prototype
22+
Object.defineProperty(context, 'coordinator', {
23+
get() {
24+
return this.master_();
25+
},
26+
});
27+
Object.defineProperty(context, 'isCoordinator', {
28+
get() {
29+
return this.isMaster_();
30+
},
31+
});
32+
context.computeInCoordinatingFrame = function (global, taskId, work, cb) {
33+
return this.computeInMasterFrame(global, taskId, work, cb);
34+
};
35+
});
36+
37+
it('should delegate coordinator to master', () => {
38+
const result = context.coordinator;
39+
expect(context.master_).to.have.been.calledOnce;
40+
expect(result).to.equal('master-window');
41+
});
42+
43+
it('should delegate isCoordinator to isMaster', () => {
44+
const result = context.isCoordinator;
45+
expect(context.isMaster_).to.have.been.calledOnce;
46+
expect(result).to.equal(true);
47+
});
48+
49+
it('should delegate computeInCoordinatingFrame to computeInMasterFrame', () => {
50+
const global = {test: 'global'};
51+
const taskId = 'test-task';
52+
const work = () => {};
53+
const cb = () => {};
54+
55+
context.computeInCoordinatingFrame(global, taskId, work, cb);
56+
57+
expect(context.computeInMasterFrame).to.have.been.calledOnceWith(
58+
global,
59+
taskId,
60+
work,
61+
cb
62+
);
63+
});
64+
});

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)