Commit 8575eb96 authored by Eduardo Sanz García's avatar Eduardo Sanz García Committed by Eduardo

Complete testing of the onDocumentReady function

I removed the `/* istanbul ignore test */` and created a facade to
emulate and iframe for testing.
parent 919e5bf1
...@@ -4,7 +4,8 @@ import { ...@@ -4,7 +4,8 @@ import {
onDocumentReady, onDocumentReady,
} from '../frame-observer'; } from '../frame-observer';
describe('FrameObserver', () => { describe('annotator/frame-observer.js', () => {
describe('FrameObserver', () => {
let container; let container;
let frameObserver; let frameObserver;
let onFrameAdded; let onFrameAdded;
...@@ -36,7 +37,11 @@ describe('FrameObserver', () => { ...@@ -36,7 +37,11 @@ describe('FrameObserver', () => {
document.body.appendChild(container); document.body.appendChild(container);
onFrameAdded = sandbox.stub(); onFrameAdded = sandbox.stub();
onFrameRemoved = sandbox.stub(); onFrameRemoved = sandbox.stub();
frameObserver = new FrameObserver(container, onFrameAdded, onFrameRemoved); frameObserver = new FrameObserver(
container,
onFrameAdded,
onFrameRemoved
);
sandbox.stub(console, 'warn'); sandbox.stub(console, 'warn');
}); });
...@@ -138,7 +143,52 @@ describe('FrameObserver', () => { ...@@ -138,7 +143,52 @@ describe('FrameObserver', () => {
await onDocumentReady(iframe); await onDocumentReady(iframe);
await waitForFrameObserver(); await waitForFrameObserver();
assert.notCalled(onFrameAdded); assert.notCalled(onFrameAdded);
assert.calledOnce(console.warn); assert.calledOnce(console.warn);
}); });
});
describe('onDocumentReady', () => {
let fakeIFrame;
let fakeIFrameDocument;
beforeEach(() => {
fakeIFrameDocument = {
addEventListener: sinon.stub(),
readyState: 'loading',
location: {
href: 'about:blank',
},
};
fakeIFrame = {
contentWindow: {
document: fakeIFrameDocument,
},
addEventListener: sinon.stub(),
hasAttribute: sinon.stub(),
};
});
it('waits for the iframe load event to be triggered if the document is blank', () => {
fakeIFrameDocument.location.href = 'about:blank';
fakeIFrame.hasAttribute.returns(true);
onDocumentReady(fakeIFrame);
assert.calledWith(fakeIFrame.addEventListener, 'load');
});
it('waits for the iframe DOMContentLoaded event to be triggered if the document is loading', () => {
fakeIFrameDocument.location.href = 'about:srcdoc';
fakeIFrame.hasAttribute.returns(false);
fakeIFrameDocument.readyState = 'loading';
onDocumentReady(fakeIFrame);
assert.calledWith(
fakeIFrameDocument.addEventListener,
'DOMContentLoaded'
);
});
});
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment