Skip to content
Snippets Groups Projects
Unverified Commit 2c6ef1b2 authored by Lutz Helm's avatar Lutz Helm Committed by GitHub
Browse files

Merge pull request #3429 from ProjectMirador/mirador-viewer

Refactor MiradorViewer to make it easier to get at the store and components
parents 584466c9 b88cb7b1
No related branches found
No related tags found
No related merge requests found
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import { shallow } from 'enzyme';
import MiradorViewer from '../../../src/lib/MiradorViewer'; import MiradorViewer from '../../../src/lib/MiradorViewer';
jest.unmock('react-i18next'); jest.unmock('react-i18next');
...@@ -10,7 +11,7 @@ describe('MiradorViewer', () => { ...@@ -10,7 +11,7 @@ describe('MiradorViewer', () => {
beforeAll(() => { beforeAll(() => {
ReactDOM.render = jest.fn(); ReactDOM.render = jest.fn();
ReactDOM.unmountComponentAtNode = jest.fn(); ReactDOM.unmountComponentAtNode = jest.fn();
instance = new MiradorViewer({}); instance = new MiradorViewer({ id: 'mirador' });
}); });
describe('constructor', () => { describe('constructor', () => {
it('returns viewer store', () => { it('returns viewer store', () => {
...@@ -104,6 +105,15 @@ describe('MiradorViewer', () => { ...@@ -104,6 +105,15 @@ describe('MiradorViewer', () => {
})); }));
}); });
}); });
describe('render', () => {
it('passes props through to the App component', () => {
const rendered = shallow(instance.render({ some: 'prop' }));
expect(rendered.find('App').length).toBe(1);
expect(rendered.find('App').prop('some')).toBe('prop');
});
});
describe('unmount', () => { describe('unmount', () => {
it('unmounts via ReactDOM', () => { it('unmounts via ReactDOM', () => {
instance.unmount(); instance.unmount();
......
import React from 'react'; import React from 'react';
import ReactDOM from 'react-dom'; import ReactDOM from 'react-dom';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';
import deepmerge from 'deepmerge';
import HotApp from '../components/App'; import HotApp from '../components/App';
import createStore from '../state/createStore';
import { importConfig } from '../state/actions/config';
import { import {
filterValidPlugins, filterValidPlugins,
getConfigFromPlugins,
getReducersFromPlugins,
getSagasFromPlugins,
} from '../extend/pluginPreprocessing'; } from '../extend/pluginPreprocessing';
import createPluggableStore from '../state/createPluggableStore';
/** /**
* Default Mirador instantiation * Default Mirador instantiation
...@@ -19,28 +14,25 @@ class MiradorViewer { ...@@ -19,28 +14,25 @@ class MiradorViewer {
/** /**
*/ */
constructor(config, viewerConfig = {}) { constructor(config, viewerConfig = {}) {
this.config = config;
this.plugins = filterValidPlugins(viewerConfig.plugins || []); this.plugins = filterValidPlugins(viewerConfig.plugins || []);
this.config = config;
this.store = viewerConfig.store this.store = viewerConfig.store
|| createStore(getReducersFromPlugins(this.plugins), getSagasFromPlugins(this.plugins)); || createPluggableStore(this.config, this.plugins);
this.processConfig();
ReactDOM.render( config.id && ReactDOM.render(
<Provider store={this.store}> this.render(),
<HotApp plugins={this.plugins} />
</Provider>,
document.getElementById(config.id), document.getElementById(config.id),
); );
} }
/** /**
* Process config with plugin configs into actions * Render the mirador viewer
*/ */
processConfig() { render(props = {}) {
this.store.dispatch( return (
importConfig( <Provider store={this.store}>
deepmerge(getConfigFromPlugins(this.plugins), this.config), <HotApp plugins={this.plugins} {...props} />
), </Provider>
); );
} }
...@@ -48,7 +40,7 @@ class MiradorViewer { ...@@ -48,7 +40,7 @@ class MiradorViewer {
* Cleanup method to unmount Mirador from the dom * Cleanup method to unmount Mirador from the dom
*/ */
unmount() { unmount() {
ReactDOM.unmountComponentAtNode(document.getElementById(this.config.id)); this.config.id && ReactDOM.unmountComponentAtNode(document.getElementById(this.config.id));
} }
} }
......
import deepmerge from 'deepmerge';
import createStore from './createStore';
import { importConfig } from './actions/config';
import {
filterValidPlugins,
getConfigFromPlugins,
getReducersFromPlugins,
getSagasFromPlugins,
} from '../extend/pluginPreprocessing';
/**
* Configure Store
*/
function createPluggableStore(config, plugins = []) {
const filteredPlugins = filterValidPlugins(plugins);
const store = createStore(
getReducersFromPlugins(filteredPlugins),
getSagasFromPlugins(filteredPlugins),
);
store.dispatch(
importConfig(
deepmerge(getConfigFromPlugins(filteredPlugins), config),
),
);
return store;
}
export default createPluggableStore;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment