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 return [];
frames: [],
};
} }
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,89 +84,83 @@ describe('sidebar/store/modules/frames', function() { ...@@ -110,89 +84,83 @@ describe('sidebar/store/modules/frames', function() {
}); });
}); });
describe('#searchUris', function() { describe('xxx #searchUris', function() {
unroll( [
'returns the expected search URIs (#when)', {
function(testCase) { when: 'one HTML frame',
let state = init(); frames: [
if (testCase.features) { {
state.session.features = testCase.features; uri: 'https://publisher.org/article.html',
} },
testCase.frames.forEach(function(frame) { ],
state = update(state, actions.connectFrame(frame)); searchUris: ['https://publisher.org/article.html'],
});
assert.deepEqual(selectors.searchUris(state), testCase.searchUris);
}, },
[ {
{ when: 'one PDF frame',
when: 'one HTML frame', frames: [
frames: [ {
{ uri: 'https://publisher.org/article.pdf',
uri: 'https://publisher.org/article.html', metadata: {
}, documentFingerprint: '1234',
], link: [
searchUris: ['https://publisher.org/article.html'], {
}, href: 'urn:x-pdf:1234',
{ },
when: 'one PDF frame', {
frames: [ // When a document fingerprint is provided, we currently rely on the
{ // host frame to include the original URL of the document in the
uri: 'https://publisher.org/article.pdf', // `metadata.link` list.
metadata: { //
documentFingerprint: '1234', // This may be omitted if the URI is a `file:///` URI.
link: [ href: 'https://publisher.org/article.pdf?from_meta_link=1',
{ },
href: 'urn:x-pdf:1234', ],
},
{
// When a document fingerprint is provided, we currently rely on the
// host frame to include the original URL of the document in the
// `metadata.link` list.
//
// This may be omitted if the URI is a `file:///` URI.
href: 'https://publisher.org/article.pdf?from_meta_link=1',
},
],
},
},
],
searchUris: [
'urn:x-pdf:1234',
'https://publisher.org/article.pdf?from_meta_link=1',
],
},
{
when: 'multiple HTML frames',
frames: [
{
uri: 'https://publisher.org/article.html',
},
{
uri: 'https://publisher.org/article2.html',
}, },
], },
searchUris: [ ],
'https://publisher.org/article.html', searchUris: [
'https://publisher.org/article2.html', 'urn:x-pdf:1234',
], 'https://publisher.org/article.pdf?from_meta_link=1',
}, ],
{ },
when: 'the document metadata includes a DOI', {
frames: [ when: 'multiple HTML frames',
{ frames: [
uri: 'https://publisher.org/article.html', {
metadata: { uri: 'https://publisher.org/article.html',
link: [ },
{ {
href: 'doi:10.1.1/1234', uri: 'https://publisher.org/article2.html',
}, },
], ],
}, searchUris: [
'https://publisher.org/article.html',
'https://publisher.org/article2.html',
],
},
{
when: 'the document metadata includes a DOI',
frames: [
{
uri: 'https://publisher.org/article.html',
metadata: {
link: [
{
href: 'doi:10.1.1/1234',
},
],
}, },
], },
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