diff --git a/web/client/epics/__tests__/epicTestUtils.js b/web/client/epics/__tests__/epicTestUtils.js new file mode 100644 index 0000000000..0707681dc0 --- /dev/null +++ b/web/client/epics/__tests__/epicTestUtils.js @@ -0,0 +1,28 @@ + +const Rx = require('rxjs'); +const { ActionsObservable } = require('redux-observable'); + +module.exports = { + /** + * Utility to test an epic + * @param {epic} epic the epic to test + * @param {number} count the number of actions to wait (note, the stream) + * @param {object|object[]} action the action(s) to trigger + * @param {Function} callback The check function, called after `count` actions received + * @param {Object} [state={}] the state + */ + testEpic: (epic, count, action, callback, state = {}) => { + const actions = new Rx.Subject(); + const actions$ = new ActionsObservable(actions); + const store = { getState: () => state }; + epic(actions$, store) + .take(count) + .toArray() + .subscribe(callback); + if (action.length) { + action.map(act => actions.next(act)); + } else { + actions.next(action); + } + } +}; diff --git a/web/client/epics/__tests__/globeswitcher-test.js b/web/client/epics/__tests__/globeswitcher-test.js index a3e441634a..2c9726bf59 100644 --- a/web/client/epics/__tests__/globeswitcher-test.js +++ b/web/client/epics/__tests__/globeswitcher-test.js @@ -10,26 +10,12 @@ var expect = require('expect'); const {toggle3d, UPDATE_LAST_2D_MAPTYPE} = require('../../actions/globeswitcher'); const assign = require('object-assign'); -const Rx = require('rxjs'); -const { ActionsObservable } = require('redux-observable'); + const {updateRouteOn3dSwitch} = require('../globeswitcher'); -const epicTest = (epic, count, action, callback, state = {}) => { - const actions = new Rx.Subject(); - const actions$ = new ActionsObservable(actions); - const store = { getState: () => state }; - epic(actions$, store) - .take(count) - .toArray() - .subscribe(callback); - if (action.length) { - action.map(act => actions.next(act)); - } else { - actions.next(action); - } -}; +const {testEpic} = require('./epicTestUtils'); describe('globeswitcher Epics', () => { it('produces the search epic', (done) => { - epicTest(updateRouteOn3dSwitch, 2, assign({hash: "/viewer/leaflet/2"}, toggle3d(true, "leaflet")), actions => { + testEpic(updateRouteOn3dSwitch, 2, assign({hash: "/viewer/leaflet/2"}, toggle3d(true, "leaflet")), actions => { expect(actions.length).toBe(2); actions.map((action) => { switch (action.type) { diff --git a/web/client/epics/__tests__/jsapi-test.js b/web/client/epics/__tests__/jsapi-test.js new file mode 100644 index 0000000000..e91b08a10f --- /dev/null +++ b/web/client/epics/__tests__/jsapi-test.js @@ -0,0 +1,34 @@ +/* + * Copyright 2017, GeoSolutions Sas. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. + */ + +var expect = require('expect'); + + +const {generateActionTrigger} = require('../jsapi'); +const {testEpic} = require('./epicTestUtils'); +describe('jsapi epic', () => { + it('check jsapi epic triggering', (done) => { + let {epic, trigger, stop} = generateActionTrigger("B"); + trigger({type: "C"}); + testEpic(epic, 1, [{type: "A"}, {type: "B"}], actions => { + actions.map((action) => { + switch (action.type) { + case "C": + stop(); + done(); + break; + default: + expect(true).toBe(false); + + } + }); + done(); + }); + + }); +}); diff --git a/web/client/epics/jsapi.js b/web/client/epics/jsapi.js new file mode 100644 index 0000000000..dac620da83 --- /dev/null +++ b/web/client/epics/jsapi.js @@ -0,0 +1,19 @@ +const Rx = require('rxjs'); + +module.exports = { + generateActionTrigger: (startAction) => { + var eventStream = new Rx.Subject(); + let init = false; + const buffer = []; + eventStream.publish(); + return { + trigger: (action) => init ? eventStream.next(action) : buffer.push(action), + stop: () => eventStream.complete(), + epic: (action$) => + action$.ofType(startAction).take(1).switchMap(() => { + init = true; + return Rx.Observable.from(buffer).concat(eventStream); + }) + }; + } +}; diff --git a/web/client/examples/api/index.html b/web/client/examples/api/index.html index df1a1dab72..8c5ffb874a 100644 --- a/web/client/examples/api/index.html +++ b/web/client/examples/api/index.html @@ -80,6 +80,8 @@