Commit 0e523df3 authored by Robert Knight's avatar Robert Knight

Use store selectors instead of `getState` in `FrameSyncService`

Remove the last remaining usage of `store.getState()` outside of the
store implementation in favor of the `store.allAnnotations()` selector
method.

This allows simplifying the state structure in the fake store that the
`FrameSyncService` tests create.
parent 4b7c566d
...@@ -66,7 +66,7 @@ export class FrameSyncService { ...@@ -66,7 +66,7 @@ export class FrameSyncService {
watch( watch(
store.subscribe, store.subscribe,
[() => store.getState().annotations.annotations, () => store.frames()], [() => store.allAnnotations(), () => store.frames()],
([annotations, frames], [prevAnnotations]) => { ([annotations, frames], [prevAnnotations]) => {
let publicAnns = 0; let publicAnns = 0;
const inSidebar = new Set(); const inSidebar = new Set();
......
...@@ -54,8 +54,11 @@ describe('FrameSyncService', () => { ...@@ -54,8 +54,11 @@ describe('FrameSyncService', () => {
beforeEach(function () { beforeEach(function () {
fakeStore = createFakeStore( fakeStore = createFakeStore(
{ annotations: { annotations: [] } }, { annotations: [] },
{ {
allAnnotations() {
return this.getState().annotations;
},
connectFrame: sinon.stub(), connectFrame: sinon.stub(),
destroyFrame: sinon.stub(), destroyFrame: sinon.stub(),
findIDsForTags: sinon.stub(), findIDsForTags: sinon.stub(),
...@@ -110,7 +113,7 @@ describe('FrameSyncService', () => { ...@@ -110,7 +113,7 @@ describe('FrameSyncService', () => {
context('when annotations are loaded into the sidebar', function () { context('when annotations are loaded into the sidebar', function () {
it('sends a "loadAnnotations" message to the frame', function () { it('sends a "loadAnnotations" message to the frame', function () {
fakeStore.setState({ fakeStore.setState({
annotations: { annotations: [fixtures.ann] }, annotations: [fixtures.ann],
}); });
assert.calledWithMatch( assert.calledWithMatch(
fakeBridge.call, fakeBridge.call,
...@@ -122,12 +125,12 @@ describe('FrameSyncService', () => { ...@@ -122,12 +125,12 @@ describe('FrameSyncService', () => {
it('sends a "loadAnnotations" message only for new annotations', function () { it('sends a "loadAnnotations" message only for new annotations', function () {
const ann2 = Object.assign({}, fixtures.ann, { $tag: 't2', id: 'a2' }); const ann2 = Object.assign({}, fixtures.ann, { $tag: 't2', id: 'a2' });
fakeStore.setState({ fakeStore.setState({
annotations: { annotations: [fixtures.ann] }, annotations: [fixtures.ann],
}); });
fakeBridge.call.reset(); fakeBridge.call.reset();
fakeStore.setState({ fakeStore.setState({
annotations: { annotations: [fixtures.ann, ann2] }, annotations: [fixtures.ann, ann2],
}); });
assert.calledWithMatch( assert.calledWithMatch(
...@@ -139,7 +142,7 @@ describe('FrameSyncService', () => { ...@@ -139,7 +142,7 @@ describe('FrameSyncService', () => {
it('does not send a "loadAnnotations" message for replies', function () { it('does not send a "loadAnnotations" message for replies', function () {
fakeStore.setState({ fakeStore.setState({
annotations: { annotations: [annotationFixtures.newReply()] }, annotations: [annotationFixtures.newReply()],
}); });
assert.isFalse(fakeBridge.call.calledWith('loadAnnotations')); assert.isFalse(fakeBridge.call.calledWith('loadAnnotations'));
}); });
...@@ -148,7 +151,7 @@ describe('FrameSyncService', () => { ...@@ -148,7 +151,7 @@ describe('FrameSyncService', () => {
context('when annotation count has changed', function () { context('when annotation count has changed', function () {
it('sends a "publicAnnotationCountChanged" message to the frame when there are public annotations', function () { it('sends a "publicAnnotationCountChanged" message to the frame when there are public annotations', function () {
fakeStore.setState({ fakeStore.setState({
annotations: { annotations: [annotationFixtures.publicAnnotation()] }, annotations: [annotationFixtures.publicAnnotation()],
}); });
assert.calledWithMatch( assert.calledWithMatch(
fakeBridge.call, fakeBridge.call,
...@@ -162,7 +165,7 @@ describe('FrameSyncService', () => { ...@@ -162,7 +165,7 @@ describe('FrameSyncService', () => {
delete annot.permissions; delete annot.permissions;
fakeStore.setState({ fakeStore.setState({
annotations: { annotations: [annot] }, annotations: [annot],
}); });
assert.calledWithMatch( assert.calledWithMatch(
fakeBridge.call, fakeBridge.call,
...@@ -174,7 +177,7 @@ describe('FrameSyncService', () => { ...@@ -174,7 +177,7 @@ describe('FrameSyncService', () => {
it('does not send a "publicAnnotationCountChanged" message to the frame if annotation fetch is not complete', function () { it('does not send a "publicAnnotationCountChanged" message to the frame if annotation fetch is not complete', function () {
fakeStore.frames.returns([{ uri: 'http://example.com' }]); fakeStore.frames.returns([{ uri: 'http://example.com' }]);
fakeStore.setState({ fakeStore.setState({
annotations: { annotations: [annotationFixtures.publicAnnotation()] }, annotations: [annotationFixtures.publicAnnotation()],
}); });
assert.isFalse( assert.isFalse(
fakeBridge.call.calledWith('publicAnnotationCountChanged') fakeBridge.call.calledWith('publicAnnotationCountChanged')
...@@ -184,7 +187,7 @@ describe('FrameSyncService', () => { ...@@ -184,7 +187,7 @@ describe('FrameSyncService', () => {
it('does not send a "publicAnnotationCountChanged" message if there are no connected frames', function () { it('does not send a "publicAnnotationCountChanged" message if there are no connected frames', function () {
fakeStore.frames.returns([]); fakeStore.frames.returns([]);
fakeStore.setState({ fakeStore.setState({
annotations: { annotations: [annotationFixtures.publicAnnotation()] }, annotations: [annotationFixtures.publicAnnotation()],
}); });
assert.isFalse( assert.isFalse(
fakeBridge.call.calledWith('publicAnnotationCountChanged') fakeBridge.call.calledWith('publicAnnotationCountChanged')
...@@ -195,10 +198,10 @@ describe('FrameSyncService', () => { ...@@ -195,10 +198,10 @@ describe('FrameSyncService', () => {
context('when annotations are removed from the sidebar', function () { context('when annotations are removed from the sidebar', function () {
it('sends a "deleteAnnotation" message to the frame', function () { it('sends a "deleteAnnotation" message to the frame', function () {
fakeStore.setState({ fakeStore.setState({
annotations: { annotations: [fixtures.ann] }, annotations: [fixtures.ann],
}); });
fakeStore.setState({ fakeStore.setState({
annotations: { annotations: [] }, annotations: [],
}); });
assert.calledWithMatch( assert.calledWithMatch(
fakeBridge.call, fakeBridge.call,
......
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