MCPcopy Create free account
hub / github.com/ampproject/amphtml / doNotLoadExternalResourcesInTest

Function doNotLoadExternalResourcesInTest

testing/iframe.js:551–590  ·  view source on GitHub ↗
(win, sandbox)

Source from the content-addressed store, hash-verified

549 * @param {!Object} sandbox
550 */
551export function doNotLoadExternalResourcesInTest(win, sandbox) {
552 const {document} = win;
553 const {prototype} = win.Document;
554 const {createElement} = prototype;
555 sandbox.stub(prototype, 'createElement').callsFake(function (tagName) {
556 const element = createElement.apply(document, arguments);
557 tagName = tagName.toLowerCase();
558 if (tagName == 'iframe' || tagName == 'img') {
559 // Make get/set write to a fake property instead of
560 // triggering invocation.
561 element.fakeSrc = '';
562 Object.defineProperty(element, 'src', {
563 set: function (val) {
564 this.fakeSrc = val;
565 },
566 get: function () {
567 return this.fakeSrc;
568 },
569 });
570 // Triggers a load event on the element in the next micro task.
571 element.triggerLoad = function () {
572 const e = new Event('load');
573 Promise.resolve().then(() => {
574 this.dispatchEvent(e);
575 });
576 };
577 // Triggers an error event on the element in the next micro task.
578 element.triggerError = function () {
579 const e = new Event('error');
580 Promise.resolve().then(() => {
581 this.dispatchEvent(e);
582 });
583 };
584 if (tagName == 'iframe') {
585 element.srcdoc = '<h1>Fake iframe</h1>';
586 }
587 }
588 return element;
589 });
590}
591
592/**
593 * Returns a promise for when an element has been added to the given

Calls 3

applyMethod · 0.80
resolveMethod · 0.80
thenMethod · 0.45

Tested by

no test coverage detected