Skip to content

Commit

Permalink
feat(TestScheduler): add createTime() parser to return number
Browse files Browse the repository at this point in the history
Add createTime() function to TestScheduler, which takes a simple marble diagram as string and
outputs a number for the time frame which the diagram represents.
  • Loading branch information
staltz authored and benlesh committed Dec 14, 2015
1 parent 2a9dfac commit cb8cf6b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/helpers/marble-testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ function cold() {
return global.rxTestScheduler.createColdObservable.apply(global.rxTestScheduler, arguments);
}

function time() {
if (!global.rxTestScheduler) {
throw 'tried to use time() in async test';
}
return global.rxTestScheduler.createTime.apply(global.rxTestScheduler, arguments);
}

function expectObservable() {
if (!global.rxTestScheduler) {
throw 'tried to use expectObservable() in async test';
Expand All @@ -33,6 +40,7 @@ function assertDeepEqual(actual, expected) {
module.exports = {
hot: hot,
cold: cold,
time: time,
expectObservable: expectObservable,
expectSubscriptions: expectSubscriptions,
assertDeepEqual: assertDeepEqual
Expand Down
1 change: 1 addition & 0 deletions spec/helpers/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var marbleHelpers = require('./marble-testing');
global.rxTestScheduler = null;
global.cold = marbleHelpers.cold;
global.hot = marbleHelpers.hot;
global.time = marbleHelpers.time;
global.expectObservable = marbleHelpers.expectObservable;
global.expectSubscriptions = marbleHelpers.expectSubscriptions;

Expand Down
1 change: 1 addition & 0 deletions spec/helpers/tests2png/diagram-test-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var painter = require('./painter');
global.rxTestScheduler = null;
global.cold = marbleHelpers.cold;
global.hot = marbleHelpers.hot;
global.time = marbleHelpers.time;
global.expectObservable = marbleHelpers.expectObservable;
global.expectSubscriptions = marbleHelpers.expectSubscriptions;

Expand Down
26 changes: 26 additions & 0 deletions spec/schedulers/TestScheduler-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ describe('TestScheduler', function () {
});
});

describe('createTime()', function () {
it('should parse a simple time marble string to a number', function () {
var scheduler = new TestScheduler();
var time = scheduler.createTime('-----|');
expect(time).toBe(50);
});

it('should throw if not given good marble input', function () {
var scheduler = new TestScheduler();
expect(function () {
var time = scheduler.createTime('-a-b-#');
}).toThrow();
});
});

describe('createColdObservable()', function () {
it('should create a cold observable', function () {
var expected = ['A', 'B'];
Expand Down Expand Up @@ -151,6 +166,17 @@ describe('TestScheduler', function () {
});
});

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

it('should parse a simple time marble string to a number', function () {
expect(time('-----|')).toBe(50);
});
});

describe('expectObservable()', function () {
it('should exist', function () {
expect(expectObservable).toBeDefined();
Expand Down
8 changes: 8 additions & 0 deletions src/testing/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export class TestScheduler extends VirtualTimeScheduler {
super();
}

createTime(marbles: string): number {
const indexOf: number = marbles.indexOf('|');
if (indexOf === -1) {
throw new Error('Marble diagram for time should have a completion marker "|"');
}
return indexOf * TestScheduler.frameTimeFactor;
}

createColdObservable<T>(marbles: string, values?: any, error?: any): Observable<T> {
if (marbles.indexOf('^') !== -1) {
throw new Error('Cold observable cannot have subscription offset "^"');
Expand Down

0 comments on commit cb8cf6b

Please sign in to comment.