From 1bb0f96d1b51e21c3022218958fc985304f61349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Sanz=20Garc=C3=ADa?= Date: Thu, 14 Oct 2021 12:17:58 +0200 Subject: [PATCH] Adoption of `Bridge` event enums in tests For greater consistency, we decided to use the enums defined in `brdige-events.js` everywhere, including test. --- src/annotator/test/annotation-counts-test.js | 3 +- src/annotator/test/annotation-sync-test.js | 39 +++--- src/annotator/test/features-test.js | 3 +- src/annotator/test/guest-test.js | 103 +++++++++------ src/annotator/test/sidebar-test.js | 48 ++++--- .../components/test/HypothesisApp-test.js | 17 ++- src/sidebar/components/test/TopBar-test.js | 6 +- src/sidebar/components/test/UserMenu-test.js | 6 +- src/sidebar/services/test/features-test.js | 5 +- src/sidebar/services/test/frame-sync-test.js | 117 +++++++++++++----- 10 files changed, 234 insertions(+), 113 deletions(-) diff --git a/src/annotator/test/annotation-counts-test.js b/src/annotator/test/annotation-counts-test.js index 1e7e20d41f0..61c50aebabd 100644 --- a/src/annotator/test/annotation-counts-test.js +++ b/src/annotator/test/annotation-counts-test.js @@ -1,3 +1,4 @@ +import { sidebarToHostEvents } from '../../shared/bridge-events'; import { annotationCounts } from '../annotation-counts'; describe('annotationCounts', () => { @@ -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); diff --git a/src/annotator/test/annotation-sync-test.js b/src/annotator/test/annotation-sync-test.js index cec1a63af71..af287e305c5 100644 --- a/src/annotator/test/annotation-sync-test.js +++ b/src/annotator/test/annotation-sync-test.js @@ -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; @@ -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); @@ -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 => { @@ -70,7 +73,7 @@ describe('AnnotationSync', () => { done(); }); - publish('deleteAnnotation', { msg: ann }, () => {}); + publish(sidebarToGuestEvents.DELETE_ANNOTATION, { msg: ann }, () => {}); }); it('deletes any existing annotation from its cache', () => { @@ -78,7 +81,7 @@ describe('AnnotationSync', () => { const annSync = createAnnotationSync(); annSync.cache.tag1 = ann; - publish('deleteAnnotation', { msg: ann }, () => {}); + publish(sidebarToGuestEvents.DELETE_ANNOTATION, { msg: ann }, () => {}); assert.isUndefined(annSync.cache.tag1); }); @@ -100,7 +103,7 @@ describe('AnnotationSync', () => { emitter.subscribe('annotationsLoaded', loadedStub); createAnnotationSync(); - publish('loadAnnotations', bodies, () => {}); + publish(sidebarToGuestEvents.LOAD_ANNOTATIONS, bodies, () => {}); assert.calledWith(loadedStub, annotations); }); @@ -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', () => { @@ -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 }, + ]); }); }); @@ -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); diff --git a/src/annotator/test/features-test.js b/src/annotator/test/features-test.js index ff392635499..99133d669f6 100644 --- a/src/annotator/test/features-test.js +++ b/src/annotator/test/features-test.js @@ -1,3 +1,4 @@ +import { sidebarToHostEvents } from '../../shared/bridge-events'; import { features, $imports } from '../features'; describe('features - annotation layer', () => { @@ -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; } }, diff --git a/src/annotator/test/guest-test.js b/src/annotator/test/guest-test.js index fdfc336a302..4e8bd087e37 100644 --- a/src/annotator/test/guest-test.js +++ b/src/annotator/test/guest-test.js @@ -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) { @@ -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); @@ -252,7 +255,7 @@ describe('Guest', () => { { annotation: { $tag: 'tag2' }, highlights: [highlight1] }, ]; - emitGuestEvent('focusAnnotations', ['tag1']); + emitSidebarEvent(sidebarToGuestEvents.FOCUS_ANNOTATIONS, ['tag1']); assert.calledWith( highlighter.setHighlightsFocused, @@ -270,7 +273,7 @@ describe('Guest', () => { { annotation: { $tag: 'tag2' }, highlights: [highlight1] }, ]; - emitGuestEvent('focusAnnotations', ['tag1']); + emitSidebarEvent(sidebarToGuestEvents.FOCUS_ANNOTATIONS, ['tag1']); assert.calledWith( highlighter.setHighlightsFocused, @@ -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']); }); @@ -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]); @@ -326,7 +332,7 @@ describe('Guest', () => { resolve(); }); - emitGuestEvent('scrollToAnnotation', 'tag1'); + emitSidebarEvent(sidebarToGuestEvents.SCROLL_TO_ANNOTATION, 'tag1'); }); }); @@ -345,7 +351,7 @@ describe('Guest', () => { event.preventDefault() ); - emitGuestEvent('scrollToAnnotation', 'tag1'); + emitSidebarEvent(sidebarToGuestEvents.SCROLL_TO_ANNOTATION, 'tag1'); assert.notCalled(fakeIntegration.scrollToAnchor); }); @@ -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); }); @@ -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); @@ -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) ); }); @@ -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, @@ -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(); } }); @@ -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', () => { @@ -540,10 +552,12 @@ 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', () => { @@ -551,10 +565,12 @@ describe('Guest', () => { 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); }); }); @@ -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'] + ); }); }); @@ -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', () => { @@ -701,10 +725,11 @@ 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', () => { @@ -712,7 +737,7 @@ describe('Guest', () => { guest.selectAnnotations([]); - assert.calledWith(fakeBridge.call, 'openSidebar'); + assert.calledWith(fakeBridge.call, guestToSidebarEvents.OPEN_SIDEBAR); }); }); @@ -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 () => { @@ -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); diff --git a/src/annotator/test/sidebar-test.js b/src/annotator/test/sidebar-test.js index 59317f1fbc8..a99b3516985 100644 --- a/src/annotator/test/sidebar-test.js +++ b/src/annotator/test/sidebar-test.js @@ -1,3 +1,7 @@ +import { + hostToSidebarEvents, + sidebarToHostEvents, +} from '../../shared/bridge-events'; import Sidebar, { MIN_RESIZE, $imports } from '../sidebar'; import { EventBus } from '../util/emitter'; @@ -201,7 +205,11 @@ describe('Sidebar', () => { }); window.dispatchEvent(event); - assert.calledWith(fakeBridge.call, 'destroyFrame', 'frame-id'); + assert.calledWith( + fakeBridge.call, + hostToSidebarEvents.DESTROY_FRAME, + 'frame-id' + ); }); function getConfigString(sidebar) { @@ -324,7 +332,7 @@ describe('Sidebar', () => { it('opens the frame', () => { const target = sandbox.stub(Sidebar.prototype, 'open'); createSidebar(); - emitEvent('openSidebar'); + emitEvent(sidebarToHostEvents.OPEN_SIDEBAR); assert.called(target); })); @@ -332,7 +340,7 @@ describe('Sidebar', () => { it('closes the frame', () => { const target = sandbox.stub(Sidebar.prototype, 'close'); createSidebar(); - emitEvent('closeSidebar'); + emitEvent(sidebarToHostEvents.CLOSE_SIDEBAR); assert.called(target); })); @@ -358,12 +366,12 @@ describe('Sidebar', () => { }); }); - describe('on LOGIN_REQUESTED event', () => { + describe('on "loginRequest" event', () => { it('calls the onLoginRequest callback function if one was provided', () => { const onLoginRequest = sandbox.stub(); createSidebar({ services: [{ onLoginRequest }] }); - emitEvent('loginRequested'); + emitEvent(sidebarToHostEvents.LOGIN_REQUESTED); assert.called(onLoginRequest); }); @@ -383,7 +391,7 @@ describe('Sidebar', () => { ], }); - emitEvent('loginRequested'); + emitEvent(sidebarToHostEvents.LOGIN_REQUESTED); assert.called(firstOnLogin); assert.notCalled(secondOnLogin); @@ -403,7 +411,7 @@ describe('Sidebar', () => { ], }); - emitEvent('loginRequested'); + emitEvent(sidebarToHostEvents.LOGIN_REQUESTED); assert.notCalled(secondOnLogin); assert.notCalled(thirdOnLogin); @@ -411,17 +419,17 @@ describe('Sidebar', () => { it('does not crash if there is no services', () => { createSidebar(); // No config.services - emitEvent('loginRequested'); + emitEvent(sidebarToHostEvents.LOGIN_REQUESTED); }); it('does not crash if services is an empty array', () => { createSidebar({ services: [] }); - emitEvent('loginRequested'); + emitEvent(sidebarToHostEvents.LOGIN_REQUESTED); }); it('does not crash if the first service has no onLoginRequest', () => { createSidebar({ services: [{}] }); - emitEvent('loginRequested'); + emitEvent(sidebarToHostEvents.LOGIN_REQUESTED); }); }); @@ -430,37 +438,37 @@ describe('Sidebar', () => { const onLogoutRequest = sandbox.stub(); createSidebar({ services: [{ onLogoutRequest }] }); - emitEvent('logoutRequested'); + emitEvent(sidebarToHostEvents.LOGOUT_REQUESTED); assert.called(onLogoutRequest); })); - describe('on SIGNUP_REQUESTED event', () => + describe('on "signupRequest" event', () => it('calls the onSignupRequest callback function', () => { const onSignupRequest = sandbox.stub(); createSidebar({ services: [{ onSignupRequest }] }); - emitEvent('signupRequested'); + emitEvent(sidebarToHostEvents.SIGNUP_REQUESTED); assert.called(onSignupRequest); })); - describe('on PROFILE_REQUESTED event', () => + describe('on "profileRequest" event', () => it('calls the onProfileRequest callback function', () => { const onProfileRequest = sandbox.stub(); createSidebar({ services: [{ onProfileRequest }] }); - emitEvent('profileRequested'); + emitEvent(sidebarToHostEvents.PROFILE_REQUESTED); assert.called(onProfileRequest); })); - describe('on HELP_REQUESTED event', () => + describe('on "helpRequested" event', () => it('calls the onHelpRequest callback function', () => { const onHelpRequest = sandbox.stub(); createSidebar({ services: [{ onHelpRequest }] }); - emitEvent('helpRequested'); + emitEvent(sidebarToHostEvents.HELP_REQUESTED); assert.called(onHelpRequest); })); @@ -636,7 +644,11 @@ describe('Sidebar', () => { it('requests sidebar to set highlight visibility in guest frames', () => { const sidebar = createSidebar(); sidebar.setAllVisibleHighlights(true); - assert.calledWith(fakeBridge.call, 'setVisibleHighlights', true); + assert.calledWith( + fakeBridge.call, + hostToSidebarEvents.SET_VISIBLE_HIGHLIGHTS, + true + ); })); it('hides toolbar controls when using the "clean" theme', () => { diff --git a/src/sidebar/components/test/HypothesisApp-test.js b/src/sidebar/components/test/HypothesisApp-test.js index c7c7c6375ba..367daabb246 100644 --- a/src/sidebar/components/test/HypothesisApp-test.js +++ b/src/sidebar/components/test/HypothesisApp-test.js @@ -1,5 +1,6 @@ import { mount } from 'enzyme'; +import { sidebarToHostEvents } from '../../../shared/bridge-events'; import mockImportedComponents from '../../../test-util/mock-imported-components'; import HypothesisApp, { $imports } from '../HypothesisApp'; @@ -223,10 +224,13 @@ describe('HypothesisApp', () => { fakeServiceConfig.returns({}); }); - it('sends SIGNUP_REQUESTED event', () => { + it('sends "signupRequest" event', () => { const wrapper = createComponent(); clickSignUp(wrapper); - assert.calledWith(fakeFrameSync.notifyHost, 'signupRequested'); + assert.calledWith( + fakeFrameSync.notifyHost, + sidebarToHostEvents.SIGNUP_REQUESTED + ); }); it('does not open a URL directly', () => { @@ -299,7 +303,9 @@ describe('HypothesisApp', () => { assert.equal(fakeFrameSync.notifyHost.callCount, 1); assert.isTrue( - fakeFrameSync.notifyHost.calledWithExactly('loginRequested') + fakeFrameSync.notifyHost.calledWithExactly( + sidebarToHostEvents.LOGIN_REQUESTED + ) ); }); }); @@ -407,7 +413,10 @@ describe('HypothesisApp', () => { await clickLogOut(wrapper); assert.calledOnce(fakeFrameSync.notifyHost); - assert.calledWithExactly(fakeFrameSync.notifyHost, 'logoutRequested'); + assert.calledWithExactly( + fakeFrameSync.notifyHost, + sidebarToHostEvents.LOGOUT_REQUESTED + ); }); it('does not send LOGOUT_REQUESTED if the user cancels the prompt', async () => { diff --git a/src/sidebar/components/test/TopBar-test.js b/src/sidebar/components/test/TopBar-test.js index 1782192377d..afcc5aa1e65 100644 --- a/src/sidebar/components/test/TopBar-test.js +++ b/src/sidebar/components/test/TopBar-test.js @@ -1,5 +1,6 @@ import { mount } from 'enzyme'; +import { sidebarToHostEvents } from '../../../shared/bridge-events'; import TopBar, { $imports } from '../TopBar'; import { checkAccessibility } from '../../../test-util/accessibility'; import mockImportedComponents from '../../../test-util/mock-imported-components'; @@ -120,7 +121,10 @@ describe('TopBar', () => { helpButton.props().onClick(); assert.equal(fakeStore.toggleSidebarPanel.callCount, 0); - assert.calledWith(fakeFrameSync.notifyHost, 'helpRequested'); + assert.calledWith( + fakeFrameSync.notifyHost, + sidebarToHostEvents.HELP_REQUESTED + ); }); }); }); diff --git a/src/sidebar/components/test/UserMenu-test.js b/src/sidebar/components/test/UserMenu-test.js index 7e97cbf6e69..6e917e1e788 100644 --- a/src/sidebar/components/test/UserMenu-test.js +++ b/src/sidebar/components/test/UserMenu-test.js @@ -1,6 +1,7 @@ import { mount } from 'enzyme'; import { act } from 'preact/test-utils'; +import { sidebarToHostEvents } from '../../../shared/bridge-events'; import mockImportedComponents from '../../../test-util/mock-imported-components'; import UserMenu, { $imports } from '../UserMenu'; @@ -145,7 +146,10 @@ describe('UserMenu', () => { onProfileSelected(); assert.equal(fakeFrameSync.notifyHost.callCount, 1); - assert.calledWith(fakeFrameSync.notifyHost, 'profileRequested'); + assert.calledWith( + fakeFrameSync.notifyHost, + sidebarToHostEvents.PROFILE_REQUESTED + ); }); it('should not fire profile event for first-party user', () => { diff --git a/src/sidebar/services/test/features-test.js b/src/sidebar/services/test/features-test.js index 0ae337b0252..b33454d8379 100644 --- a/src/sidebar/services/test/features-test.js +++ b/src/sidebar/services/test/features-test.js @@ -1,3 +1,4 @@ +import { sidebarToHostEvents } from '../../../shared/bridge-events'; import { FeaturesService } from '../features'; describe('FeaturesService', () => { @@ -51,7 +52,7 @@ describe('FeaturesService', () => { assert.calledWith( fakeFrameSync.notifyHost, - 'featureFlagsUpdated', + sidebarToHostEvents.FEATURE_FLAGS_UPDATED, fakeStore.profile().features ); }); @@ -70,7 +71,7 @@ describe('FeaturesService', () => { assert.calledWith( fakeFrameSync.notifyHost, - 'featureFlagsUpdated', + sidebarToHostEvents.FEATURE_FLAGS_UPDATED, fakeStore.profile().features ); }); diff --git a/src/sidebar/services/test/frame-sync-test.js b/src/sidebar/services/test/frame-sync-test.js index 8e4815726c6..5241daa217d 100644 --- a/src/sidebar/services/test/frame-sync-test.js +++ b/src/sidebar/services/test/frame-sync-test.js @@ -1,5 +1,11 @@ import EventEmitter from 'tiny-emitter'; +import { + hostToSidebarEvents, + guestToSidebarEvents, + sidebarToHostEvents, + sidebarToGuestEvents, +} from '../../../shared/bridge-events'; import { Injector } from '../../../shared/injector'; import * as annotationFixtures from '../../test/annotation-fixtures'; import createFakeStore from '../../test/fake-redux-store'; @@ -206,7 +212,7 @@ describe('FrameSyncService', () => { assert.calledWithMatch( guestBridge().call, - 'loadAnnotations', + sidebarToGuestEvents.LOAD_ANNOTATIONS, sinon.match([formatAnnot(fixtures.ann)]) ); }); @@ -224,7 +230,7 @@ describe('FrameSyncService', () => { assert.calledWithMatch( guestBridge().call, - 'loadAnnotations', + sidebarToGuestEvents.LOAD_ANNOTATIONS, sinon.match([formatAnnot(ann2)]) ); }); @@ -234,7 +240,9 @@ describe('FrameSyncService', () => { annotations: [annotationFixtures.newReply()], }); - assert.isFalse(guestBridge().call.calledWith('loadAnnotations')); + assert.isFalse( + guestBridge().call.calledWith(sidebarToGuestEvents.LOAD_ANNOTATIONS) + ); }); }); @@ -249,7 +257,7 @@ describe('FrameSyncService', () => { }); assert.calledWithMatch( hostBridge().call, - 'publicAnnotationCountChanged', + sidebarToHostEvents.PUBLIC_ANNOTATION_COUNT_CHANGED, sinon.match(1) ); }); @@ -264,7 +272,7 @@ describe('FrameSyncService', () => { assert.calledWithMatch( hostBridge().call, - 'publicAnnotationCountChanged', + sidebarToHostEvents.PUBLIC_ANNOTATION_COUNT_CHANGED, sinon.match(0) ); }); @@ -275,7 +283,9 @@ describe('FrameSyncService', () => { annotations: [annotationFixtures.publicAnnotation()], }); assert.isFalse( - hostBridge().call.calledWith('publicAnnotationCountChanged') + hostBridge().call.calledWith( + sidebarToHostEvents.PUBLIC_ANNOTATION_COUNT_CHANGED + ) ); }); @@ -285,7 +295,9 @@ describe('FrameSyncService', () => { annotations: [annotationFixtures.publicAnnotation()], }); assert.isFalse( - hostBridge().call.calledWith('publicAnnotationCountChanged') + hostBridge().call.calledWith( + sidebarToHostEvents.PUBLIC_ANNOTATION_COUNT_CHANGED + ) ); }); }); @@ -303,7 +315,7 @@ describe('FrameSyncService', () => { assert.calledWithMatch( guestBridge().call, - 'deleteAnnotation', + sidebarToGuestEvents.DELETE_ANNOTATION, sinon.match(formatAnnot(fixtures.ann)) ); }); @@ -316,7 +328,10 @@ describe('FrameSyncService', () => { fakeStore.isLoggedIn.returns(true); const ann = { target: [] }; - guestBridge().emit('beforeCreateAnnotation', { tag: 't1', msg: ann }); + guestBridge().emit(guestToSidebarEvents.BEFORE_CREATE_ANNOTATION, { + tag: 't1', + msg: ann, + }); assert.calledWith( fakeAnnotationsService.create, @@ -337,30 +352,45 @@ describe('FrameSyncService', () => { it('should not create an annotation in the sidebar', () => { const ann = { target: [] }; - guestBridge().emit('beforeCreateAnnotation', { tag: 't1', msg: ann }); + guestBridge().emit(guestToSidebarEvents.BEFORE_CREATE_ANNOTATION, { + tag: 't1', + msg: ann, + }); assert.notCalled(fakeAnnotationsService.create); }); it('should open the sidebar', () => { const ann = { target: [] }; - guestBridge().emit('beforeCreateAnnotation', { tag: 't1', msg: ann }); + guestBridge().emit(guestToSidebarEvents.BEFORE_CREATE_ANNOTATION, { + tag: 't1', + msg: ann, + }); - assert.calledWith(hostBridge().call, 'openSidebar'); + assert.calledWith(hostBridge().call, sidebarToHostEvents.OPEN_SIDEBAR); }); it('should open the login prompt panel', () => { const ann = { target: [] }; - guestBridge().emit('beforeCreateAnnotation', { tag: 't1', msg: ann }); + guestBridge().emit(guestToSidebarEvents.BEFORE_CREATE_ANNOTATION, { + tag: 't1', + msg: ann, + }); assert.calledWith(fakeStore.openSidebarPanel, 'loginPrompt'); }); it('should send a "deleteAnnotation" message to the frame', () => { const ann = { target: [] }; - guestBridge().emit('beforeCreateAnnotation', { tag: 't1', msg: ann }); + guestBridge().emit(guestToSidebarEvents.BEFORE_CREATE_ANNOTATION, { + tag: 't1', + msg: ann, + }); - assert.calledWith(guestBridge().call, 'deleteAnnotation'); + assert.calledWith( + guestBridge().call, + sidebarToGuestEvents.DELETE_ANNOTATION + ); }); }); }); @@ -384,7 +414,9 @@ describe('FrameSyncService', () => { } it('updates the anchoring status for the annotation', () => { - guestBridge().emit('sync', [{ tag: 't1', msg: { $orphan: false } }]); + guestBridge().emit(guestToSidebarEvents.SYNC, [ + { tag: 't1', msg: { $orphan: false } }, + ]); expireDebounceTimeout(); @@ -392,8 +424,12 @@ describe('FrameSyncService', () => { }); it('coalesces multiple "sync" messages', () => { - guestBridge().emit('sync', [{ tag: 't1', msg: { $orphan: false } }]); - guestBridge().emit('sync', [{ tag: 't2', msg: { $orphan: true } }]); + guestBridge().emit(guestToSidebarEvents.SYNC, [ + { tag: 't1', msg: { $orphan: false } }, + ]); + guestBridge().emit(guestToSidebarEvents.SYNC, [ + { tag: 't2', msg: { $orphan: true } }, + ]); expireDebounceTimeout(); @@ -444,7 +480,7 @@ describe('FrameSyncService', () => { it('removes the frame from the frames list', () => { frameSync.connect(); - hostBridge().emit('destroyFrame', frameId); + hostBridge().emit(hostToSidebarEvents.DESTROY_FRAME, frameId); assert.calledWith(fakeStore.destroyFrame, fixtures.framesListEntry); }); @@ -454,7 +490,11 @@ describe('FrameSyncService', () => { it('selects annotations which have an ID', () => { frameSync.connect(); fakeStore.findIDsForTags.returns(['id1', 'id2', 'id3']); - guestBridge().emit('showAnnotations', ['tag1', 'tag2', 'tag3']); + guestBridge().emit(guestToSidebarEvents.SHOW_ANNOTATIONS, [ + 'tag1', + 'tag2', + 'tag3', + ]); assert.calledWith(fakeStore.selectAnnotations, ['id1', 'id2', 'id3']); assert.calledWith(fakeStore.selectTab, 'annotation'); @@ -464,7 +504,11 @@ describe('FrameSyncService', () => { describe('on "focusAnnotations" message', () => { it('focuses the annotations', () => { frameSync.connect(); - guestBridge().emit('focusAnnotations', ['tag1', 'tag2', 'tag3']); + guestBridge().emit(guestToSidebarEvents.FOCUS_ANNOTATIONS, [ + 'tag1', + 'tag2', + 'tag3', + ]); assert.calledWith(fakeStore.focusAnnotations, ['tag1', 'tag2', 'tag3']); }); }); @@ -473,7 +517,11 @@ describe('FrameSyncService', () => { it('toggles the selected state of the annotations', () => { frameSync.connect(); fakeStore.findIDsForTags.returns(['id1', 'id2', 'id3']); - guestBridge().emit('toggleAnnotationSelection', ['tag1', 'tag2', 'tag3']); + guestBridge().emit(guestToSidebarEvents.TOGGLE_ANNOTATION_SELECTION, [ + 'tag1', + 'tag2', + 'tag3', + ]); assert.calledWith(fakeStore.toggleSelectedAnnotations, [ 'id1', 'id2', @@ -485,7 +533,7 @@ describe('FrameSyncService', () => { describe('on "sidebarOpened" message', () => { it('sets the sidebar open in the store', () => { frameSync.connect(); - hostBridge().emit('sidebarOpened'); + hostBridge().emit(hostToSidebarEvents.SIDEBAR_OPENED); assert.calledWith(fakeStore.setSidebarOpened, true); }); @@ -497,21 +545,24 @@ describe('FrameSyncService', () => { }); it('calls "openSidebar"', () => { - guestBridge().emit('openSidebar'); + guestBridge().emit(guestToSidebarEvents.OPEN_SIDEBAR); - assert.calledWith(hostBridge().call, 'openSidebar'); + assert.calledWith(hostBridge().call, sidebarToHostEvents.OPEN_SIDEBAR); }); it('calls "closeSidebar"', () => { - guestBridge().emit('closeSidebar'); + guestBridge().emit(guestToSidebarEvents.CLOSE_SIDEBAR); - assert.calledWith(hostBridge().call, 'closeSidebar'); + assert.calledWith(hostBridge().call, sidebarToHostEvents.CLOSE_SIDEBAR); }); it('calls "setVisibleHighlights"', () => { - hostBridge().emit('setVisibleHighlights'); + hostBridge().emit(hostToSidebarEvents.SET_VISIBLE_HIGHLIGHTS); - assert.calledWith(guestBridge().call, 'setVisibleHighlights'); + assert.calledWith( + guestBridge().call, + sidebarToGuestEvents.SET_VISIBLE_HIGHLIGHTS + ); }); }); @@ -532,7 +583,7 @@ describe('FrameSyncService', () => { frameSync.focusAnnotations([1, 2]); assert.calledWith( guestBridge().call, - 'focusAnnotations', + sidebarToGuestEvents.FOCUS_ANNOTATIONS, sinon.match.array.deepEquals([1, 2]) ); }); @@ -542,7 +593,11 @@ describe('FrameSyncService', () => { it('should scroll to the annotation in the guest', () => { frameSync.connect(); frameSync.scrollToAnnotation('atag'); - assert.calledWith(guestBridge().call, 'scrollToAnnotation', 'atag'); + assert.calledWith( + guestBridge().call, + sidebarToGuestEvents.SCROLL_TO_ANNOTATION, + 'atag' + ); }); });