Skip to content

Commit

Permalink
Adoption of Bridge event enums in tests
Browse files Browse the repository at this point in the history
For greater consistency, we decided to use the enums defined in
`brdige-events.js` everywhere, including test.
  • Loading branch information
esanzgar committed Oct 14, 2021
1 parent 4695749 commit 1bb0f96
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 113 deletions.
3 changes: 2 additions & 1 deletion src/annotator/test/annotation-counts-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sidebarToHostEvents } from '../../shared/bridge-events';
import { annotationCounts } from '../annotation-counts';

describe('annotationCounts', () => {
Expand Down Expand Up @@ -57,7 +58,7 @@ describe('annotationCounts', () => {
const newCount = 10;
annotationCounts(document.body, fakeCrossFrame);

emitEvent('publicAnnotationCountChanged', newCount);
emitEvent(sidebarToHostEvents.PUBLIC_ANNOTATION_COUNT_CHANGED, newCount);

assert.equal(countEl1.textContent, newCount);
assert.equal(countEl2.textContent, newCount);
Expand Down
39 changes: 24 additions & 15 deletions src/annotator/test/annotation-sync-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { EventBus } from '../util/emitter';

import {
guestToSidebarEvents,
sidebarToGuestEvents,
} from '../../shared/bridge-events';
import { AnnotationSync } from '../annotation-sync';
import { EventBus } from '../util/emitter';

describe('AnnotationSync', () => {
let createAnnotationSync;
Expand Down Expand Up @@ -44,12 +47,12 @@ describe('AnnotationSync', () => {
emitter.subscribe('annotationDeleted', eventStub);
createAnnotationSync();

publish('deleteAnnotation', { msg: ann }, () => {});
publish(sidebarToGuestEvents.DELETE_ANNOTATION, { msg: ann }, () => {});

assert.calledWith(eventStub, ann);
});

it("calls the 'deleteAnnotation' event's callback function", done => {
it('calls the "deleteAnnotation" event\'s callback function', done => {
const ann = { id: 1, $tag: 'tag1' };
const callback = function (err, result) {
assert.isNull(err);
Expand All @@ -58,7 +61,7 @@ describe('AnnotationSync', () => {
};
createAnnotationSync();

publish('deleteAnnotation', { msg: ann }, callback);
publish(sidebarToGuestEvents.DELETE_ANNOTATION, { msg: ann }, callback);
});

it('deletes any existing annotation from its cache before publishing event to the annotator', done => {
Expand All @@ -70,15 +73,15 @@ describe('AnnotationSync', () => {
done();
});

publish('deleteAnnotation', { msg: ann }, () => {});
publish(sidebarToGuestEvents.DELETE_ANNOTATION, { msg: ann }, () => {});
});

it('deletes any existing annotation from its cache', () => {
const ann = { id: 1, $tag: 'tag1' };
const annSync = createAnnotationSync();
annSync.cache.tag1 = ann;

publish('deleteAnnotation', { msg: ann }, () => {});
publish(sidebarToGuestEvents.DELETE_ANNOTATION, { msg: ann }, () => {});

assert.isUndefined(annSync.cache.tag1);
});
Expand All @@ -100,7 +103,7 @@ describe('AnnotationSync', () => {
emitter.subscribe('annotationsLoaded', loadedStub);
createAnnotationSync();

publish('loadAnnotations', bodies, () => {});
publish(sidebarToGuestEvents.LOAD_ANNOTATIONS, bodies, () => {});

assert.calledWith(loadedStub, annotations);
});
Expand All @@ -118,10 +121,14 @@ describe('AnnotationSync', () => {
emitter.publish('beforeAnnotationCreated', ann);

assert.called(fakeBridge.call);
assert.calledWith(fakeBridge.call, 'beforeCreateAnnotation', {
msg: ann,
tag: ann.$tag,
});
assert.calledWith(
fakeBridge.call,
guestToSidebarEvents.BEFORE_CREATE_ANNOTATION,
{
msg: ann,
tag: ann.$tag,
}
);
});

it('assigns a non-empty tag to the annotation', () => {
Expand Down Expand Up @@ -162,7 +169,9 @@ describe('AnnotationSync', () => {

annotationSync.sync([ann]);

assert.calledWith(fakeBridge.call, 'sync', [{ msg: ann, tag: ann.$tag }]);
assert.calledWith(fakeBridge.call, guestToSidebarEvents.SYNC, [
{ msg: ann, tag: ann.$tag },
]);
});
});

Expand All @@ -173,8 +182,8 @@ describe('AnnotationSync', () => {
annotationSync.destroy();
const cb = sinon.stub();

publish('loadAnnotations', [ann], cb);
publish('deleteAnnotation', ann, cb);
publish(sidebarToGuestEvents.LOAD_ANNOTATIONS, [ann], cb);
publish(sidebarToGuestEvents.DELETE_ANNOTATION, ann, cb);

assert.calledTwice(cb);
assert.calledWith(cb.firstCall, null);
Expand Down
3 changes: 2 additions & 1 deletion src/annotator/test/features-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { sidebarToHostEvents } from '../../shared/bridge-events';
import { features, $imports } from '../features';

describe('features - annotation layer', () => {
Expand All @@ -21,7 +22,7 @@ describe('features - annotation layer', () => {

features.init({
on: function (topic, handler) {
if (topic === 'featureFlagsUpdated') {
if (topic === sidebarToHostEvents.FEATURE_FLAGS_UPDATED) {
featureFlagsUpdateHandler = handler;
}
},
Expand Down
103 changes: 64 additions & 39 deletions src/annotator/test/guest-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Guest from '../guest';
import {
guestToSidebarEvents,
sidebarToGuestEvents,
} from '../../shared/bridge-events';
import Guest, { $imports } from '../guest';
import { EventBus } from '../util/emitter';
import { $imports } from '../guest';

class FakeAdder {
constructor(container, options) {
Expand Down Expand Up @@ -234,7 +237,7 @@ describe('Guest', () => {
});

describe('events from sidebar', () => {
const emitGuestEvent = (event, ...args) => {
const emitSidebarEvent = (event, ...args) => {
for (let [evt, fn] of fakeBridge.on.args) {
if (event === evt) {
fn(...args);
Expand All @@ -252,7 +255,7 @@ describe('Guest', () => {
{ annotation: { $tag: 'tag2' }, highlights: [highlight1] },
];

emitGuestEvent('focusAnnotations', ['tag1']);
emitSidebarEvent(sidebarToGuestEvents.FOCUS_ANNOTATIONS, ['tag1']);

assert.calledWith(
highlighter.setHighlightsFocused,
Expand All @@ -270,7 +273,7 @@ describe('Guest', () => {
{ annotation: { $tag: 'tag2' }, highlights: [highlight1] },
];

emitGuestEvent('focusAnnotations', ['tag1']);
emitSidebarEvent(sidebarToGuestEvents.FOCUS_ANNOTATIONS, ['tag1']);

assert.calledWith(
highlighter.setHighlightsFocused,
Expand All @@ -282,8 +285,11 @@ describe('Guest', () => {
it('updates focused tag set', () => {
const guest = createGuest();

emitGuestEvent('focusAnnotations', ['tag1']);
emitGuestEvent('focusAnnotations', ['tag2', 'tag3']);
emitSidebarEvent(sidebarToGuestEvents.FOCUS_ANNOTATIONS, ['tag1']);
emitSidebarEvent(sidebarToGuestEvents.FOCUS_ANNOTATIONS, [
'tag2',
'tag3',
]);

assert.deepEqual([...guest.focusedAnnotationTags], ['tag2', 'tag3']);
});
Expand All @@ -302,7 +308,7 @@ describe('Guest', () => {
},
];

emitGuestEvent('scrollToAnnotation', 'tag1');
emitSidebarEvent(sidebarToGuestEvents.SCROLL_TO_ANNOTATION, 'tag1');

assert.called(fakeIntegration.scrollToAnchor);
assert.calledWith(fakeIntegration.scrollToAnchor, guest.anchors[0]);
Expand All @@ -326,7 +332,7 @@ describe('Guest', () => {
resolve();
});

emitGuestEvent('scrollToAnnotation', 'tag1');
emitSidebarEvent(sidebarToGuestEvents.SCROLL_TO_ANNOTATION, 'tag1');
});
});

Expand All @@ -345,7 +351,7 @@ describe('Guest', () => {
event.preventDefault()
);

emitGuestEvent('scrollToAnnotation', 'tag1');
emitSidebarEvent(sidebarToGuestEvents.SCROLL_TO_ANNOTATION, 'tag1');

assert.notCalled(fakeIntegration.scrollToAnchor);
});
Expand All @@ -354,7 +360,7 @@ describe('Guest', () => {
const guest = createGuest();

guest.anchors = [{ annotation: { $tag: 'tag1' } }];
emitGuestEvent('scrollToAnnotation', 'tag1');
emitSidebarEvent(sidebarToGuestEvents.SCROLL_TO_ANNOTATION, 'tag1');

assert.notCalled(fakeIntegration.scrollToAnchor);
});
Expand All @@ -374,7 +380,7 @@ describe('Guest', () => {
const eventEmitted = sandbox.stub();
guest.element.addEventListener('scrolltorange', eventEmitted);

emitGuestEvent('scrollToAnnotation', 'tag1');
emitSidebarEvent(sidebarToGuestEvents.SCROLL_TO_ANNOTATION, 'tag1');

assert.notCalled(eventEmitted);
assert.notCalled(fakeIntegration.scrollToAnchor);
Expand Down Expand Up @@ -408,8 +414,8 @@ describe('Guest', () => {

fakeIntegration.getMetadata.resolves(metadata);

emitGuestEvent(
'getDocumentInfo',
emitSidebarEvent(
sidebarToGuestEvents.GET_DOCUMENT_INFO,
createCallback('https://example.com/test.pdf', metadata, done)
);
});
Expand All @@ -419,14 +425,14 @@ describe('Guest', () => {
it('sets visibility of highlights in document', () => {
const guest = createGuest();

emitGuestEvent('setVisibleHighlights', true);
emitSidebarEvent(sidebarToGuestEvents.SET_VISIBLE_HIGHLIGHTS, true);
assert.calledWith(
highlighter.setHighlightsVisible,
guest.element,
true
);

emitGuestEvent('setVisibleHighlights', false);
emitSidebarEvent(sidebarToGuestEvents.SET_VISIBLE_HIGHLIGHTS, false);
assert.calledWith(
highlighter.setHighlightsVisible,
guest.element,
Expand Down Expand Up @@ -467,7 +473,7 @@ describe('Guest', () => {
it('hides sidebar on user "mousedown" or "touchstart" events in the document', () => {
for (let event of ['mousedown', 'touchstart']) {
rootElement.dispatchEvent(new Event(event));
assert.calledWith(fakeBridge.call, 'closeSidebar');
assert.calledWith(fakeBridge.call, guestToSidebarEvents.CLOSE_SIDEBAR);
fakeBridge.call.resetHistory();
}
});
Expand Down Expand Up @@ -511,13 +517,19 @@ describe('Guest', () => {
// Hover the highlight
fakeHighlight.dispatchEvent(new Event('mouseover', { bubbles: true }));
assert.calledWith(highlighter.getHighlightsContainingNode, fakeHighlight);
assert.calledWith(fakeBridge.call, 'focusAnnotations', [
'highlight-ann-tag',
]);
assert.calledWith(
fakeBridge.call,
sidebarToGuestEvents.FOCUS_ANNOTATIONS,
['highlight-ann-tag']
);

// Un-hover the highlight
fakeHighlight.dispatchEvent(new Event('mouseout', { bubbles: true }));
assert.calledWith(fakeBridge.call, 'focusAnnotations', []);
assert.calledWith(
fakeBridge.call,
sidebarToGuestEvents.FOCUS_ANNOTATIONS,
[]
);
});

it('does not focus annotations in the sidebar when a non-highlight element is hovered', () => {
Expand All @@ -540,21 +552,25 @@ describe('Guest', () => {
it('selects annotations in the sidebar when clicking on a highlight', () => {
fakeHighlight.dispatchEvent(new Event('mouseup', { bubbles: true }));

assert.calledWith(fakeBridge.call, 'showAnnotations', [
'highlight-ann-tag',
]);
assert.calledWith(fakeBridge.call, 'openSidebar');
assert.calledWith(
fakeBridge.call,
guestToSidebarEvents.SHOW_ANNOTATIONS,
['highlight-ann-tag']
);
assert.calledWith(fakeBridge.call, guestToSidebarEvents.OPEN_SIDEBAR);
});

it('toggles selected annotations in the sidebar when Ctrl/Cmd-clicking a highlight', () => {
fakeHighlight.dispatchEvent(
new MouseEvent('mouseup', { bubbles: true, ctrlKey: true })
);

assert.calledWith(fakeBridge.call, 'toggleAnnotationSelection', [
'highlight-ann-tag',
]);
assert.calledWith(fakeBridge.call, 'openSidebar');
assert.calledWith(
fakeBridge.call,
guestToSidebarEvents.TOGGLE_ANNOTATION_SELECTION,
['highlight-ann-tag']
);
assert.calledWith(fakeBridge.call, guestToSidebarEvents.OPEN_SIDEBAR);
});
});

Expand Down Expand Up @@ -680,8 +696,12 @@ describe('Guest', () => {

FakeAdder.instance.options.onShowAnnotations([{ $tag: 'ann1' }]);

assert.calledWith(fakeBridge.call, 'openSidebar');
assert.calledWith(fakeBridge.call, 'showAnnotations', ['ann1']);
assert.calledWith(fakeBridge.call, guestToSidebarEvents.OPEN_SIDEBAR);
assert.calledWith(
fakeBridge.call,
guestToSidebarEvents.SHOW_ANNOTATIONS,
['ann1']
);
});
});

Expand All @@ -692,7 +712,11 @@ describe('Guest', () => {

guest.selectAnnotations(annotations);

assert.calledWith(fakeBridge.call, 'showAnnotations', ['ann1', 'ann2']);
assert.calledWith(
fakeBridge.call,
guestToSidebarEvents.SHOW_ANNOTATIONS,
['ann1', 'ann2']
);
});

it('toggles the annotations if `toggle` is true', () => {
Expand All @@ -701,18 +725,19 @@ describe('Guest', () => {

guest.selectAnnotations(annotations, true /* toggle */);

assert.calledWith(fakeBridge.call, 'toggleAnnotationSelection', [
'ann1',
'ann2',
]);
assert.calledWith(
fakeBridge.call,
guestToSidebarEvents.TOGGLE_ANNOTATION_SELECTION,
['ann1', 'ann2']
);
});

it('opens the sidebar', () => {
const guest = createGuest();

guest.selectAnnotations([]);

assert.calledWith(fakeBridge.call, 'openSidebar');
assert.calledWith(fakeBridge.call, guestToSidebarEvents.OPEN_SIDEBAR);
});
});

Expand Down Expand Up @@ -816,7 +841,7 @@ describe('Guest', () => {
it('opens sidebar if `highlight` is false', async () => {
const guest = createGuest();
await guest.createAnnotation();
assert.calledWith(fakeBridge.call, 'openSidebar');
assert.calledWith(fakeBridge.call, guestToSidebarEvents.OPEN_SIDEBAR);
});

it('does not open sidebar if `highlight` is true', async () => {
Expand Down Expand Up @@ -1050,7 +1075,7 @@ describe('Guest', () => {

// Focus the annotation (in the sidebar) before it is anchored in the page.
const [, focusAnnotationsCallback] = fakeBridge.on.args.find(
args => args[0] === 'focusAnnotations'
args => args[0] === guestToSidebarEvents.FOCUS_ANNOTATIONS
);
focusAnnotationsCallback([annotation.$tag]);
const anchors = await guest.anchor(annotation);
Expand Down
Loading

0 comments on commit 1bb0f96

Please sign in to comment.