Commit dcf77f92 authored by Robert Knight's avatar Robert Knight

Refactor export of bound selectors from Redux store

Group the selectors in each Redux module together in a `selectors`
object so they can be re-exported as methods on the Redux store without
having to list each of them manually.

This also just makes it easier to visually see which exported functions
from a Redux module are selectors.
parent d78a4e4b
...@@ -109,23 +109,14 @@ module.exports = function ($rootScope, settings) { ...@@ -109,23 +109,14 @@ module.exports = function ($rootScope, settings) {
// selection.isAnnotationSelected(annotationUI.getState(), id) // selection.isAnnotationSelected(annotationUI.getState(), id)
// You can use: // You can use:
// annotationUI.isAnnotationSelected(id) // annotationUI.isAnnotationSelected(id)
var selectors = util.bindSelectors({ var selectors = util.bindSelectors(Object.assign({},
isAnnotationSelected: selectionReducer.isAnnotationSelected, annotationsReducer.selectors,
hasSelectedAnnotations: selectionReducer.hasSelectedAnnotations, framesReducer.selectors,
linksReducer.selectors,
annotationExists: annotationsReducer.annotationExists, selectionReducer.selectors,
findAnnotationByID: annotationsReducer.findAnnotationByID, sessionReducer.selectors,
findIDsForTags: annotationsReducer.findIDsForTags, viewerReducer.selectors
savedAnnotations: annotationsReducer.savedAnnotations, ), store.getState);
frames: framesReducer.frames,
searchUris: framesReducer.searchUris,
isSidebar: viewerReducer.isSidebar,
isFeatureEnabled: sessionReducer.isFeatureEnabled,
profile: sessionReducer.profile,
}, store.getState);
return Object.assign(store, actionCreators, selectors); return Object.assign(store, actionCreators, selectors);
}; };
...@@ -389,9 +389,10 @@ module.exports = { ...@@ -389,9 +389,10 @@ module.exports = {
unhideAnnotation: unhideAnnotation, unhideAnnotation: unhideAnnotation,
}, },
// Selectors selectors: {
annotationExists: annotationExists, annotationExists,
findAnnotationByID: findAnnotationByID, findAnnotationByID,
findIDsForTags: findIDsForTags, findIDsForTags,
savedAnnotations: savedAnnotations, savedAnnotations,
},
}; };
...@@ -111,7 +111,8 @@ module.exports = { ...@@ -111,7 +111,8 @@ module.exports = {
updateFrameAnnotationFetchStatus: updateFrameAnnotationFetchStatus, updateFrameAnnotationFetchStatus: updateFrameAnnotationFetchStatus,
}, },
// Selectors selectors: {
frames: frames, frames,
searchUris: searchUris, searchUris,
},
}; };
...@@ -24,4 +24,5 @@ module.exports = { ...@@ -24,4 +24,5 @@ module.exports = {
init: init, init: init,
update: { UPDATE_LINKS: updateLinks }, update: { UPDATE_LINKS: updateLinks },
actions: { updateLinks: updateLinksAction }, actions: { updateLinks: updateLinksAction },
selectors: {},
}; };
...@@ -311,7 +311,8 @@ module.exports = { ...@@ -311,7 +311,8 @@ module.exports = {
toggleSelectedAnnotations: toggleSelectedAnnotations, toggleSelectedAnnotations: toggleSelectedAnnotations,
}, },
// Selectors selectors: {
hasSelectedAnnotations: hasSelectedAnnotations, hasSelectedAnnotations,
isAnnotationSelected: isAnnotationSelected, isAnnotationSelected,
},
}; };
...@@ -73,7 +73,8 @@ module.exports = { ...@@ -73,7 +73,8 @@ module.exports = {
updateSession, updateSession,
}, },
// Selectors selectors: {
isFeatureEnabled, isFeatureEnabled,
profile, profile,
},
}; };
...@@ -9,7 +9,7 @@ var fixtures = require('../../test/annotation-fixtures'); ...@@ -9,7 +9,7 @@ var fixtures = require('../../test/annotation-fixtures');
var util = require('../util'); var util = require('../util');
var unroll = require('../../../shared/test/util').unroll; var unroll = require('../../../shared/test/util').unroll;
var actions = annotations.actions; var { actions, selectors } = annotations;
/** /**
* Create a Redux store which only handles annotation actions. * Create a Redux store which only handles annotation actions.
...@@ -26,7 +26,7 @@ function createStore() { ...@@ -26,7 +26,7 @@ function createStore() {
describe('annotations reducer', function () { describe('annotations reducer', function () {
describe('#savedAnnotations', function () { describe('#savedAnnotations', function () {
var savedAnnotations = annotations.savedAnnotations; var savedAnnotations = selectors.savedAnnotations;
it('returns annotations which are saved', function () { it('returns annotations which are saved', function () {
var state = { var state = {
...@@ -37,7 +37,7 @@ describe('annotations reducer', function () { ...@@ -37,7 +37,7 @@ describe('annotations reducer', function () {
}); });
describe('#findIDsForTags', function () { describe('#findIDsForTags', function () {
var findIDsForTags = annotations.findIDsForTags; var findIDsForTags = selectors.findIDsForTags;
it('returns the IDs corresponding to the provided local tags', function () { it('returns the IDs corresponding to the provided local tags', function () {
var ann = fixtures.defaultAnnotation(); var ann = fixtures.defaultAnnotation();
...@@ -64,7 +64,7 @@ describe('annotations reducer', function () { ...@@ -64,7 +64,7 @@ describe('annotations reducer', function () {
store.dispatch(actions.addAnnotations([ann])); store.dispatch(actions.addAnnotations([ann]));
store.dispatch(actions.hideAnnotation(ann.id)); store.dispatch(actions.hideAnnotation(ann.id));
var storeAnn = annotations.findAnnotationByID(store.getState(), ann.id); var storeAnn = selectors.findAnnotationByID(store.getState(), ann.id);
assert.equal(storeAnn.hidden, true); assert.equal(storeAnn.hidden, true);
}); });
}); });
...@@ -77,7 +77,7 @@ describe('annotations reducer', function () { ...@@ -77,7 +77,7 @@ describe('annotations reducer', function () {
store.dispatch(actions.addAnnotations([ann])); store.dispatch(actions.addAnnotations([ann]));
store.dispatch(actions.unhideAnnotation(ann.id)); store.dispatch(actions.unhideAnnotation(ann.id));
var storeAnn = annotations.findAnnotationByID(store.getState(), ann.id); var storeAnn = selectors.findAnnotationByID(store.getState(), ann.id);
assert.equal(storeAnn.hidden, false); assert.equal(storeAnn.hidden, false);
}); });
}); });
...@@ -92,7 +92,7 @@ describe('annotations reducer', function () { ...@@ -92,7 +92,7 @@ describe('annotations reducer', function () {
store.dispatch(actions.addAnnotations([ann])); store.dispatch(actions.addAnnotations([ann]));
store.dispatch(actions.updateFlagStatus(ann.id, testCase.nowFlagged)); store.dispatch(actions.updateFlagStatus(ann.id, testCase.nowFlagged));
var storeAnn = annotations.findAnnotationByID(store.getState(), ann.id); var storeAnn = selectors.findAnnotationByID(store.getState(), ann.id);
assert.equal(storeAnn.flagged, testCase.nowFlagged); assert.equal(storeAnn.flagged, testCase.nowFlagged);
assert.deepEqual(storeAnn.moderation, testCase.newModeration); assert.deepEqual(storeAnn.moderation, testCase.newModeration);
}, [{ }, [{
......
...@@ -7,6 +7,7 @@ var unroll = require('../../../shared/test/util').unroll; ...@@ -7,6 +7,7 @@ var unroll = require('../../../shared/test/util').unroll;
var actions = frames.actions; var actions = frames.actions;
var update = util.createReducer(frames.update); var update = util.createReducer(frames.update);
var selectors = frames.selectors;
function init() { function init() {
return Object.assign({}, frames.init(), session.init()); return Object.assign({}, frames.init(), session.init());
...@@ -17,7 +18,7 @@ describe('frames reducer', function () { ...@@ -17,7 +18,7 @@ describe('frames reducer', function () {
it('adds the frame to the list of connected frames', function () { it('adds the frame to the list of connected frames', function () {
var frame = {uri: 'http://example.com'}; var frame = {uri: 'http://example.com'};
var state = update(init(), actions.connectFrame(frame)); var state = update(init(), actions.connectFrame(frame));
assert.deepEqual(frames.frames(state), [frame]); assert.deepEqual(selectors.frames(state), [frame]);
}); });
}); });
...@@ -28,9 +29,9 @@ describe('frames reducer', function () { ...@@ -28,9 +29,9 @@ describe('frames reducer', function () {
frameList.forEach(function (frame) { frameList.forEach(function (frame) {
state = update(state, actions.connectFrame(frame)); state = update(state, actions.connectFrame(frame));
}); });
assert.deepEqual(frames.frames(state), frameList); assert.deepEqual(selectors.frames(state), frameList);
var updatedState = update(state, actions.destroyFrame(frameList[0])); var updatedState = update(state, actions.destroyFrame(frameList[0]));
assert.deepEqual(frames.frames(updatedState), [frameList[1]]); assert.deepEqual(selectors.frames(updatedState), [frameList[1]]);
}); });
}); });
...@@ -46,7 +47,7 @@ describe('frames reducer', function () { ...@@ -46,7 +47,7 @@ describe('frames reducer', function () {
var connectedState = update(init(), actions.connectFrame(frame)); var connectedState = update(init(), actions.connectFrame(frame));
var updatedState = update(connectedState, var updatedState = update(connectedState,
actions.updateFrameAnnotationFetchStatus(frame.uri, true)); actions.updateFrameAnnotationFetchStatus(frame.uri, true));
assert.deepEqual(frames.frames(updatedState), [expectedFrame]); assert.deepEqual(selectors.frames(updatedState), [expectedFrame]);
}); });
it('does not update the isAnnotationFetchComplete status of the wrong frame', function () { it('does not update the isAnnotationFetchComplete status of the wrong frame', function () {
...@@ -56,7 +57,7 @@ describe('frames reducer', function () { ...@@ -56,7 +57,7 @@ describe('frames reducer', function () {
var connectedState = update(init(), actions.connectFrame(frame)); var connectedState = update(init(), actions.connectFrame(frame));
var updatedState = update(connectedState, var updatedState = update(connectedState,
actions.updateFrameAnnotationFetchStatus('http://anotherexample.com', true)); actions.updateFrameAnnotationFetchStatus('http://anotherexample.com', true));
assert.deepEqual(frames.frames(updatedState), [frame]); assert.deepEqual(selectors.frames(updatedState), [frame]);
}); });
}); });
...@@ -69,7 +70,7 @@ describe('frames reducer', function () { ...@@ -69,7 +70,7 @@ describe('frames reducer', function () {
testCase.frames.forEach(function (frame) { testCase.frames.forEach(function (frame) {
state = update(state, actions.connectFrame(frame)); state = update(state, actions.connectFrame(frame));
}); });
assert.deepEqual(frames.searchUris(state), testCase.searchUris); assert.deepEqual(selectors.searchUris(state), testCase.searchUris);
},[{ },[{
when: 'one HTML frame', when: 'one HTML frame',
frames: [{ frames: [{
......
...@@ -4,8 +4,7 @@ var session = require('../session'); ...@@ -4,8 +4,7 @@ var session = require('../session');
var util = require('../util'); var util = require('../util');
var init = session.init; var { init, actions, selectors } = session;
var actions = session.actions;
var update = util.createReducer(session.update); var update = util.createReducer(session.update);
describe('sidebar.reducers.session', function () { describe('sidebar.reducers.session', function () {
...@@ -21,7 +20,7 @@ describe('sidebar.reducers.session', function () { ...@@ -21,7 +20,7 @@ describe('sidebar.reducers.session', function () {
it("returns the user's profile", () => { it("returns the user's profile", () => {
var newSession = Object.assign(init(), {userid: 'john'}); var newSession = Object.assign(init(), {userid: 'john'});
var state = update(init(), actions.updateSession(newSession)); var state = update(init(), actions.updateSession(newSession));
assert.equal(session.profile(state), newSession); assert.equal(selectors.profile(state), newSession);
}); });
}); });
}); });
...@@ -4,20 +4,19 @@ var viewer = require('../viewer'); ...@@ -4,20 +4,19 @@ var viewer = require('../viewer');
var util = require('../util'); var util = require('../util');
var init = viewer.init; var { init, actions, selectors } = viewer;
var actions = viewer.actions;
var update = util.createReducer(viewer.update); var update = util.createReducer(viewer.update);
describe('viewer reducer', function () { describe('viewer reducer', function () {
describe('#setAppIsSidebar', function () { describe('#setAppIsSidebar', function () {
it('sets a flag indicating that the app is the sidebar', function () { it('sets a flag indicating that the app is the sidebar', function () {
var state = update(init(), actions.setAppIsSidebar(true)); var state = update(init(), actions.setAppIsSidebar(true));
assert.isTrue(viewer.isSidebar(state)); assert.isTrue(selectors.isSidebar(state));
}); });
it('sets a flag indicating that the app is not the sidebar', function () { it('sets a flag indicating that the app is not the sidebar', function () {
var state = update(init(), actions.setAppIsSidebar(false)); var state = update(init(), actions.setAppIsSidebar(false));
assert.isFalse(viewer.isSidebar(state)); assert.isFalse(selectors.isSidebar(state));
}); });
}); });
}); });
...@@ -57,6 +57,7 @@ module.exports = { ...@@ -57,6 +57,7 @@ module.exports = {
setShowHighlights: setShowHighlights, setShowHighlights: setShowHighlights,
}, },
// Selectors selectors: {
isSidebar: isSidebar, isSidebar,
},
}; };
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