Skip to content

Commit

Permalink
chore(test): add jasmine helpers for virtual time tests
Browse files Browse the repository at this point in the history
related #151
  • Loading branch information
benlesh committed Sep 6, 2015
1 parent b23daf1 commit e892568
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
38 changes: 38 additions & 0 deletions spec/helpers/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,26 @@
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;

var _ = require("lodash");
var Rx = require('../../dist/cjs/Rx');

global.rxTestScheduler = null;
global.cold;
global.hot;
global.expectObservable;


beforeEach(function () {
global.rxTestScheduler = new Rx.TestScheduler(assertDeepEqual);
global.hot = function () {
setupFlush();
return global.rxTestScheduler.createHotObservable.apply(global.rxTestScheduler, arguments);
};
global.cold = function () {
setupFlush();
return global.rxTestScheduler.createColdObservable.apply(global.rxTestScheduler, arguments);
};
global.expectObservable = global.rxTestScheduler.expect.bind(global.rxTestScheduler);

jasmine.addMatchers({
toDeepEqual: function(util, customEqualityTesters) {
return {
Expand All @@ -15,3 +33,23 @@ beforeEach(function () {
}
});
});

var glit = global.it;
var willFlush = false;

afterEach(function () {
willFlush = false;
global.it = glit;
});

function assertDeepEqual(actual, expected) {
return expect(actual).toDeepEqual(actual);
}

function setupFlush() {
willFlush = true;
global.it = function () {
glit.apply(this, arguments);
global.rxTestScheduler.flush();
};
}
34 changes: 34 additions & 0 deletions spec/schedulers/TestScheduler-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,38 @@ describe('TestScheduler', function() {
expect(expected.length).toBe(0);
});
});

describe('jasmine helpers', function () {
describe('rxTestScheduler', function () {
it('should exist', function () {
expect(rxTestScheduler instanceof Rx.TestScheduler).toBe(true);
});
});

describe('cold()', function () {
it('should exist', function () {
expect(typeof cold).toBe('function');
});
});

describe('hot()', function () {
it('should exist', function () {
expect(typeof hot).toBe('function');
});
});

describe('expectObservable()', function () {
it('should exist', function () {
expect(typeof expectObservable).toBe('function');
});
});

describe('end-to-end helper tests', function () {
it('should be awesome', function () {
var values = { a: 1, b: 2 };
var myObservable = cold('---a---b--|', values);
expectObservable(myObservable).toBe('---a---b--|', values);
});
});
});
});
43 changes: 41 additions & 2 deletions src/schedulers/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import Notification from '../Notification';
import Subject from '../Subject';

export default class TestScheduler extends VirtualTimeScheduler {
constructor(public assertDeepEqual: (actual: any, expected: any) => boolean | void) {
super();
}

createColdObservable(marbles: string, values?: any, error?: any) {
if (marbles.indexOf('^') !== -1) {
throw new Error('cold observable cannot have subscription offset "^"');
Expand All @@ -14,7 +18,7 @@ export default class TestScheduler extends VirtualTimeScheduler {
this.schedule(() => {
notification.observe(subscriber);
}, frame);
});
}, this);
});
}

Expand All @@ -25,10 +29,45 @@ export default class TestScheduler extends VirtualTimeScheduler {
this.schedule(() => {
notification.observe(subject);
}, frame);
});
}, this);
return subject;
}

flushTests: ({ observable: Observable<any>, marbles: string, actual?: any[], expected?: any[] })[] = [];

expect(observable: Observable<any>): ({ toBe: (marbles: string, values?: any, errorValue?: any) => void }) {
let actual = [];
let subscription = observable.subscribe((value) => {
actual.push({ frame: this.frame, notification: Notification.createNext(value) });
}, (err) => {
actual.push({ frame: this.frame, notification: Notification.createError(err) });
}, () => {
actual.push({ frame: this.frame, notification: Notification.createComplete() });
});

let flushTest: ({ observable: Observable<any>, marbles: string, actual?: any[], expected?: any[] }) = {
observable, actual, marbles: null
};

this.flushTests.push(flushTest);

return {
toBe(marbles: string, values?: any, errorValue?: any) {
flushTest.marbles = marbles;
flushTest.expected = TestScheduler.parseMarbles(marbles, values, errorValue);
}
};
}

flush() {
super.flush();
const flushTests = this.flushTests
while (flushTests.length > 0) {
var test = flushTests.shift();
this.assertDeepEqual(test.actual, test.expected);
}
}

static parseMarbles(marbles: string, values?: any, errorValue?: any) : ({ notification: Notification<any>, frame: number })[] {
let len = marbles.length;
let results: ({ notification: Notification<any>, frame: number })[] = [];
Expand Down

0 comments on commit e892568

Please sign in to comment.