Commit b52f81ff authored by Robert Knight's avatar Robert Knight

Fix error when VitalSource integration injects client into content frame

Construction of the integration was recently moved to happen earlier in the
Guest.  This caused a regression in the VitalSource integration, which may
synchronously call `Guest#injectClient` when constructed. `injectClient` in turn
relies on `_hypothesisInjector` being initialized, which was not the case.

The error could be seen in the dev server's VS test case at
http://localhost:3000/document/vitalsource-epub.

Fix the issue by moving `_hypothesisInjector` initialization to happen earlier
in the Guest constructor, before `createIntegration` is called.
parent d9966ae4
......@@ -176,6 +176,10 @@ export default class Guest {
source: 'guest',
});
// Set up automatic and integration-triggered injection of client into
// iframes in this frame.
this._hypothesisInjector = new HypothesisInjector(this.element, config);
/**
* Integration that handles document-type specific functionality in the
* guest.
......@@ -198,10 +202,6 @@ export default class Guest {
this._sidebarRPC = new PortRPC();
this._connectSidebar();
// Set up automatic and integration-triggered injection of client into
// iframes in this frame.
this._hypothesisInjector = new HypothesisInjector(this.element, config);
this._bucketBarClient =
this._frameIdentifier === null
? new BucketBarClient({
......
......@@ -37,6 +37,7 @@ describe('Guest', () => {
let FakeBucketBarClient;
let fakeBucketBarClient;
let fakeCreateIntegration;
let FakePortRPC;
let fakePortRPCs;
let fakeIntegration;
......@@ -134,6 +135,8 @@ describe('Guest', () => {
uri: sinon.stub().resolves('https://example.com/test.pdf'),
};
fakeCreateIntegration = sinon.stub().returns(fakeIntegration);
fakeHypothesisInjector = {
destroy: sinon.stub(),
injectClient: sinon.stub().resolves(),
......@@ -165,7 +168,7 @@ describe('Guest', () => {
BucketBarClient: FakeBucketBarClient,
},
'./integrations': {
createIntegration: sinon.stub().returns(fakeIntegration),
createIntegration: fakeCreateIntegration,
},
'./highlighter': highlighter,
'./hypothesis-injector': { HypothesisInjector: FakeHypothesisInjector },
......@@ -1330,5 +1333,21 @@ describe('Guest', () => {
assert.calledWith(FakeHypothesisInjector, guest.element, config);
assert.calledWith(fakeHypothesisInjector.injectClient, frame);
});
it('can be called by the integration when created', async () => {
const config = {};
const frame = {};
// Simulate the integration injecting the client into pre-existing content
// frames when created. The VitalSource integration does this for example.
fakeCreateIntegration.callsFake(annotator => {
annotator.injectClient(frame);
return fakeIntegration;
});
const guest = createGuest({});
assert.calledWith(FakeHypothesisInjector, guest.element, config);
assert.calledWith(fakeHypothesisInjector.injectClient, frame);
});
});
});
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