Skip to content

Commit

Permalink
test(map): add tests from RxJS 4
Browse files Browse the repository at this point in the history
the only test that was not migrated is actually related to mapTo() in RxJS Next.

related to #389.
  • Loading branch information
luisgabriel committed Oct 8, 2015
1 parent c05fdc9 commit 414c1b7
Showing 1 changed file with 148 additions and 1 deletion.
149 changes: 148 additions & 1 deletion spec/operators/map-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var Observable = Rx.Observable;

// function shortcuts
var addDrama = function (x) { return x + '!'; };
var identity = function (x) { return x; };
var throwError = function () { throw new Error(); };

describe('Observable.prototype.map()', function () {
it('should map one value', function () {
Expand All @@ -22,7 +24,7 @@ describe('Observable.prototype.map()', function () {
expectObservable(r).toBe(expected, {x: '1!', y: '2!', z: '3!'});
});

it('should send errors down the error path', function () {
it('should propagate errors from map function', function () {
var a = cold('--x--|', {x: 42});
var expected = '--#';

Expand All @@ -31,4 +33,149 @@ describe('Observable.prototype.map()', function () {
});
expectObservable(r).toBe(expected, null, 'too bad');
});

it('should propagate errors from observable that emits only errors', function () {
var a = cold('--#--', null, 'too bad');
var expected = '--#';

var r = a.map(identity);
expectObservable(r).toBe(expected, null, 'too bad');
});

it('should propagate errors from observable that emit values', function () {
var a = cold('--a--b--#--', {a: 1, b: 2}, 'too bad');
var expected = '--x--y--#';

var r = a.map(addDrama);
expectObservable(r).toBe(expected, {x: '1!', y: '2!'}, 'too bad');
});

it('should propagate errors from subscribe', function () {
var r = function () {
Observable.of(1)
.map(identity)
.subscribe(throwError);
};

expect(r).toThrow();
});

it('should not map an empty observable', function () {
var a = Observable.empty();
var expected = '|';

var invoked = 0;
var r = a
.map(function (x) { invoked++; return x; })
.do(null, null, function () {
expect(invoked).toBe(0);
});

expectObservable(r).toBe(expected);
});

it('should map with index', function () {
var a = hot('-5-^-4--3---2----1');
var expected = '--a--b---c----d';
var values = {a: 5, b: 14, c: 23, d: 32};

var invoked = 0;
var r = a.map(function (x, index) {
invoked++;
return (parseInt(x) + 1) + (index * 10);
}).do(null, null, function () {
expect(invoked).toBe(4);
});

expectObservable(r).toBe(expected, values);
});

it('should map with index until completed', function () {
var a = hot('-5-^-4--3---2----1--|--8--|--#');
var expected = '--a--b---c----d--|';
var values = {a: 5, b: 14, c: 23, d: 32};

var invoked = 0;
var r = a.map(function (x, index) {
invoked++;
return (parseInt(x) + 1) + (index * 10);
}).do(null, null, function () {
expect(invoked).toBe(4);
});

expectObservable(r).toBe(expected, values);
});

it('should map with index until an error occurs', function () {
var a = hot('-5-^-4--3---2----1--#--8--|', undefined, 'too bad');
var expected = '--a--b---c----d--#';
var values = {a: 5, b: 14, c: 23, d: 32};

var invoked = 0;
var r = a.map(function (x, index) {
invoked++;
return (parseInt(x) + 1) + (index * 10);
}).do(null, null, function () {
expect(invoked).toBe(4);
});

expectObservable(r).toBe(expected, values, 'too bad');
});

it('should map using a custom thisArg', function () {
var a = hot('-5-^-4--3---2----1--|--8--|--#');
var expected = '--a--b---c----d--|';
var values = {a: 5, b: 14, c: 23, d: 32};

var invoked = 0;
var foo = 42;
var r = a
.map(function (x, index) {
invoked++;
expect(this).toEqual(foo);
return (parseInt(x) + 1) + (index * 10);
}, 42)
.do(null, null, function () {
expect(invoked).toBe(4);
});

expectObservable(r).toBe(expected, values);
});

it('should map twice', function () {
var a = hot('-0----1-^-2---3--4-5--6--7-8-|--9-#-|');
var expected = '--a---b--c-d--e--f-g-|';
var values = {a: 2, b: 3, c: 4, d: 5, e: 6, f: 7, g: 8};

var invoked1 = 0;
var invoked2 = 0;
var r = a
.map(function (x) { invoked1++; return parseInt(x) * 2; })
.map(function (x) { invoked2++; return x / 2; })
.do(null, null, function () {
expect(invoked1).toBe(7);
expect(invoked2).toBe(7);
});

expectObservable(r).toBe(expected, values);
});

it('should do multiple maps using a custom thisArg', function () {
var a = hot('--1--2--3--4--|');
var expected = '--a--b--c--d--|';
var values = {a: 11, b: 14, c: 17, d: 20};

function Filterer() {
this.selector1 = function (x) { return parseInt(x) + 2; };
this.selector2 = function (x) { return parseInt(x) * 3; };
}
var filterer = new Filterer();

var r = a
.map(function (x) { return this.selector1(x);}, filterer)
.map(function (x) { return this.selector2(x);}, filterer)
.map(function (x) { return this.selector1(x);}, filterer);

expectObservable(r).toBe(expected, values);
});
});

0 comments on commit 414c1b7

Please sign in to comment.