|
| 1 | +/* |
| 2 | + * Copyright 2019, GeoSolutions Sas. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +const ReactDOM = require('react-dom'); |
| 10 | +const {findIndex} = require('lodash'); |
| 11 | +const ConfigUtils = require('../../utils/ConfigUtils'); |
| 12 | +const { INIT_MAP } = require('../../actions/map'); |
| 13 | +const { MAP_CONFIG_LOADED } = require('../../actions/config'); |
| 14 | +const { CHANGE_BROWSER_PROPERTIES } = require('../../actions/browser'); |
| 15 | + |
| 16 | +const { LOCAL_CONFIG_LOADED } = require('../../actions/localConfig'); |
| 17 | + |
| 18 | + |
| 19 | +const expect = require('expect'); |
| 20 | +const MapStore2 = require('../MapStore2'); |
| 21 | + |
| 22 | +const testConfig = { |
| 23 | + versionURL: 'base/web/client/test-resources/version.txt', |
| 24 | + configUrl: 'base/web/client/test-resources/geostore/data/1#', |
| 25 | + originalUrl: 'base/web/client/test-resources/geostore/extjs/search/category/MAP/1.json#' |
| 26 | +}; |
| 27 | +describe('MapStore2 API', () => { |
| 28 | + beforeEach((done) => { |
| 29 | + document.body.innerHTML = '<div id="container"></div>'; |
| 30 | + ConfigUtils.setLocalConfigurationFile('base/web/client/test-resources/localConfig.json'); |
| 31 | + ConfigUtils.setConfigProp('translationsPath', "base/web/client/translations"); |
| 32 | + setTimeout(done); |
| 33 | + }); |
| 34 | + afterEach((done) => { |
| 35 | + ReactDOM.unmountComponentAtNode(document.getElementById("container")); |
| 36 | + ConfigUtils.setLocalConfigurationFile('localConfig.json'); |
| 37 | + ConfigUtils.setConfigProp('translationsPath', "translations"); |
| 38 | + document.body.innerHTML = ''; |
| 39 | + setTimeout(done); |
| 40 | + }); |
| 41 | + it('MapStore2 rendering with defaults', (done) => { |
| 42 | + |
| 43 | + MapStore2.create('container', { |
| 44 | + epics: { |
| 45 | + testEpic1: action$ => action$ |
| 46 | + .filter(a => a.type === CHANGE_BROWSER_PROPERTIES).do( () => { |
| 47 | + // CHANGE BROWSER PROPERTIES IS THE FIRST ACTION |
| 48 | + done(); |
| 49 | + }).ignoreElements() |
| 50 | + }, |
| 51 | + ...testConfig |
| 52 | + }); |
| 53 | + }); |
| 54 | + it('initMap action called before map load action', (done) => { |
| 55 | + |
| 56 | + MapStore2.create('container', { |
| 57 | + epics: { |
| 58 | + testEpic1: action$ => action$ |
| 59 | + .bufferCount(5).do((actions) => { |
| 60 | + expect(findIndex(actions, a => a.type === INIT_MAP)).toBeLessThan(findIndex(actions, a => a.type === MAP_CONFIG_LOADED)); |
| 61 | + done(); |
| 62 | + }).ignoreElements() |
| 63 | + }, |
| 64 | + ...testConfig |
| 65 | + }); |
| 66 | + }); |
| 67 | + it('onAction', (done) => { |
| 68 | + |
| 69 | + MapStore2.create('container', { |
| 70 | + ...testConfig |
| 71 | + }); |
| 72 | + MapStore2.onAction(MAP_CONFIG_LOADED, () => done()); |
| 73 | + }); |
| 74 | + it('offAction', (done) => { |
| 75 | + MapStore2.create('container', { |
| 76 | + epics: { |
| 77 | + testEpic1: action$ => action$ |
| 78 | + .bufferCount(5).do((actions) => { |
| 79 | + // the action has been emitted but the listener has not been called |
| 80 | + expect(findIndex(actions, a => a.type === LOCAL_CONFIG_LOADED)).toBeLessThan(5); |
| 81 | + done(); |
| 82 | + }).ignoreElements() |
| 83 | + }, |
| 84 | + ...testConfig |
| 85 | + }); |
| 86 | + const listenerToDeactivate = () => expect(true).toBe(false); |
| 87 | + MapStore2.onAction(LOCAL_CONFIG_LOADED, listenerToDeactivate); |
| 88 | + MapStore2.offAction(LOCAL_CONFIG_LOADED, listenerToDeactivate); |
| 89 | + }); |
| 90 | + it('onStateChange', (done) => { |
| 91 | + |
| 92 | + MapStore2.create('container', { |
| 93 | + ...testConfig |
| 94 | + }); |
| 95 | + let counter = 0; |
| 96 | + MapStore2.onStateChange( state => { |
| 97 | + if (state.map) { |
| 98 | + // this has been triggered more than once before the map to be loaded |
| 99 | + expect(counter).toBeGreaterThan(0); |
| 100 | + done(); |
| 101 | + } else { |
| 102 | + counter++; |
| 103 | + } |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
0 commit comments