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