Created
March 29, 2017 16:12
-
-
Save dfbaskin/99d9536071534fc5ee54347fea248852 to your computer and use it in GitHub Desktop.
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
import {ActionsObservable as Observable} from 'redux-observable' | |
import {VirtualTimeScheduler} from 'rxjs/scheduler/VirtualTimeScheduler'; | |
import 'rxjs/add/observable/empty'; | |
import 'rxjs/add/observable/interval'; | |
import 'rxjs/add/operator/delay'; | |
import 'rxjs/add/operator/concat'; | |
import 'rxjs/add/operator/reduce'; | |
import 'rxjs/add/operator/takeUntil'; | |
import 'rxjs/add/operator/switchMap'; | |
import 'rxjs/add/operator/toArray'; | |
import 'rxjs/add/operator/do'; | |
const intervalSeconds = 60; | |
let scheduler; | |
const intervalEpic = (action$) => { | |
return Observable | |
.interval(intervalSeconds * 1000, scheduler) | |
.takeUntil(action$.reduce((v, action) => 0, 0)) | |
.switchMap(() => { | |
return Observable.of({ | |
type: "SOME_ACTION" | |
}) | |
}); | |
}; | |
export function runObservableEpicTest(observable, epic, done, verify) { | |
let mockStore = { | |
getState() { | |
return {}; | |
} | |
}; | |
Observable | |
.empty() | |
.concat(epic(observable, mockStore)) | |
.toArray() | |
.do((actions) => verify(actions)) | |
.subscribe({ | |
error: (err) => done.fail(err), | |
complete: () => done() | |
}); | |
} | |
describe('test with interval', () => { | |
beforeEach(() => { | |
scheduler = new VirtualTimeScheduler(); | |
}); | |
it('should allow testing of interval', (done) => { | |
let observable = Observable | |
.empty() | |
.delay((intervalSeconds + 1) * 1000, scheduler); | |
runObservableEpicTest(observable, intervalEpic, done, (actions) => { | |
expect(actions).toEqual([ | |
{ type: "SOME_ACTION" } | |
]); | |
}); | |
scheduler.flush(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Testing, using VirtualTimeScheduler, a redux-observable epic that uses interval for a regularly occurring event. Note that the scheduler has to be passed through to the epic.