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

Fix tests with new method signatures

Following the removal of `AnnotationSync` from `Guest` we correct the
tests.
parent e63a0be7
...@@ -37,8 +37,6 @@ describe('Guest', () => { ...@@ -37,8 +37,6 @@ describe('Guest', () => {
let notifySelectionChanged; let notifySelectionChanged;
let rangeUtil; let rangeUtil;
let FakeAnnotationSync;
let fakeAnnotationSync;
let fakeBridge; let fakeBridge;
let fakeIntegration; let fakeIntegration;
let FakeHypothesisInjector; let FakeHypothesisInjector;
...@@ -75,12 +73,6 @@ describe('Guest', () => { ...@@ -75,12 +73,6 @@ describe('Guest', () => {
FakeAdder.instance = null; FakeAdder.instance = null;
fakeAnnotationSync = {
destroy: sinon.stub(),
sync: sinon.stub(),
};
FakeAnnotationSync = sinon.stub().returns(fakeAnnotationSync);
fakeBridge = { fakeBridge = {
call: sinon.stub(), call: sinon.stub(),
createChannel: sinon.stub(), createChannel: sinon.stub(),
...@@ -130,7 +122,6 @@ describe('Guest', () => { ...@@ -130,7 +122,6 @@ describe('Guest', () => {
'./anchoring/text-range': { './anchoring/text-range': {
TextRange: FakeTextRange, TextRange: FakeTextRange,
}, },
'./annotation-sync': { AnnotationSync: FakeAnnotationSync },
'./integrations': { './integrations': {
createIntegration: sinon.stub().returns(fakeIntegration), createIntegration: sinon.stub().returns(fakeIntegration),
}, },
...@@ -149,12 +140,7 @@ describe('Guest', () => { ...@@ -149,12 +140,7 @@ describe('Guest', () => {
$imports.$restore(); $imports.$restore();
}); });
describe('communication with sidebar', () => { describe('communication with sidebar component', () => {
it('provides an event bus for the annotation sync module', () => {
createGuest();
assert.deepEqual(FakeAnnotationSync.lastCall.args[0], eventBus);
});
describe('event subscription', () => { describe('event subscription', () => {
let emitter; let emitter;
let guest; let guest;
...@@ -192,24 +178,6 @@ describe('Guest', () => { ...@@ -192,24 +178,6 @@ describe('Guest', () => {
emitter = eventBus.createEmitter(); emitter = eventBus.createEmitter();
}); });
it('detaches annotations on "annotationDeleted"', () => {
const ann = { id: 1, $tag: 'tag1' };
sandbox.stub(guest, 'detach');
emitter.publish('annotationDeleted', ann);
assert.calledOnce(guest.detach);
assert.calledWith(guest.detach, ann);
});
it('anchors annotations on "annotationsLoaded"', () => {
const ann1 = { id: 1, $tag: 'tag1' };
const ann2 = { id: 2, $tag: 'tag2' };
sandbox.stub(guest, 'anchor');
emitter.publish('annotationsLoaded', [ann1, ann2]);
assert.calledTwice(guest.anchor);
assert.calledWith(guest.anchor, ann1);
assert.calledWith(guest.anchor, ann2);
});
it('proxies all other events into the annotator event system', () => { it('proxies all other events into the annotator event system', () => {
const fooHandler = sandbox.stub(); const fooHandler = sandbox.stub();
const barHandler = sandbox.stub(); const barHandler = sandbox.stub();
...@@ -226,7 +194,7 @@ describe('Guest', () => { ...@@ -226,7 +194,7 @@ describe('Guest', () => {
}); });
}); });
describe('events from sidebar', () => { describe('events from sidebar frame', () => {
const emitSidebarEvent = (event, ...args) => { const emitSidebarEvent = (event, ...args) => {
for (let [evt, fn] of fakeBridge.on.args) { for (let [evt, fn] of fakeBridge.on.args) {
if (event === evt) { if (event === evt) {
...@@ -427,6 +395,34 @@ describe('Guest', () => { ...@@ -427,6 +395,34 @@ describe('Guest', () => {
); );
}); });
}); });
describe('on "loadAnnotations" event', () => {
it('anchors annotations', () => {
const guest = createGuest();
const ann1 = { id: 1, $tag: 'tag1' };
const ann2 = { id: 2, $tag: 'tag2' };
sandbox.stub(guest, 'anchor');
emitSidebarEvent('loadAnnotations', [ann1, ann2]);
assert.calledTwice(guest.anchor);
assert.calledWith(guest.anchor, ann1);
assert.calledWith(guest.anchor, ann2);
});
});
describe('on "deleteAnnotation" event', () => {
it('detaches annotation', () => {
const guest = createGuest();
const $tag = 'tag1';
sandbox.stub(guest, 'detach');
emitSidebarEvent('deleteAnnotation', $tag);
assert.calledOnce(guest.detach);
assert.calledWith(guest.detach, $tag);
});
});
}); });
describe('document events', () => { describe('document events', () => {
...@@ -649,23 +645,23 @@ describe('Guest', () => { ...@@ -649,23 +645,23 @@ describe('Guest', () => {
// nb. Detailed tests for properties of new annotations are in the // nb. Detailed tests for properties of new annotations are in the
// `createAnnotation` tests. // `createAnnotation` tests.
it('creates a new annotation if "Annotate" is clicked', async () => { it('creates a new annotation if "Annotate" is clicked', async () => {
const guest = createGuest(); createGuest();
const callback = sandbox.stub();
guest._emitter.subscribe('beforeAnnotationCreated', callback);
await FakeAdder.instance.options.onAnnotate(); await FakeAdder.instance.options.onAnnotate();
assert.called(callback); assert.calledWith(fakeBridge.call, 'createAnnotation');
}); });
it('creates a new highlight if "Highlight" is clicked', async () => { it('creates a new highlight if "Highlight" is clicked', async () => {
const guest = createGuest(); createGuest();
const callback = sandbox.stub();
guest._emitter.subscribe('beforeAnnotationCreated', callback);
await FakeAdder.instance.options.onHighlight(); await FakeAdder.instance.options.onHighlight();
assert.calledWith(callback, sinon.match({ $highlight: true })); assert.calledWith(
fakeBridge.call,
'createAnnotation',
sinon.match({ $highlight: true })
);
}); });
it('shows annotations if "Show" is clicked', () => { it('shows annotations if "Show" is clicked', () => {
...@@ -788,10 +784,10 @@ describe('Guest', () => { ...@@ -788,10 +784,10 @@ describe('Guest', () => {
]); ]);
}); });
it('sets `$tag` to a falsey value', async () => { it('sets `$tag` to a temporarily value', async () => {
const guest = createGuest(); const guest = createGuest();
const annotation = await guest.createAnnotation(); const annotation = await guest.createAnnotation();
assert.notOk(annotation.$tag); assert.match(annotation.$tag, /a:\w{8}/);
}); });
it('sets falsey `$highlight` if `highlight` is false', async () => { it('sets falsey `$highlight` if `highlight` is false', async () => {
...@@ -806,14 +802,12 @@ describe('Guest', () => { ...@@ -806,14 +802,12 @@ describe('Guest', () => {
assert.equal(annotation.$highlight, true); assert.equal(annotation.$highlight, true);
}); });
it('triggers a "beforeAnnotationCreated" event', async () => { it('triggers a "createAnnotation" event', async () => {
const guest = createGuest(); const guest = createGuest();
const callback = sandbox.stub();
guest._emitter.subscribe('beforeAnnotationCreated', callback);
const annotation = await guest.createAnnotation(); const annotation = await guest.createAnnotation();
assert.calledWith(callback, annotation); assert.calledWith(fakeBridge.call, 'createAnnotation', annotation);
}); });
}); });
...@@ -958,7 +952,7 @@ describe('Guest', () => { ...@@ -958,7 +952,7 @@ describe('Guest', () => {
const guest = createGuest(); const guest = createGuest();
const annotation = {}; const annotation = {};
return guest.anchor(annotation).then(() => { return guest.anchor(annotation).then(() => {
assert.called(fakeAnnotationSync.sync); assert.calledWith(fakeBridge.call, 'syncAnchoringStatus', annotation);
}); });
}); });
...@@ -1047,16 +1041,20 @@ describe('Guest', () => { ...@@ -1047,16 +1041,20 @@ describe('Guest', () => {
}); });
describe('#detach', () => { describe('#detach', () => {
let index = 1;
function createAnchor() { function createAnchor() {
return { annotation: {}, highlights: [document.createElement('span')] }; return {
annotation: { $tag: `t${index++}` },
highlights: [document.createElement('span')],
};
} }
it('removes anchors associated with the removed annotation', () => { it('removes anchors associated with the removed annotation', () => {
const guest = createGuest(); const guest = createGuest();
const annotation = {}; const annotation = { $tag: 't0' };
guest.anchors.push({ annotation }); guest.anchors.push({ annotation });
guest.detach(annotation); guest.detach('t0');
assert.equal(guest.anchors.length, 0); assert.equal(guest.anchors.length, 0);
}); });
...@@ -1067,7 +1065,7 @@ describe('Guest', () => { ...@@ -1067,7 +1065,7 @@ describe('Guest', () => {
const { removeHighlights } = highlighter; const { removeHighlights } = highlighter;
guest.anchors.push(anchor); guest.anchors.push(anchor);
guest.detach(anchor.annotation); guest.detach(anchor.annotation.$tag);
assert.calledOnce(removeHighlights); assert.calledOnce(removeHighlights);
assert.calledWith(removeHighlights, anchor.highlights); assert.calledWith(removeHighlights, anchor.highlights);
...@@ -1079,7 +1077,7 @@ describe('Guest', () => { ...@@ -1079,7 +1077,7 @@ describe('Guest', () => {
const anchorB = createAnchor(); const anchorB = createAnchor();
guest.anchors.push(anchorA, anchorB); guest.anchors.push(anchorA, anchorB);
guest.detach(anchorA.annotation); guest.detach(anchorA.annotation.$tag);
assert.include(guest.anchors, anchorB); assert.include(guest.anchors, anchorB);
assert.isFalse( assert.isFalse(
...@@ -1093,7 +1091,7 @@ describe('Guest', () => { ...@@ -1093,7 +1091,7 @@ describe('Guest', () => {
const anchorsChanged = sandbox.stub(); const anchorsChanged = sandbox.stub();
guest._emitter.subscribe('anchorsChanged', anchorsChanged); guest._emitter.subscribe('anchorsChanged', anchorsChanged);
guest.detach(anchor.annotation); guest.detach(anchor.annotation.$tag);
assert.calledWith(anchorsChanged, guest.anchors); assert.calledWith(anchorsChanged, guest.anchors);
}); });
...@@ -1129,7 +1127,6 @@ describe('Guest', () => { ...@@ -1129,7 +1127,6 @@ describe('Guest', () => {
const guest = createGuest(); const guest = createGuest();
guest.destroy(); guest.destroy();
assert.called(fakeBridge.destroy); assert.called(fakeBridge.destroy);
assert.called(fakeAnnotationSync.destroy);
}); });
it('notifies host frame that guest has been unloaded', () => { it('notifies host frame that guest has been unloaded', () => {
......
...@@ -275,20 +275,17 @@ describe('FrameSyncService', () => { ...@@ -275,20 +275,17 @@ describe('FrameSyncService', () => {
context('when annotations are removed from the sidebar', () => { context('when annotations are removed from the sidebar', () => {
it('sends a "deleteAnnotation" message to the frame', () => { it('sends a "deleteAnnotation" message to the frame', () => {
const ann = fixtures.ann;
frameSync.connect(); frameSync.connect();
fakeStore.setState({ fakeStore.setState({
annotations: [fixtures.ann], annotations: [ann],
}); });
fakeStore.setState({ fakeStore.setState({
annotations: [], annotations: [],
}); });
assert.calledWithMatch( assert.calledWithMatch(guestBridge().call, 'deleteAnnotation', ann.$tag);
guestBridge().call,
'deleteAnnotation',
sinon.match(formatAnnot(fixtures.ann))
);
}); });
}); });
...@@ -296,9 +293,8 @@ describe('FrameSyncService', () => { ...@@ -296,9 +293,8 @@ describe('FrameSyncService', () => {
it('makes the new highlight visible in the frame', () => { it('makes the new highlight visible in the frame', () => {
frameSync.connect(); frameSync.connect();
fakeStore.isLoggedIn.returns(true); fakeStore.isLoggedIn.returns(true);
const ann = { target: [] };
guestBridge().emit('createAnnotation', { tag: 't1', msg: ann }); guestBridge().emit('createAnnotation', { $tag: 't1', target: [] });
assert.calledWith(hostBridge().call, 'showHighlights'); assert.calledWith(hostBridge().call, 'showHighlights');
}); });
...@@ -307,25 +303,18 @@ describe('FrameSyncService', () => { ...@@ -307,25 +303,18 @@ describe('FrameSyncService', () => {
it('creates the annotation in the sidebar', () => { it('creates the annotation in the sidebar', () => {
frameSync.connect(); frameSync.connect();
fakeStore.isLoggedIn.returns(true); fakeStore.isLoggedIn.returns(true);
const ann = { target: [] }; const ann = { $tag: 't1', target: [] };
guestBridge().emit('createAnnotation', { tag: 't1', msg: ann }); guestBridge().emit('createAnnotation', ann);
assert.calledWith( assert.calledWith(fakeAnnotationsService.create, ann);
fakeAnnotationsService.create,
sinon.match({
$tag: 't1',
target: [],
})
);
}); });
it('opens the sidebar ready for the user to edit the draft', () => { it('opens the sidebar ready for the user to edit the draft', () => {
frameSync.connect(); frameSync.connect();
fakeStore.isLoggedIn.returns(true); fakeStore.isLoggedIn.returns(true);
const ann = { target: [] };
guestBridge().emit('createAnnotation', { tag: 't1', msg: ann }); guestBridge().emit('createAnnotation', { $tag: 't1', target: [] });
assert.calledWith(hostBridge().call, 'openSidebar'); assert.calledWith(hostBridge().call, 'openSidebar');
}); });
...@@ -333,9 +322,12 @@ describe('FrameSyncService', () => { ...@@ -333,9 +322,12 @@ describe('FrameSyncService', () => {
it('does not open the sidebar if the annotation is a highlight', () => { it('does not open the sidebar if the annotation is a highlight', () => {
frameSync.connect(); frameSync.connect();
fakeStore.isLoggedIn.returns(true); fakeStore.isLoggedIn.returns(true);
const ann = { $highlight: true, target: [] };
guestBridge().emit('createAnnotation', { tag: 't1', msg: ann }); guestBridge().emit('createAnnotation', {
$tag: 't1',
$highlight: true,
target: [],
});
assert.neverCalledWith(hostBridge().call, 'openSidebar'); assert.neverCalledWith(hostBridge().call, 'openSidebar');
}); });
...@@ -348,30 +340,25 @@ describe('FrameSyncService', () => { ...@@ -348,30 +340,25 @@ describe('FrameSyncService', () => {
}); });
it('should not create an annotation in the sidebar', () => { it('should not create an annotation in the sidebar', () => {
const ann = { target: [] }; guestBridge().emit('createAnnotation', { $tag: 't1', target: [] });
guestBridge().emit('createAnnotation', { tag: 't1', msg: ann });
assert.notCalled(fakeAnnotationsService.create); assert.notCalled(fakeAnnotationsService.create);
}); });
it('should open the sidebar', () => { it('should open the sidebar', () => {
const ann = { target: [] }; guestBridge().emit('createAnnotation', { $tag: 't1', target: [] });
guestBridge().emit('createAnnotation', { tag: 't1', msg: ann });
assert.calledWith(hostBridge().call, 'openSidebar'); assert.calledWith(hostBridge().call, 'openSidebar');
}); });
it('should open the login prompt panel', () => { it('should open the login prompt panel', () => {
const ann = { target: [] }; guestBridge().emit('createAnnotation', { $tag: 't1', target: [] });
guestBridge().emit('createAnnotation', { tag: 't1', msg: ann });
assert.calledWith(fakeStore.openSidebarPanel, 'loginPrompt'); assert.calledWith(fakeStore.openSidebarPanel, 'loginPrompt');
}); });
it('should send a "deleteAnnotation" message to the frame', () => { it('should send a "deleteAnnotation" message to the frame', () => {
const ann = { target: [] }; guestBridge().emit('createAnnotation', { $tag: 't1', target: [] });
guestBridge().emit('createAnnotation', { tag: 't1', msg: ann });
assert.calledWith(guestBridge().call, 'deleteAnnotation'); assert.calledWith(guestBridge().call, 'deleteAnnotation');
}); });
...@@ -397,9 +384,7 @@ describe('FrameSyncService', () => { ...@@ -397,9 +384,7 @@ describe('FrameSyncService', () => {
} }
it('updates the anchoring status for the annotation', () => { it('updates the anchoring status for the annotation', () => {
guestBridge().emit('syncAnchoringStatus', [ guestBridge().emit('syncAnchoringStatus', { $tag: 't1', $orphan: false });
{ tag: 't1', msg: { $orphan: false } },
]);
expireDebounceTimeout(); expireDebounceTimeout();
...@@ -407,12 +392,14 @@ describe('FrameSyncService', () => { ...@@ -407,12 +392,14 @@ describe('FrameSyncService', () => {
}); });
it('coalesces multiple "syncAnchoringStatus" messages', () => { it('coalesces multiple "syncAnchoringStatus" messages', () => {
guestBridge().emit('syncAnchoringStatus', [ guestBridge().emit('syncAnchoringStatus', {
{ tag: 't1', msg: { $orphan: false } }, $tag: 't1',
]); $orphan: false,
guestBridge().emit('syncAnchoringStatus', [ });
{ tag: 't2', msg: { $orphan: true } }, guestBridge().emit('syncAnchoringStatus', {
]); $tag: 't2',
$orphan: true,
});
expireDebounceTimeout(); expireDebounceTimeout();
......
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