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

Namespace the frames module

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