Commit 63d39712 authored by Kyle Keating's avatar Kyle Keating

Namespace the frames module

parent 1e62dac0
...@@ -5,25 +5,21 @@ const { createSelector } = require('reselect'); ...@@ -5,25 +5,21 @@ const { createSelector } = require('reselect');
const util = require('../util'); const util = require('../util');
function init() { function init() {
return {
// The list of frames connected to the sidebar app // The list of frames connected to the sidebar app
frames: [], return [];
};
} }
const update = { const update = {
CONNECT_FRAME: function(state, action) { CONNECT_FRAME: function(state, action) {
return { frames: state.frames.concat(action.frame) }; return [...state, action.frame];
}, },
DESTROY_FRAME: function(state, action) { DESTROY_FRAME: function(state, action) {
return { return state.filter(f => f !== action.frame);
frames: state.frames.filter(f => f !== action.frame),
};
}, },
UPDATE_FRAME_ANNOTATION_FETCH_STATUS: function(state, action) { UPDATE_FRAME_ANNOTATION_FETCH_STATUS: function(state, action) {
const frames = state.frames.map(function(frame) { const frames = state.map(function(frame) {
const match = frame.uri && frame.uri === action.uri; const match = frame.uri && frame.uri === action.uri;
if (match) { if (match) {
return Object.assign({}, frame, { return Object.assign({}, frame, {
...@@ -33,9 +29,7 @@ const update = { ...@@ -33,9 +29,7 @@ const update = {
return frame; return frame;
} }
}); });
return { return frames;
frames: frames,
};
}, },
}; };
...@@ -123,6 +117,7 @@ function searchUris(state) { ...@@ -123,6 +117,7 @@ function searchUris(state) {
module.exports = { module.exports = {
init: init, init: init,
namespace: 'frames',
update: update, update: update,
actions: { actions: {
......
...@@ -3,33 +3,19 @@ ...@@ -3,33 +3,19 @@
const frames = require('../frames'); const frames = require('../frames');
const createStore = require('../../create-store'); const createStore = require('../../create-store');
const session = require('../session');
const util = require('../../util');
const unroll = require('../../../../shared/test/util').unroll;
const actions = frames.actions;
const update = util.createReducer(frames.update);
const selectors = frames.selectors;
function init() {
return Object.assign({}, frames.init(), session.init());
}
describe('sidebar/store/modules/frames', function() { describe('sidebar/store/modules/frames', function() {
let store; let store;
beforeEach(() => { beforeEach(() => {
// Setup a store for tests. Note that some of the tests in this module // Setup a store for tests.
// pre-date the `createStore` helper and have not been refactored to use
// it yet.
store = createStore([frames]); store = createStore([frames]);
}); });
describe('#connectFrame', function() { describe('#connectFrame', function() {
it('adds the frame to the list of connected frames', function() { it('adds the frame to the list of connected frames', function() {
const frame = { uri: 'http://example.com' }; const frame = { uri: 'http://example.com' };
const state = update(init(), actions.connectFrame(frame)); store.connectFrame(frame);
assert.deepEqual(selectors.frames(state), [frame]); assert.deepEqual(store.frames(), [frame]);
}); });
}); });
...@@ -39,13 +25,10 @@ describe('sidebar/store/modules/frames', function() { ...@@ -39,13 +25,10 @@ describe('sidebar/store/modules/frames', function() {
{ uri: 'http://example.com' }, { uri: 'http://example.com' },
{ uri: 'http://example.org' }, { uri: 'http://example.org' },
]; ];
let state = init(); store.connectFrame(frameList[0]);
frameList.forEach(function(frame) { store.connectFrame(frameList[1]);
state = update(state, actions.connectFrame(frame)); store.destroyFrame(frameList[0]);
}); assert.deepEqual(store.frames(), [frameList[1]]);
assert.deepEqual(selectors.frames(state), frameList);
const updatedState = update(state, actions.destroyFrame(frameList[0]));
assert.deepEqual(selectors.frames(updatedState), [frameList[1]]);
}); });
}); });
...@@ -58,27 +41,18 @@ describe('sidebar/store/modules/frames', function() { ...@@ -58,27 +41,18 @@ describe('sidebar/store/modules/frames', function() {
uri: 'http://example.com', uri: 'http://example.com',
isAnnotationFetchComplete: true, isAnnotationFetchComplete: true,
}; };
const connectedState = update(init(), actions.connectFrame(frame)); store.connectFrame(frame);
const updatedState = update( store.updateFrameAnnotationFetchStatus(frame.uri, true);
connectedState, assert.deepEqual(store.frames(), [expectedFrame]);
actions.updateFrameAnnotationFetchStatus(frame.uri, true)
);
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() {
const frame = { const frame = {
uri: 'http://example.com', uri: 'http://example.com',
}; };
const connectedState = update(init(), actions.connectFrame(frame)); store.connectFrame(frame);
const updatedState = update( store.updateFrameAnnotationFetchStatus('http://anotherexample.com', true);
connectedState, assert.deepEqual(store.frames(), [frame]);
actions.updateFrameAnnotationFetchStatus(
'http://anotherexample.com',
true
)
);
assert.deepEqual(selectors.frames(updatedState), [frame]);
}); });
}); });
...@@ -110,19 +84,7 @@ describe('sidebar/store/modules/frames', function() { ...@@ -110,19 +84,7 @@ describe('sidebar/store/modules/frames', function() {
}); });
}); });
describe('#searchUris', function() { describe('xxx #searchUris', function() {
unroll(
'returns the expected search URIs (#when)',
function(testCase) {
let state = init();
if (testCase.features) {
state.session.features = testCase.features;
}
testCase.frames.forEach(function(frame) {
state = update(state, actions.connectFrame(frame));
});
assert.deepEqual(selectors.searchUris(state), testCase.searchUris);
},
[ [
{ {
when: 'one HTML frame', when: 'one HTML frame',
...@@ -192,7 +154,13 @@ describe('sidebar/store/modules/frames', function() { ...@@ -192,7 +154,13 @@ describe('sidebar/store/modules/frames', function() {
], ],
searchUris: ['https://publisher.org/article.html', 'doi:10.1.1/1234'], searchUris: ['https://publisher.org/article.html', 'doi:10.1.1/1234'],
}, },
] ].forEach(testCase => {
); it(testCase.when, () => {
testCase.frames.forEach(frame => {
store.connectFrame(frame);
});
assert.deepEqual(store.searchUris(), testCase.searchUris);
});
});
}); });
}); });
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