Create a gist now

Instantly share code, notes, and snippets.

@valotas /index.html
Last active Mar 25, 2016

What would you like to do?
RxJs Marble testing
<!DOCTYPE html>
<html>
<head>
<script src="https://npmcdn.com/@reactivex/rxjs@5.0.0-beta.2/dist/global/Rx.KitchenSink.umd.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.min.css">
<script src="http://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/jasmine-html.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jasmine/2.3.4/boot.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>RxJS Marble testing</title>
</head>
<body></body>
</html>
describe('rxjs TestScheduler', function () {
var rx = Rx.KitchenSink;
it('should be part of the Rx namespace', function () {
expect(rx).toBeDefined();
expect(rx.TestScheduler).toBeDefined();
});
describe('createHotObservable()', function () {
it('should create a hot observable emmiting the values given as marble strings', function () {
//given
var scheduler = new rx.TestScheduler(null);
var source = scheduler.createHotObservable('--a---b-cd-|');
var results = [];
//when
source
.bufferCount(2)
.map(function (array) {
return array.join('');
})
.subscribe(function (val) {
results.push(val);
}, null, function () {
results.push('done');
});
scheduler.flush();
expect(results).toEqual(['ab', 'cd', 'done']);
});
it('should create a hot observable emmiting the values given as marble strings', function () {
//given
var scheduler = new rx.TestScheduler(null);
var source = scheduler.createHotObservable('--a-^--b-c|');
var results = [];
//when
scheduler.schedule(function () {
source.subscribe(function (val) {
results.push(val);
}, null, function () {
results.push('done');
});
});
scheduler.flush();
expect(results).toEqual(['b', 'c', 'done']);
});
});
describe('createColdObservable()', function () {
it('should create a cold observable emmiting the values given as marble strings', function () {
//given
var scheduler = new rx.TestScheduler(null);
var source = scheduler.createColdObservable('--a---b|');
var results = [];
//when
source.subscribe(function (val) {
results.push(val);
});
scheduler.flush();
expect(results).toEqual(['a', 'b']);
});
});
it('should be reusable with time related operators', function () {
//given
var scheduler = new rx.TestScheduler(null);
var source = scheduler.createHotObservable('a----bcd-f|');
var results = [];
//when
source
.bufferTime(40, null, scheduler)
.map(function (buffer) {
return buffer.join('');
})
.subscribe(function (val) {
results.push(val);
});
scheduler.flush();
//then
expect(results).toEqual(['a', 'bcd', 'f']);
});
describe('schedule()', function () {
it('should schedule the subscription on the right time', function () {
//given
var scheduler = new rx.TestScheduler(null);
var source = scheduler.createHotObservable('--a-^--b-c|');
var results = [];
//when
scheduler.schedule(function () {
source.subscribe(function (val) {
results.push(val);
}, null, function () {
results.push('done');
});
});
scheduler.flush();
expect(results).toEqual(['b', 'c', 'done']);
});
it('should allow scheduling of non ending streams', function () {
//given
var scheduler = new rx.TestScheduler(null);
var source = scheduler.createHotObservable('--a-^--b-c');
var results = [];
//when
scheduler.schedule(function () {
source.subscribe(function (val) {
results.push(val);
}, null, function () {
results.push('done');
});
});
scheduler.flush();
expect(results).toEqual(['b', 'c']);
});
});
function assertEquals (actual, expected) {
expect(actual).toEqual(expected);
}
describe('expectObservable()', function () {
it('should simplify the result check of given observables', function () {
//given
var scheduler = new rx.TestScheduler(assertEquals);
var source = scheduler.createHotObservable('--a-^--b-c');
//then
scheduler.expectObservable(source).toBe('---b-c');
scheduler.flush();
});
});
describe('operators', function () {
describe('fromPromise()', function () {
it('should transform a Promise to an Observable', function (done) {
var promise = Promise.resolve('promise value');
rx.Observable.fromPromise(promise)
.subscribe(function (e) {
expect(e).toEqual('promise value');
}, done.fail, done);
});
});
describe('mergeMap()', function () {
it('should transform a Promise to an Observable', function () {
//given
var scheduler = new rx.TestScheduler(assertEquals);
var source = scheduler.createHotObservable('--a-^--b-c');
var other = scheduler.createColdObservable('-d--e');
//then
scheduler.expectObservable(source.mergeMap(function () {
return other;
})).toBe('----d-de-e');
scheduler.flush();
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment