Split packages into platform and extensions
This commit is contained in:
1 parent
f175d4b96b
commit
e8969fba11
992 files changed
+2
-1
No files matched your search
@@ -0,0 +1,114 @@
|
||||
/** Action Creators:
|
||||
* https://redux.js.org/basics/actions#action-creators
|
||||
*/
|
||||
|
||||
import {
|
||||
CLEAR_VIEWPORT,
|
||||
SET_ACTIVE_SPECIFIC_DATA,
|
||||
SET_SERVERS,
|
||||
SET_VIEWPORT,
|
||||
SET_VIEWPORT_ACTIVE,
|
||||
SET_VIEWPORT_LAYOUT,
|
||||
SET_VIEWPORT_LAYOUT_AND_DATA,
|
||||
} from './constants/ActionTypes.js';
|
||||
|
||||
/**
|
||||
* VIEWPORT
|
||||
*/
|
||||
export const setViewportSpecificData = (viewportIndex, data) => ({
|
||||
type: SET_VIEWPORT,
|
||||
viewportIndex,
|
||||
data,
|
||||
});
|
||||
|
||||
export const setViewportActive = viewportIndex => ({
|
||||
type: SET_VIEWPORT_ACTIVE,
|
||||
viewportIndex,
|
||||
});
|
||||
|
||||
export const setLayout = layout => ({
|
||||
type: SET_VIEWPORT_LAYOUT,
|
||||
layout,
|
||||
});
|
||||
|
||||
export const clearViewportSpecificData = viewportIndex => ({
|
||||
type: CLEAR_VIEWPORT,
|
||||
viewportIndex,
|
||||
});
|
||||
|
||||
export const setActiveViewportSpecificData = data => ({
|
||||
type: SET_ACTIVE_SPECIFIC_DATA,
|
||||
data,
|
||||
});
|
||||
|
||||
/**
|
||||
* NOT-VIEWPORT
|
||||
*/
|
||||
export const setStudyLoadingProgress = (progressId, progressData) => ({
|
||||
type: 'SET_STUDY_LOADING_PROGRESS',
|
||||
progressId,
|
||||
progressData,
|
||||
});
|
||||
|
||||
export const clearStudyLoadingProgress = progressId => ({
|
||||
type: 'CLEAR_STUDY_LOADING_PROGRESS',
|
||||
progressId,
|
||||
});
|
||||
|
||||
export const setUserPreferences = state => ({
|
||||
type: 'SET_USER_PREFERENCES',
|
||||
state,
|
||||
});
|
||||
|
||||
export const setExtensionData = (extension, data) => ({
|
||||
type: 'SET_EXTENSION_DATA',
|
||||
extension,
|
||||
data,
|
||||
});
|
||||
|
||||
export const setTimepoints = state => ({
|
||||
type: 'SET_TIMEPOINTS',
|
||||
state,
|
||||
});
|
||||
|
||||
export const setMeasurements = state => ({
|
||||
type: 'SET_MEASUREMENTS',
|
||||
state,
|
||||
});
|
||||
|
||||
export const setStudyData = (studyInstanceUid, data) => ({
|
||||
type: 'SET_STUDY_DATA',
|
||||
studyInstanceUid,
|
||||
data,
|
||||
});
|
||||
|
||||
export const setServers = servers => ({
|
||||
type: SET_SERVERS,
|
||||
servers,
|
||||
});
|
||||
|
||||
export const setViewportLayoutAndData = (layout, viewportSpecificData) => ({
|
||||
type: SET_VIEWPORT_LAYOUT_AND_DATA,
|
||||
layout,
|
||||
viewportSpecificData,
|
||||
});
|
||||
|
||||
const actions = {
|
||||
// VIEWPORT
|
||||
setViewportActive,
|
||||
setViewportSpecificData,
|
||||
setViewportLayoutAndData,
|
||||
setLayout,
|
||||
clearViewportSpecificData,
|
||||
setActiveViewportSpecificData,
|
||||
setStudyLoadingProgress,
|
||||
clearStudyLoadingProgress,
|
||||
setUserPreferences,
|
||||
setExtensionData,
|
||||
setTimepoints,
|
||||
setMeasurements,
|
||||
setStudyData,
|
||||
setServers,
|
||||
};
|
||||
|
||||
export default actions;
|
||||
@@ -0,0 +1,112 @@
|
||||
import * as types from './constants/ActionTypes.js';
|
||||
|
||||
import actions from './actions.js';
|
||||
|
||||
describe('actions', () => {
|
||||
test('exports have not changed', () => {
|
||||
const expectedExports = [
|
||||
'setViewportActive',
|
||||
'setViewportSpecificData',
|
||||
'setViewportLayoutAndData',
|
||||
'setLayout',
|
||||
'clearViewportSpecificData',
|
||||
'setActiveViewportSpecificData',
|
||||
'setStudyLoadingProgress',
|
||||
'clearStudyLoadingProgress',
|
||||
'setUserPreferences',
|
||||
'setExtensionData',
|
||||
'setTimepoints',
|
||||
'setMeasurements',
|
||||
'setServers',
|
||||
'setStudyData',
|
||||
].sort();
|
||||
|
||||
const exports = Object.keys(actions).sort();
|
||||
|
||||
expect(exports).toEqual(expectedExports);
|
||||
});
|
||||
|
||||
describe('viewport action creators', () => {
|
||||
it('should create an action to set the viewport specific data', () => {
|
||||
const data = {
|
||||
displaySetInstanceUid: 'ef859a23-4631-93ab-d26b-7940a822c699',
|
||||
seriesDate: '20151026',
|
||||
seriesTime: '082611.370000',
|
||||
seriesInstanceUid:
|
||||
'1.3.6.1.4.1.25403.345050719074.3824.20170126085406.5',
|
||||
seriesNumber: 2,
|
||||
seriesDescription: 'Chest 3x3 Soft',
|
||||
numImageFrames: 126,
|
||||
modality: 'CT',
|
||||
isMultiFrame: false,
|
||||
instanceNumber: 1,
|
||||
studyInstanceUid:
|
||||
'1.3.6.1.4.1.25403.345050719074.3824.20170126085406.1',
|
||||
sopClassUids: ['1.2.840.10008.5.1.4.1.1.2'],
|
||||
plugin: 'cornerstone',
|
||||
viewport: {
|
||||
zoomScale: null,
|
||||
rotation: 360,
|
||||
resetViewport: null,
|
||||
invert: null,
|
||||
vflip: null,
|
||||
hflip: null,
|
||||
clearTools: null,
|
||||
scrollUp: null,
|
||||
scrollDown: null,
|
||||
scrollFirstImage: null,
|
||||
scrollLastImage: null,
|
||||
previousPanel: null,
|
||||
nextPanel: null,
|
||||
nextSeries: null,
|
||||
previousSeries: null,
|
||||
},
|
||||
};
|
||||
|
||||
const expectedAction = {
|
||||
type: types.SET_ACTIVE_SPECIFIC_DATA,
|
||||
data,
|
||||
};
|
||||
|
||||
expect(actions.setActiveViewportSpecificData(data)).toEqual(
|
||||
expectedAction
|
||||
);
|
||||
});
|
||||
|
||||
it('should create an action to clear clearViewportSpecificData', () => {
|
||||
const viewportIndex = 1;
|
||||
const expectedAction = {
|
||||
type: types.CLEAR_VIEWPORT,
|
||||
viewportIndex,
|
||||
};
|
||||
expect(actions.clearViewportSpecificData(viewportIndex)).toEqual(
|
||||
expectedAction
|
||||
);
|
||||
});
|
||||
|
||||
it('should create an action to set the active viewport', () => {
|
||||
const viewportIndex = 1;
|
||||
const expectedAction = {
|
||||
type: types.SET_VIEWPORT_ACTIVE,
|
||||
viewportIndex,
|
||||
};
|
||||
expect(actions.setViewportActive(viewportIndex)).toEqual(expectedAction);
|
||||
});
|
||||
|
||||
it('should create an action to set the viewport layout', () => {
|
||||
const layout = {
|
||||
viewports: [
|
||||
{
|
||||
height: '100%',
|
||||
width: '100%',
|
||||
},
|
||||
],
|
||||
};
|
||||
const expectedAction = {
|
||||
type: types.SET_VIEWPORT_LAYOUT,
|
||||
layout,
|
||||
};
|
||||
expect(actions.setLayout(layout)).toEqual(expectedAction);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* VIEWPORT
|
||||
*/
|
||||
export const SET_VIEWPORT = 'VIEWPORT::SET';
|
||||
export const SET_VIEWPORT_ACTIVE = 'VIEWPORT::SET_ACTIVE';
|
||||
export const SET_VIEWPORT_LAYOUT = 'VIEWPORT::SET_LAYOUT';
|
||||
export const SET_VIEWPORT_LAYOUT_AND_DATA =
|
||||
'VIEWPORT::SET_VIEWPORT_LAYOUT_AND_DATA';
|
||||
export const CLEAR_VIEWPORT = 'VIEWPORT::CLEAR';
|
||||
export const SET_SPECIFIC_DATA = 'VIEWPORT::SET_SPECIFIC_DATA';
|
||||
export const SET_ACTIVE_SPECIFIC_DATA = 'VIEWPORT::SET_ACTIVE_SPECIFIC_DATA';
|
||||
|
||||
/**
|
||||
* SERVERS
|
||||
*/
|
||||
export const ADD_SERVER = 'ADD_SERVER';
|
||||
export const SET_SERVERS = 'SET_SERVERS';
|
||||
|
||||
/**
|
||||
* EXTENSIONS
|
||||
*/
|
||||
export const SET_EXTENSION_DATA = 'SET_EXTENSION_DATA';
|
||||
@@ -0,0 +1,11 @@
|
||||
import actions from './actions.js';
|
||||
import reducers from './reducers';
|
||||
import localStorage from './localStorage.js';
|
||||
|
||||
const redux = {
|
||||
reducers,
|
||||
actions,
|
||||
localStorage,
|
||||
};
|
||||
|
||||
export default redux;
|
||||
@@ -0,0 +1,11 @@
|
||||
import redux from './index.js';
|
||||
|
||||
describe('redux exports', () => {
|
||||
test('have not changed', () => {
|
||||
const expectedExports = ['actions', 'reducers', 'localStorage'].sort();
|
||||
|
||||
const exports = Object.keys(redux).sort();
|
||||
|
||||
expect(exports).toEqual(expectedExports);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
export const loadState = () => {
|
||||
try {
|
||||
const serializedState = window.localStorage.getItem('state');
|
||||
if (!serializedState) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return JSON.parse(serializedState);
|
||||
} catch (e) {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
export const saveState = state => {
|
||||
try {
|
||||
const serializedState = JSON.stringify(state);
|
||||
localStorage.setItem('state', serializedState);
|
||||
} catch (e) {}
|
||||
};
|
||||
|
||||
const localStorage = {
|
||||
saveState,
|
||||
loadState,
|
||||
};
|
||||
|
||||
export default localStorage;
|
||||
@@ -0,0 +1,25 @@
|
||||
export const defaultState = {};
|
||||
|
||||
const extensions = (state = defaultState, action) => {
|
||||
switch (action.type) {
|
||||
case 'SET_EXTENSION_DATA':
|
||||
const extensionName = action.extension;
|
||||
const currentData = state[extensionName] || {};
|
||||
|
||||
const incomingData = action.data;
|
||||
|
||||
const extension = {
|
||||
[extensionName]: {
|
||||
...currentData,
|
||||
...incomingData,
|
||||
},
|
||||
};
|
||||
|
||||
return { ...state, ...extension };
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export default extensions;
|
||||
@@ -0,0 +1,50 @@
|
||||
import { Reducer } from 'redux-testkit';
|
||||
|
||||
import reducer, { defaultState } from './extensions';
|
||||
import * as actionTypes from './../constants/ActionTypes.js';
|
||||
|
||||
describe('viewports reducer', () => {
|
||||
it('should return the initial state', () => {
|
||||
expect(reducer(undefined, {})).toEqual(defaultState);
|
||||
});
|
||||
|
||||
it('should set new data on first SET_EXTENSION_DATA', () => {
|
||||
const initialState = defaultState;
|
||||
|
||||
const action = {
|
||||
type: actionTypes.SET_EXTENSION_DATA,
|
||||
extension: 'uber plugin',
|
||||
data: { greeting: 'Hello!' },
|
||||
};
|
||||
|
||||
const expectedState = {
|
||||
'uber plugin': { greeting: 'Hello!' },
|
||||
};
|
||||
|
||||
Reducer(reducer)
|
||||
.withState(initialState)
|
||||
.expect(action)
|
||||
.toReturnState(expectedState);
|
||||
});
|
||||
|
||||
it('should shallow-merge extension data, keeping unmodified fields, on SET_EXTENSION_DATA', () => {
|
||||
const initialState = {
|
||||
'uber plugin': { greeting: 'Hello!', "Can't touch this": 42 },
|
||||
};
|
||||
|
||||
const action = {
|
||||
type: actionTypes.SET_EXTENSION_DATA,
|
||||
extension: 'uber plugin',
|
||||
data: { greeting: 'Aloha!' },
|
||||
};
|
||||
|
||||
const expectedState = {
|
||||
'uber plugin': { greeting: 'Aloha!', "Can't touch this": 42 },
|
||||
};
|
||||
|
||||
Reducer(reducer)
|
||||
.withState(initialState)
|
||||
.expect(action)
|
||||
.toReturnState(expectedState);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
import extensions from './extensions';
|
||||
import loading from './loading';
|
||||
import preferences from './preferences';
|
||||
import servers from './servers';
|
||||
import studies from './studies';
|
||||
import timepointManager from './timepointManager';
|
||||
import viewports from './viewports';
|
||||
|
||||
const reducers = {
|
||||
extensions,
|
||||
loading,
|
||||
preferences,
|
||||
servers,
|
||||
studies,
|
||||
timepointManager,
|
||||
viewports,
|
||||
};
|
||||
|
||||
export default reducers;
|
||||
@@ -0,0 +1,33 @@
|
||||
import cloneDeep from 'lodash.clonedeep';
|
||||
|
||||
const defaultState = {
|
||||
progress: {},
|
||||
lastUpdated: null,
|
||||
};
|
||||
|
||||
const loading = (state = defaultState, action) => {
|
||||
let progress;
|
||||
let lastUpdated;
|
||||
switch (action.type) {
|
||||
case 'SET_STUDY_LOADING_PROGRESS':
|
||||
progress = cloneDeep(state).progress;
|
||||
progress[action.progressId] = action.progressData;
|
||||
|
||||
// This is a workaround so we can easily identify changes
|
||||
// to the progress object without doing deep comparison.
|
||||
lastUpdated = new Date().getTime();
|
||||
|
||||
return Object.assign({}, state, { progress, lastUpdated });
|
||||
case 'CLEAR_STUDY_LOADING_PROGRESS':
|
||||
progress = cloneDeep(state).progress;
|
||||
delete progress[action.progressId];
|
||||
|
||||
lastUpdated = new Date().getTime();
|
||||
|
||||
return Object.assign({}, state, { progress, lastUpdated });
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export default loading;
|
||||
@@ -0,0 +1,31 @@
|
||||
import cloneDeep from 'lodash.clonedeep';
|
||||
|
||||
const defaultState = {
|
||||
// Top level key
|
||||
viewer: {
|
||||
// First tab
|
||||
hotKeysData: {
|
||||
// hotkeyName, label, keys, column
|
||||
// zoom: { label: 'Zoom', command: 'Z', column: 0 },
|
||||
},
|
||||
// Second tab
|
||||
windowLevelData: {
|
||||
// order, description, window (int), level (int)
|
||||
// 0: { description: 'Soft tissue', window: '', level: '' },
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const preferences = (state, action) => {
|
||||
switch (action.type) {
|
||||
case 'SET_USER_PREFERENCES': {
|
||||
const newState = action.state ? action.state : cloneDeep(defaultState);
|
||||
|
||||
return Object.assign({}, state, newState);
|
||||
}
|
||||
default:
|
||||
return cloneDeep(state) || cloneDeep(defaultState);
|
||||
}
|
||||
};
|
||||
|
||||
export default preferences;
|
||||
@@ -0,0 +1,22 @@
|
||||
import uniqBy from 'lodash/uniqBy';
|
||||
|
||||
export const defaultState = {
|
||||
servers: [],
|
||||
};
|
||||
|
||||
const servers = (state = defaultState, action) => {
|
||||
switch (action.type) {
|
||||
case 'ADD_SERVER':
|
||||
let servers = uniqBy([...state.servers, action.server], 'id');
|
||||
servers.forEach(s => (s.active = true));
|
||||
return { ...state, servers };
|
||||
|
||||
case 'SET_SERVERS':
|
||||
return { ...state, servers: action.servers };
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export default servers;
|
||||
@@ -0,0 +1,89 @@
|
||||
import { Reducer } from 'redux-testkit';
|
||||
|
||||
import reducer, { defaultState } from './servers';
|
||||
import * as types from './../constants/ActionTypes.js';
|
||||
|
||||
describe('viewports reducer', () => {
|
||||
it('should return the initial state', () => {
|
||||
expect(reducer(undefined, {})).toEqual(defaultState);
|
||||
});
|
||||
|
||||
it('should result in one server when there were no servers before on ADD_SERVER', () => {
|
||||
const initialState = defaultState;
|
||||
|
||||
const action = { type: types.ADD_SERVER, server: { id: 'some-server-id' } };
|
||||
|
||||
const expectedState = { servers: [action.server] };
|
||||
|
||||
Reducer(reducer)
|
||||
.withState(initialState)
|
||||
.expect(action)
|
||||
.toReturnState(expectedState);
|
||||
});
|
||||
|
||||
it('should add to servers list on ADD_SERVER', () => {
|
||||
const initialState = {
|
||||
servers: [
|
||||
{ id: 'one', active: true },
|
||||
{ id: 'two', active: true },
|
||||
{ id: 'three', active: true },
|
||||
{ id: 'four', active: true },
|
||||
],
|
||||
};
|
||||
|
||||
const action = {
|
||||
type: types.ADD_SERVER,
|
||||
server: { id: 'five', active: true },
|
||||
};
|
||||
|
||||
const expectedState = {
|
||||
servers: [...initialState.servers, action.server],
|
||||
};
|
||||
|
||||
Reducer(reducer)
|
||||
.withState(initialState)
|
||||
.expect(action)
|
||||
.toReturnState(expectedState);
|
||||
});
|
||||
|
||||
it('should not add duplicated servers on ADD_SERVER', () => {
|
||||
const initialState = {
|
||||
servers: [
|
||||
{ id: 'one', active: true },
|
||||
{ id: 'two', active: true },
|
||||
{ id: 'three', active: true },
|
||||
{ id: 'four', active: true },
|
||||
],
|
||||
};
|
||||
|
||||
const action = {
|
||||
type: types.ADD_SERVER,
|
||||
server: { id: 'two', active: true },
|
||||
};
|
||||
|
||||
Reducer(reducer)
|
||||
.withState(initialState)
|
||||
.expect(action)
|
||||
.toReturnState(initialState);
|
||||
});
|
||||
|
||||
it('should replace servers on SET_SERVERS', () => {
|
||||
const initialState = {
|
||||
servers: [{ id: 'one' }, { id: 'two' }, { id: 'three' }, { id: 'four' }],
|
||||
};
|
||||
|
||||
const action = {
|
||||
type: types.SET_SERVERS,
|
||||
servers: [{ id: 'un' }, { id: 'deux' }, { id: 'trois' }],
|
||||
};
|
||||
|
||||
const expectedState = {
|
||||
servers: action.servers,
|
||||
};
|
||||
|
||||
Reducer(reducer)
|
||||
.withState(initialState)
|
||||
.expect(action)
|
||||
.toReturnState(expectedState);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
import cloneDeep from 'lodash.clonedeep';
|
||||
|
||||
const defaultState = {
|
||||
studyData: {},
|
||||
};
|
||||
|
||||
const servers = (state = defaultState, action) => {
|
||||
switch (action.type) {
|
||||
case 'SET_STUDY_DATA':
|
||||
const updatedStudyData = cloneDeep(state).studyData;
|
||||
updatedStudyData[action.studyInstanceUid] = action.data;
|
||||
|
||||
return Object.assign({}, state, { studyData: updatedStudyData });
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export default servers;
|
||||
@@ -0,0 +1,17 @@
|
||||
const defaultState = {
|
||||
timepoints: [],
|
||||
measurements: [],
|
||||
};
|
||||
|
||||
const timepointManager = (state = defaultState, action) => {
|
||||
switch (action.type) {
|
||||
case 'SET_TIMEPOINTS':
|
||||
return Object.assign({}, state, { timepoints: action.state });
|
||||
case 'SET_MEASUREMENTS':
|
||||
return Object.assign({}, state, { measurements: action.state });
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export default timepointManager;
|
||||
@@ -0,0 +1,97 @@
|
||||
import {
|
||||
CLEAR_VIEWPORT,
|
||||
SET_ACTIVE_SPECIFIC_DATA,
|
||||
SET_SPECIFIC_DATA,
|
||||
SET_VIEWPORT,
|
||||
SET_VIEWPORT_ACTIVE,
|
||||
SET_VIEWPORT_LAYOUT,
|
||||
SET_VIEWPORT_LAYOUT_AND_DATA,
|
||||
} from './../constants/ActionTypes.js';
|
||||
|
||||
import cloneDeep from 'lodash.clonedeep';
|
||||
import merge from 'lodash.merge';
|
||||
|
||||
const defaultState = {
|
||||
activeViewportIndex: 0,
|
||||
layout: {
|
||||
viewports: [
|
||||
{
|
||||
// plugin: 'cornerstone',
|
||||
height: '100%',
|
||||
width: '100%',
|
||||
},
|
||||
],
|
||||
},
|
||||
viewportSpecificData: {},
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Object} [state=defaultState]
|
||||
* @param {Object} action
|
||||
* @param {string} [action.type]
|
||||
* @param {number} [action.viewportIndex]
|
||||
* @param {Object} [action.layout]
|
||||
* @param {Object} [action.viewportSpecificData]
|
||||
*/
|
||||
const viewports = (state = defaultState, action) => {
|
||||
let currentData;
|
||||
let viewportSpecificData;
|
||||
let useActiveViewport = false;
|
||||
switch (action.type) {
|
||||
case SET_VIEWPORT_LAYOUT_AND_DATA:
|
||||
return Object.assign({}, state, {
|
||||
viewportSpecificData: action.viewportSpecificData,
|
||||
layout: action.layout,
|
||||
});
|
||||
case SET_VIEWPORT_ACTIVE:
|
||||
return Object.assign({}, state, {
|
||||
activeViewportIndex: action.viewportIndex,
|
||||
});
|
||||
case SET_VIEWPORT_LAYOUT:
|
||||
return Object.assign({}, state, { layout: action.layout });
|
||||
case SET_VIEWPORT: {
|
||||
const layout = cloneDeep(state.layout);
|
||||
const hasPlugin = action.data && action.data.plugin;
|
||||
|
||||
viewportSpecificData = cloneDeep(state.viewportSpecificData);
|
||||
viewportSpecificData[action.viewportIndex] = merge(
|
||||
{},
|
||||
viewportSpecificData[action.viewportIndex],
|
||||
action.data
|
||||
);
|
||||
|
||||
if (hasPlugin) {
|
||||
layout.viewports[action.viewportIndex].plugin = action.data.plugin;
|
||||
}
|
||||
|
||||
return Object.assign({}, state, { layout, viewportSpecificData });
|
||||
}
|
||||
case SET_ACTIVE_SPECIFIC_DATA:
|
||||
useActiveViewport = true;
|
||||
// Allow fall-through
|
||||
// eslint-disable-next-line
|
||||
case SET_SPECIFIC_DATA: {
|
||||
const viewportIndex = useActiveViewport
|
||||
? state.activeViewportIndex
|
||||
: action.viewportIndex;
|
||||
currentData = cloneDeep(state.viewportSpecificData[viewportIndex]) || {};
|
||||
viewportSpecificData = cloneDeep(state.viewportSpecificData);
|
||||
viewportSpecificData[viewportIndex] = merge({}, currentData, action.data);
|
||||
|
||||
return Object.assign({}, state, { viewportSpecificData });
|
||||
}
|
||||
case CLEAR_VIEWPORT:
|
||||
viewportSpecificData = cloneDeep(state.viewportSpecificData);
|
||||
if (action.viewportIndex) {
|
||||
viewportSpecificData[action.viewportIndex] = {};
|
||||
return Object.assign({}, state, { viewportSpecificData });
|
||||
} else {
|
||||
return defaultState;
|
||||
}
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
export default viewports;
|
||||
@@ -0,0 +1,101 @@
|
||||
// https://medium.com/@netxm/testing-redux-reducers-with-jest-6653abbfe3e1
|
||||
import reducer from './viewports.js';
|
||||
import * as types from './../constants/ActionTypes.js';
|
||||
|
||||
describe('viewports reducer', () => {
|
||||
it('should return the initial state', () => {
|
||||
expect(reducer(undefined, {})).toEqual({
|
||||
activeViewportIndex: 0,
|
||||
layout: {
|
||||
viewports: [
|
||||
{
|
||||
height: '100%',
|
||||
width: '100%',
|
||||
},
|
||||
],
|
||||
},
|
||||
viewportSpecificData: {},
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle SET_VIEWPORT_ACTIVE', () => {
|
||||
const setViewportActiveAction = {
|
||||
type: types.SET_VIEWPORT_ACTIVE,
|
||||
viewportIndex: 100,
|
||||
};
|
||||
|
||||
const updatedState = reducer({}, setViewportActiveAction);
|
||||
|
||||
expect(updatedState.activeViewportIndex).toEqual(
|
||||
setViewportActiveAction.viewportIndex
|
||||
);
|
||||
});
|
||||
|
||||
it('should handle SET_VIEWPORT_LAYOUT', () => {
|
||||
const setViewportLayoutAction = {
|
||||
type: types.SET_VIEWPORT_LAYOUT,
|
||||
layout: {
|
||||
viewports: [
|
||||
{
|
||||
height: '100%',
|
||||
width: '50%',
|
||||
},
|
||||
{
|
||||
height: '100%',
|
||||
width: '50%',
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const updatedState = reducer({}, setViewportLayoutAction);
|
||||
|
||||
expect(updatedState.layout).toEqual(setViewportLayoutAction.layout);
|
||||
});
|
||||
|
||||
// If there were previous keys, this would have
|
||||
// "merge" behavior, not a clear & set
|
||||
// May be worth another test?
|
||||
it('should handle SET_VIEWPORT', () => {
|
||||
const viewportToSet = 0;
|
||||
const setViewportAction = {
|
||||
type: types.SET_VIEWPORT,
|
||||
viewportIndex: viewportToSet,
|
||||
data: {
|
||||
hello: 'this is that data for the viewport',
|
||||
world: 'that will be set for the viewportIndex',
|
||||
},
|
||||
};
|
||||
|
||||
const updatedState = reducer(undefined, setViewportAction);
|
||||
const updatedViewport = updatedState.viewportSpecificData[viewportToSet];
|
||||
|
||||
expect(updatedViewport).toEqual(setViewportAction.data);
|
||||
});
|
||||
|
||||
it('should handle CLEAR_VIEWPORT', () => {
|
||||
const existingViewportData = {
|
||||
viewportSpecificData: {
|
||||
0: {
|
||||
viewportProperty: 'hello world',
|
||||
},
|
||||
1: {
|
||||
viewportProperty: 'fizzbuzz',
|
||||
},
|
||||
},
|
||||
};
|
||||
const clearViewportAction = {
|
||||
type: types.CLEAR_VIEWPORT,
|
||||
viewportIndex: 1,
|
||||
};
|
||||
|
||||
const updatedState = reducer(existingViewportData, clearViewportAction);
|
||||
const clearedViewport =
|
||||
updatedState.viewportSpecificData[clearViewportAction.viewportIndex];
|
||||
const originalOtherViewport = existingViewportData.viewportSpecificData[0];
|
||||
const updatedOtherViewport = updatedState.viewportSpecificData[0];
|
||||
|
||||
expect(clearedViewport).toEqual({});
|
||||
expect(updatedOtherViewport).toEqual(originalOtherViewport);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user