-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #1818. Add action triggering via JS-API
- Loading branch information
1 parent
54458a4
commit e6516dd
Showing
7 changed files
with
143 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
|
||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}) | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters