Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing 'routerReducer' with a configurable option #417

Merged
merged 17 commits into from
Oct 23, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added toEqualObservable jasmine matcher for effect testing
phillipzada committed Jul 28, 2017
commit 6c419f892cd29cb0a932486d442c11886226dbdd
1 change: 1 addition & 0 deletions modules/effects/testing/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './src/testing';
export * from './src/toEqualObservable';
116 changes: 116 additions & 0 deletions modules/effects/testing/src/toEqualObservable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { getTestScheduler } from 'jasmine-marbles';
import { Notification } from 'rxjs/Notification';
import { Observable } from 'rxjs/Observable';
import { Subscription } from 'rxjs/Subscription';
import { TestMessage } from 'rxjs/testing/TestMessage';
import { TestScheduler } from 'rxjs/testing/TestScheduler';

declare global {
namespace jasmine {
interface Matchers {
toEqualObservable: any;
}
}
}

/*
* Based on source code found in rxjs library
* https://github.com/ReactiveX/rxjs/blob/master/src/testing/TestScheduler.ts
*
*/
function materializeInnerObservable(
observable: Observable<any>,
outerFrame: number
): TestMessage[] {
const messages: TestMessage[] = [];
const scheduler = getTestScheduler();

observable.subscribe(
value => {
messages.push({
frame: scheduler.frame - outerFrame,
notification: Notification.createNext(value),
});
},
err => {
messages.push({
frame: scheduler.frame - outerFrame,
notification: Notification.createError(err),
});
},
() => {
messages.push({
frame: scheduler.frame - outerFrame,
notification: Notification.createComplete(),
});
}
);
return messages;
}

jasmine.getEnv().beforeAll(() =>
jasmine.addMatchers({
/*
* Performs toEqual as an alternative to toBeObservable.
* Based on source code found in rxjs library
* https://github.com/ReactiveX/rxjs/blob/master/src/testing/TestScheduler.ts
*
* Provides a more detailed error response on why an observable
* doesn't match
*
* Usage => expect(effect$).toEqualObservable(coldObservable);
*
*/
toEqualObservable: () => ({
compare: function(actual: any, { fixture }: any) {
const results: TestMessage[] = [];
let subscription: Subscription;
const scheduler = getTestScheduler();

if (!scheduler) {
throw new Error('TestScheduler must be initialised');
}

scheduler.schedule(() => {
subscription = actual.subscribe(
(x: any) => {
let value = x;
// Support Observable-of-Observables
if (x instanceof Observable) {
value = materializeInnerObservable(value, scheduler.frame);
}
results.push({
frame: scheduler.frame,
notification: Notification.createNext(value),
});
},
(err: any) => {
results.push({
frame: scheduler.frame,
notification: Notification.createError(err),
});
},
() => {
results.push({
frame: scheduler.frame,
notification: Notification.createComplete(),
});
}
);
});
scheduler.flush();

expect(results).toEqual(
TestScheduler.parseMarbles(
fixture.marbles,
fixture.values,
fixture.errorValue || fixture.error,
true
)
);

return { pass: true };
},
}),
})
);