-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(TestScheduler): add TestScheduler
adds a test scheduler that features a marble parser as well as cold and hot observable creation methods closes #270
- Loading branch information
Showing
4 changed files
with
140 additions
and
2 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,67 @@ | ||
/* globals describe, it, expect */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var TestScheduler = Rx.TestScheduler; | ||
var Notification = Rx.Notification; | ||
|
||
describe('TestScheduler', function() { | ||
it('should exist', function () { | ||
expect(typeof TestScheduler).toBe('function'); | ||
}); | ||
|
||
describe('parseMarbles()', function () { | ||
it('should parse a marble string into a series of notifications and types', function () { | ||
var result = TestScheduler.parseMarbles('-------a---b---|', { a: 'A', b: 'B' }); | ||
expect(result).toDeepEqual([ | ||
{ frame: 70, notification: Notification.createNext('A') }, | ||
{ frame: 110, notification: Notification.createNext('B') }, | ||
{ frame: 150, notification: Notification.createComplete() } | ||
]); | ||
}); | ||
|
||
it('should parse a marble string with a subscription point', function () { | ||
var result = TestScheduler.parseMarbles('---^---a---b---|', { a: 'A', b: 'B' }); | ||
expect(result).toDeepEqual([ | ||
{ frame: 40, notification: Notification.createNext('A') }, | ||
{ frame: 80, notification: Notification.createNext('B') }, | ||
{ frame: 120, notification: Notification.createComplete() } | ||
]); | ||
}); | ||
|
||
it('should parse a marble string with an error', function () { | ||
var result = TestScheduler.parseMarbles('-------a---b---#', { a: 'A', b: 'B' }, 'omg error!'); | ||
expect(result).toDeepEqual([ | ||
{ frame: 70, notification: Notification.createNext('A') }, | ||
{ frame: 110, notification: Notification.createNext('B') }, | ||
{ frame: 150, notification: Notification.createError('omg error!') } | ||
]); | ||
}); | ||
}); | ||
|
||
describe('createColdObservable()', function () { | ||
it('should create a cold observable', function () { | ||
var expected = ['A', 'B']; | ||
var scheduler = new TestScheduler(); | ||
var source = scheduler.createColdObservable('--a---b--|', { a: 'A', b: 'B' }); | ||
expect(source instanceof Rx.Observable).toBe(true); | ||
source.subscribe(function (x) { | ||
expect(x).toBe(expected.shift()); | ||
}); | ||
scheduler.flush(); | ||
expect(expected.length).toBe(0); | ||
}); | ||
}); | ||
|
||
describe('createHotObservable()', function () { | ||
it('should create a cold observable', function () { | ||
var expected = ['A', 'B']; | ||
var scheduler = new TestScheduler(); | ||
var source = scheduler.createHotObservable('--a---b--|', { a: 'A', b: 'B' }); | ||
expect(source instanceof Rx.Subject).toBe(true); | ||
source.subscribe(function (x) { | ||
expect(x).toBe(expected.shift()); | ||
}); | ||
scheduler.flush(); | ||
expect(expected.length).toBe(0); | ||
}); | ||
}); | ||
}); |
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,66 @@ | ||
import Observable from '../Observable'; | ||
import VirtualTimeScheduler from './VirtualTimeScheduler'; | ||
import Notification from '../Notification'; | ||
import Subject from '../Subject'; | ||
|
||
export default class TestScheduler extends VirtualTimeScheduler { | ||
createColdObservable(marbles: string, values?: any, error?: any) { | ||
if (marbles.indexOf('^') !== -1) { | ||
throw new Error('cold observable cannot have subscription offset "^"'); | ||
} | ||
let messages = TestScheduler.parseMarbles(marbles, values, error); | ||
return Observable.create(subscriber => { | ||
messages.forEach(({ notification, frame }) => { | ||
this.schedule(() => { | ||
notification.observe(subscriber); | ||
}, frame); | ||
}); | ||
}); | ||
} | ||
|
||
createHotObservable(marbles: string, values?: any, error?: any) { | ||
let messages = TestScheduler.parseMarbles(marbles, values, error); | ||
let subject = new Subject(); | ||
messages.forEach(({ notification, frame }) => { | ||
this.schedule(() => { | ||
notification.observe(subject); | ||
}, frame); | ||
}); | ||
return subject; | ||
} | ||
|
||
static parseMarbles(marbles: string, values?: any, errorValue?: any) : ({ notification: Notification<any>, frame: number })[] { | ||
let len = marbles.length; | ||
let results: ({ notification: Notification<any>, frame: number })[] = []; | ||
let subIndex = marbles.indexOf('^'); | ||
let frameOffset = subIndex === -1 ? 0 : (subIndex * -10); | ||
|
||
for (let i = 0; i < len; i++) { | ||
let frame = i * 10; | ||
let notification; | ||
let c = marbles[i]; | ||
switch (c) { | ||
case '-': | ||
break; | ||
case '|': | ||
notification = Notification.createComplete(); | ||
break; | ||
case '^': | ||
break; | ||
case '#': | ||
notification = Notification.createError(errorValue || 'error'); | ||
break; | ||
default: | ||
notification = Notification.createNext(values[c]); | ||
break; | ||
} | ||
|
||
frame += frameOffset; | ||
|
||
if (notification) { | ||
results.push({ notification, frame }); | ||
} | ||
} | ||
return results; | ||
} | ||
} |
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