-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
test-ampdoc.js
725 lines (632 loc) · 22.2 KB
/
test-ampdoc.js
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as docready from '../../src/document-ready';
import * as dom from '../../src/dom';
import {
AmpDocFie,
AmpDocService,
AmpDocShadow,
AmpDocSingle,
} from '../../src/service/ampdoc-impl';
import {
ShadowDomVersion,
getShadowDomSupportedVersion,
isShadowDomSupported,
setShadowDomSupportedVersionForTesting,
} from '../../src/web-components';
import {createShadowRoot} from '../../src/shadow-embed';
import {setParentWindow} from '../../src/service';
import {toggleExperiment} from '../../src/experiments';
describe('AmpDocService', () => {
let sandbox;
beforeEach(() => {
sandbox = sinon.sandbox;
});
afterEach(() => {
sandbox.restore();
delete window.document['__AMPDOC'];
});
describe('single-doc mode', () => {
let service;
beforeEach(() => {
service = new AmpDocService(window, /* isSingleDoc */ true);
});
it('should initialize as single-doc', () => {
expect(service.isSingleDoc()).to.be.true;
expect(service.singleDoc_).to.exist;
expect(service.singleDoc_).to.be.instanceOf(AmpDocSingle);
});
it('should always yield the single document', () => {
expect(service.getAmpDoc(null)).to.equal(service.singleDoc_);
expect(service.getAmpDoc(document)).to.equal(service.singleDoc_);
const div = document.createElement('div');
document.body.appendChild(div);
expect(service.getAmpDoc(div)).to.equal(service.singleDoc_);
});
it('should yield the single doc when ampdoc-closest is enabled', () => {
toggleExperiment(window, 'ampdoc-closest', true);
service = new AmpDocService(window, /* isSingleDoc */ true);
expect(service.getAmpDoc(null)).to.equal(service.singleDoc_);
expect(service.getAmpDoc(document)).to.equal(service.singleDoc_);
const div = document.createElement('div');
document.body.appendChild(div);
expect(service.getAmpDoc(div)).to.equal(service.singleDoc_);
});
// For example, <amp-next-page> creates shadow documents in single-doc
// mode.
describe('shadow documents', () => {
let host;
let shadowRoot;
let content;
beforeEach(() => {
content = document.createElement('span');
host = document.createElement('div');
setShadowDomSupportedVersionForTesting(undefined);
if (isShadowDomSupported()) {
if (getShadowDomSupportedVersion() == ShadowDomVersion.V1) {
shadowRoot = host.attachShadow({mode: 'open'});
} else {
shadowRoot = host.createShadowRoot();
}
shadowRoot.appendChild(content);
}
document.body.appendChild(host);
});
afterEach(() => {
if (host.parentNode) {
host.parentNode.removeChild(host);
}
toggleExperiment(window, 'ampdoc-closest', false);
});
it('should yield the single doc', () => {
if (!shadowRoot) {
return;
}
service.installShadowDoc('https://a.org/', shadowRoot);
const ampDoc = service.getAmpDoc(content);
expect(ampDoc).to.equal(service.singleDoc_);
});
it('should yield the shadow doc when explicitly asked', () => {
if (!shadowRoot) {
return;
}
const newAmpDoc = service.installShadowDoc(
'https://a.org/',
shadowRoot
);
const ampDoc = service.getAmpDoc(content, {closestAmpDoc: true});
expect(ampDoc).to.equal(newAmpDoc);
});
it('should yield the shadow doc when ampdoc-closest is enabled', () => {
if (!shadowRoot) {
return;
}
toggleExperiment(window, 'ampdoc-closest', true);
service = new AmpDocService(window, /* isSingleDoc */ true);
const newAmpDoc = service.installShadowDoc(
'https://a.org/',
shadowRoot
);
const ampDoc = service.getAmpDoc(content);
expect(ampDoc).to.equal(newAmpDoc);
});
});
});
describe('shadow-doc mode', () => {
let service;
let host, shadowRoot, content;
beforeEach(() => {
service = new AmpDocService(window, /* isSingleDoc */ false);
content = document.createElement('span');
host = document.createElement('div');
setShadowDomSupportedVersionForTesting(undefined);
if (isShadowDomSupported()) {
if (getShadowDomSupportedVersion() == ShadowDomVersion.V1) {
shadowRoot = host.attachShadow({mode: 'open'});
} else {
shadowRoot = host.createShadowRoot();
}
shadowRoot.appendChild(content);
}
document.body.appendChild(host);
});
afterEach(() => {
if (host.parentNode) {
host.parentNode.removeChild(host);
}
});
it('should not initialize as single-doc', () => {
expect(service.isSingleDoc()).to.be.false;
expect(service.singleDoc_).to.not.exist;
});
it('should yield custom-element shadow-doc when exists', () => {
const ampDoc = {};
content.ampdoc_ = ampDoc;
host.appendChild(content);
expect(service.getAmpDoc(content)).to.equal(ampDoc);
});
it('should yield cached or custom-element shadow-doc when exists', () => {
if (!shadowRoot) {
return;
}
const ampDoc = {};
shadowRoot['__AMPDOC'] = ampDoc;
expect(service.getAmpDoc(content)).to.equal(ampDoc);
expect(service.getAmpDoc(shadowRoot)).to.equal(ampDoc);
// Override via custom element.
const ampDoc2 = {};
content.ampdoc_ = ampDoc2;
expect(service.getAmpDoc(content)).to.equal(ampDoc2);
// Fallback to cached version when custom element returns null.
content.ampdoc_ = null;
expect(service.getAmpDoc(content)).to.equal(ampDoc);
});
it('should create and cache shadow-doc', () => {
if (!shadowRoot) {
return;
}
expect(() => {
service.getAmpDoc(content);
}).to.throw(/No ampdoc found/);
const newAmpDoc = service.installShadowDoc('https://a.org/', shadowRoot);
const ampDoc = service.getAmpDoc(content);
expect(ampDoc).to.equal(newAmpDoc);
expect(ampDoc).to.exist;
expect(service.getAmpDoc(shadowRoot)).to.equal(ampDoc);
expect(ampDoc).to.be.instanceOf(AmpDocShadow);
expect(ampDoc.shadowRoot_).to.equal(shadowRoot);
expect(shadowRoot['__AMPDOC']).to.equal(ampDoc);
});
it('should fail if shadow root not found', () => {
if (!shadowRoot) {
return;
}
expect(() => {
service.getAmpDoc(host);
}).to.throw(/No ampdoc found/);
});
it('should allow checking for an AmpDoc for an external node', () => {
if (!shadowRoot) {
return;
}
const ampDoc = service.getAmpDocIfAvailable(host);
expect(ampDoc).to.be.null;
});
it('should fail to install shadow doc twice', () => {
if (!shadowRoot) {
return;
}
service.installShadowDoc('https://a.org/', shadowRoot);
allowConsoleError(() => {
expect(() => {
service.installShadowDoc('https://a.org/', shadowRoot);
}).to.throw(/The shadow root already contains ampdoc/);
});
});
// TODO(dvoytenko, #11827): Make this test work on Safari.
it.configure()
.skipSafari()
.skipFirefox()
.run('should navigate via host', () => {
if (!shadowRoot) {
return;
}
const newAmpDoc = service.installShadowDoc(
'https://a.org/',
shadowRoot
);
const ampDoc = service.getAmpDoc(content);
expect(ampDoc).to.equal(newAmpDoc);
const content2 = document.createElement('span');
const host2 = document.createElement('div');
const shadowRoot2 = host2.createShadowRoot();
shadowRoot2.appendChild(content2);
shadowRoot.appendChild(host2);
expect(content2.parentNode).to.equal(shadowRoot2);
expect(shadowRoot2.host).to.equal(host2);
expect(host2.shadowRoot).to.equal(shadowRoot2);
expect(host2.parentNode).to.equal(shadowRoot);
expect(service.getAmpDoc(host2)).to.equal(ampDoc);
expect(service.getAmpDoc(content2)).to.equal(ampDoc);
expect(service.getAmpDoc(shadowRoot2)).to.equal(ampDoc);
});
});
describe('fie-doc mode', () => {
let service;
let host, shadowRoot, content;
beforeEach(() => {
toggleExperiment(window, 'ampdoc-fie', true);
service = new AmpDocService(window, /* isSingleDoc */ true);
content = document.createElement('span');
host = document.createElement('div');
setShadowDomSupportedVersionForTesting(undefined);
if (isShadowDomSupported()) {
if (getShadowDomSupportedVersion() == ShadowDomVersion.V1) {
shadowRoot = host.attachShadow({mode: 'open'});
} else {
shadowRoot = host.createShadowRoot();
}
shadowRoot.appendChild(content);
}
document.body.appendChild(host);
});
afterEach(() => {
toggleExperiment(window, 'ampdoc-fie', false);
if (host.parentNode) {
host.parentNode.removeChild(host);
}
});
it('should initialize as single-doc', () => {
expect(service.isSingleDoc()).to.be.true;
expect(service.singleDoc_).to.exist;
});
it('should yield custom-element doc when exists', () => {
const ampDoc = {};
content.ampdoc_ = ampDoc;
host.appendChild(content);
expect(service.getAmpDoc(content)).to.equal(ampDoc);
});
it('should yield cached or custom-element shadow-doc when exists', () => {
if (!shadowRoot) {
return;
}
const ampDoc = {};
shadowRoot['__AMPDOC'] = ampDoc;
expect(service.getAmpDoc(content)).to.equal(ampDoc);
expect(service.getAmpDoc(shadowRoot)).to.equal(ampDoc);
// Override via custom element.
const ampDoc2 = {};
content.ampdoc_ = ampDoc2;
expect(service.getAmpDoc(content)).to.equal(ampDoc2);
// Fallback to cached version when custom element returns null.
content.ampdoc_ = null;
expect(service.getAmpDoc(content)).to.equal(ampDoc);
});
it('should create and cache shadow-doc', () => {
if (!shadowRoot) {
return;
}
const newAmpDoc = service.installShadowDoc('https://a.org/', shadowRoot);
const ampDoc = service.getAmpDoc(content);
expect(ampDoc).to.equal(newAmpDoc);
expect(ampDoc).to.exist;
expect(service.getAmpDoc(shadowRoot)).to.equal(ampDoc);
expect(ampDoc).to.be.instanceOf(AmpDocShadow);
expect(ampDoc.shadowRoot_).to.equal(shadowRoot);
expect(shadowRoot['__AMPDOC']).to.equal(ampDoc);
});
it('should navigate to parent if shadow root not found', () => {
if (!shadowRoot) {
return;
}
expect(service.getAmpDoc(host)).to.equal(service.singleDoc_);
});
it('should fail to install shadow doc twice', () => {
if (!shadowRoot) {
return;
}
service.installShadowDoc('https://a.org/', shadowRoot);
allowConsoleError(() => {
expect(() => {
service.installShadowDoc('https://a.org/', shadowRoot);
}).to.throw(/The shadow root already contains ampdoc/);
});
});
// TODO(dvoytenko, #11827): Make this test work on Safari.
it.configure()
.skipSafari()
.skipFirefox()
.run('should navigate via host', () => {
if (!shadowRoot) {
return;
}
const newAmpDoc = service.installShadowDoc(
'https://a.org/',
shadowRoot
);
const ampDoc = service.getAmpDoc(content);
expect(ampDoc).to.equal(newAmpDoc);
const content2 = document.createElement('span');
const host2 = document.createElement('div');
const shadowRoot2 = host2.createShadowRoot();
shadowRoot2.appendChild(content2);
shadowRoot.appendChild(host2);
expect(content2.parentNode).to.equal(shadowRoot2);
expect(shadowRoot2.host).to.equal(host2);
expect(host2.shadowRoot).to.equal(shadowRoot2);
expect(host2.parentNode).to.equal(shadowRoot);
expect(service.getAmpDoc(host2)).to.equal(ampDoc);
expect(service.getAmpDoc(content2)).to.equal(ampDoc);
expect(service.getAmpDoc(shadowRoot2)).to.equal(ampDoc);
});
describes.realWin('fie-doc', {}, env => {
let childWin;
beforeEach(() => {
childWin = env.win;
setParentWindow(childWin, service.win);
});
it('should create and cache fie-doc', () => {
const newAmpDoc = service.installFieDoc('https://a.org/', childWin);
const content = document.createElement('div');
childWin.document.body.appendChild(content);
const ampDoc = service.getAmpDoc(content);
expect(ampDoc).to.equal(newAmpDoc);
expect(ampDoc.getParent()).to.equal(service.singleDoc_);
expect(childWin.document['__AMPDOC']).to.equal(ampDoc);
});
});
});
});
describe('AmpDocSingle', () => {
let sandbox;
let ampdoc;
beforeEach(() => {
sandbox = sinon.sandbox;
ampdoc = new AmpDocSingle(window);
});
afterEach(() => {
sandbox.restore();
});
it('should return window', () => {
expect(ampdoc.win).to.equal(window);
expect(ampdoc.getUrl()).to.equal(window.location.href);
});
it('should return document as root', () => {
expect(ampdoc.getRootNode()).to.equal(window.document);
expect(ampdoc.getHeadNode()).to.equal(window.document.head);
expect(ampdoc.isSingleDoc()).to.be.true;
});
it('should find element by id', () => {
const id = 'ampdoc_test_element_' + Date.now();
const element = document.createElement('div');
element.setAttribute('id', id);
document.body.appendChild(element);
expect(ampdoc.getElementById(id)).to.equal(element);
});
it('should initialize ready state and body immediately', () => {
expect(ampdoc.getBody()).to.equal(window.document.body);
expect(ampdoc.isBodyAvailable()).to.be.true;
expect(ampdoc.isReady()).to.be.true;
return Promise.all([ampdoc.waitForBodyOpen(), ampdoc.whenReady()]).then(
results => {
expect(results[0]).to.equal(window.document.body);
expect(ampdoc.getBody()).to.equal(window.document.body);
expect(ampdoc.isBodyAvailable()).to.be.true;
expect(ampdoc.isReady()).to.be.true;
}
);
});
it('should wait for body and ready state', () => {
const doc = {body: null};
const win = {document: doc};
let bodyCallback;
sandbox.stub(dom, 'waitForBodyOpenPromise').callsFake(() => {
return new Promise(resolve => {
bodyCallback = resolve;
});
});
let ready = false;
sandbox.stub(docready, 'isDocumentReady').callsFake(() => {
return ready;
});
let readyCallback;
sandbox.stub(docready, 'whenDocumentReady').callsFake(() => {
return new Promise(resolve => {
readyCallback = resolve;
});
});
const ampdoc = new AmpDocSingle(win);
expect(ampdoc.isBodyAvailable()).to.be.false;
allowConsoleError(() => {
expect(() => ampdoc.getBody()).to.throw(/body not available/);
});
const bodyPromise = ampdoc.waitForBodyOpen();
const readyPromise = ampdoc.whenReady();
doc.body = {nodeType: 1};
bodyCallback();
ready = true;
readyCallback();
expect(ampdoc.isBodyAvailable()).to.be.true;
expect(ampdoc.getBody()).to.equal(doc.body);
expect(ampdoc.isReady()).to.be.true;
return Promise.all([bodyPromise, readyPromise]).then(results => {
expect(results[0]).to.equal(doc.body);
expect(ampdoc.isBodyAvailable()).to.be.true;
expect(ampdoc.getBody()).to.equal(doc.body);
expect(ampdoc.isReady()).to.be.true;
});
});
it('should declare extension', () => {
expect(ampdoc.declaresExtension('ext1')).to.be.false;
expect(ampdoc.declaresExtension('ext2')).to.be.false;
ampdoc.declareExtension('ext1');
expect(ampdoc.declaresExtension('ext1')).to.be.true;
expect(ampdoc.declaresExtension('ext2')).to.be.false;
ampdoc.declareExtension('ext2');
expect(ampdoc.declaresExtension('ext1')).to.be.true;
expect(ampdoc.declaresExtension('ext2')).to.be.true;
});
it('should ignore duplicate extensions', () => {
expect(ampdoc.declaresExtension('ext1')).to.be.false;
ampdoc.declareExtension('ext1');
expect(ampdoc.declaresExtension('ext1')).to.be.true;
expect(ampdoc.declaredExtensions_).to.have.length(1);
// Repeat.
ampdoc.declareExtension('ext1');
expect(ampdoc.declaredExtensions_).to.have.length(1);
expect(ampdoc.declaresExtension('ext1')).to.be.true;
});
});
describe('AmpDocShadow', () => {
const URL = 'https://example.org/document';
let sandbox;
let content, host, shadowRoot;
let ampdoc;
beforeEach(() => {
sandbox = sinon.sandbox;
content = document.createElement('div');
host = document.createElement('div');
shadowRoot = createShadowRoot(host);
shadowRoot.appendChild(content);
ampdoc = new AmpDocShadow(window, URL, shadowRoot);
});
afterEach(() => {
sandbox.restore();
});
it('should return window', () => {
if (!ampdoc) {
return;
}
expect(ampdoc.win).to.equal(window);
expect(ampdoc.isSingleDoc()).to.be.false;
expect(ampdoc.getUrl()).to.equal(URL);
});
it('should return document as root', () => {
if (!ampdoc) {
return;
}
expect(ampdoc.getRootNode()).to.equal(shadowRoot);
expect(ampdoc.getHeadNode()).to.equal(shadowRoot);
});
it('should find element by id', () => {
if (!ampdoc) {
return;
}
const id = 'ampdoc_test_element_' + Date.now();
const element = document.createElement('div');
element.setAttribute('id', id);
content.appendChild(element);
expect(ampdoc.getElementById(id)).to.equal(element);
});
it('should update when body is available', () => {
// Body is still expected.
expect(ampdoc.isBodyAvailable()).to.be.false;
allowConsoleError(() => {
expect(() => ampdoc.getBody()).to.throw(/body not available/);
});
expect(ampdoc.bodyResolver_).to.be.ok;
// Set body.
const bodyPromise = ampdoc.waitForBodyOpen();
const body = {nodeType: 1};
ampdoc.setBody(body);
expect(ampdoc.isBodyAvailable()).to.be.true;
expect(ampdoc.getBody()).to.equal(body);
expect(ampdoc.bodyResolver_).to.be.undefined;
expect(ampdoc.bodyPromise_).to.be.ok;
return bodyPromise.then(() => {
expect(ampdoc.isBodyAvailable()).to.be.true;
expect(ampdoc.getBody()).to.equal(body);
});
});
it('should only allow one body update', () => {
const body = {nodeType: 1};
ampdoc.setBody(body);
allowConsoleError(() => {
expect(() => {
ampdoc.setBody(body);
}).to.throw(/Duplicate body/);
});
});
it('should update when doc is ready', () => {
// "Ready" is still expected.
expect(ampdoc.isReady()).to.be.false;
expect(ampdoc.readyResolver_).to.be.ok;
// Set ready.
const readyPromise = ampdoc.whenReady();
ampdoc.setReady();
expect(ampdoc.isReady()).to.be.true;
expect(ampdoc.readyResolver_).to.be.undefined;
expect(ampdoc.readyPromise_).to.be.ok;
return readyPromise.then(() => {
expect(ampdoc.isReady()).to.be.true;
});
});
it('should only allow one ready update', () => {
ampdoc.setReady();
allowConsoleError(() => {
expect(() => {
ampdoc.setReady();
}).to.throw(/Duplicate ready state/);
});
});
});
describes.realWin('AmpDocFie', {}, env => {
const URL = 'https://example.org/document';
let service;
let childWin;
let parent;
let ampdoc;
beforeEach(() => {
service = new AmpDocService(window, /* isSingleDoc */ true);
childWin = env.win;
setParentWindow(childWin, window);
parent = service.singleDoc_;
ampdoc = new AmpDocFie(childWin, URL, parent);
});
it('should create AmpDocFie', () => {
const ampdoc = service.installFieDoc(URL, childWin);
expect(ampdoc.win).to.equal(childWin);
expect(ampdoc.isSingleDoc()).to.be.false;
expect(ampdoc.getUrl()).to.equal(URL);
expect(ampdoc.getParent()).to.equal(parent);
});
it('should return window', () => {
expect(ampdoc.win).to.equal(childWin);
expect(ampdoc.isSingleDoc()).to.be.false;
expect(ampdoc.getUrl()).to.equal(URL);
expect(ampdoc.getParent()).to.equal(parent);
});
it('should return document as root', () => {
expect(ampdoc.getRootNode()).to.equal(childWin.document);
expect(ampdoc.getHeadNode()).to.equal(childWin.document.head);
});
it('should find element by id', () => {
const id = 'ampdoc_test_element_' + Date.now();
const element = document.createElement('div');
element.setAttribute('id', id);
childWin.document.body.appendChild(element);
expect(ampdoc.getElementById(id)).to.equal(element);
});
it('should update when body is available', () => {
expect(ampdoc.isBodyAvailable()).to.be.true;
expect(ampdoc.getBody()).to.equal(childWin.document.body);
return ampdoc.waitForBodyOpen().then(body => {
expect(body).to.equal(childWin.document.body);
});
});
it('should update when doc is ready', () => {
// "Ready" is still expected.
expect(ampdoc.isReady()).to.be.false;
expect(ampdoc.readyResolver_).to.be.ok;
// Set ready.
const readyPromise = ampdoc.whenReady();
ampdoc.setReady();
expect(ampdoc.isReady()).to.be.true;
expect(ampdoc.readyResolver_).to.be.undefined;
expect(ampdoc.readyPromise_).to.be.ok;
return readyPromise.then(() => {
expect(ampdoc.isReady()).to.be.true;
});
});
it('should only allow one ready update', () => {
ampdoc.setReady();
allowConsoleError(() => {
expect(() => {
ampdoc.setReady();
}).to.throw(/Duplicate ready state/);
});
});
});