-
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(withLatestFrom): default array output, handle other types
- now handles promises, iteratables, lowercase-o observables and Observables - updates marble tests to be more comprehensive - fixes issue where values were emitted before all observables responded - makes project function optional and will output arrays - updates documentation
- Loading branch information
Showing
2 changed files
with
169 additions
and
52 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 |
---|---|---|
@@ -1,27 +1,117 @@ | ||
/* globals describe, it, expect */ | ||
/* globals describe, it, expect, expectObservable, hot, cold, lowerCaseO */ | ||
var Rx = require('../../dist/cjs/Rx'); | ||
var Observable = Rx.Observable; | ||
var Promise = require('promise'); | ||
|
||
describe('Observable.prototype.withLatestFrom()', function () { | ||
it('should merge the emitted value with the latest values of the other observables', function (done) { | ||
var a = Observable.of('a'); | ||
var b = Observable.of('b', 'c'); | ||
|
||
Observable.of('d').delay(100) | ||
.withLatestFrom(a, b, function (x, a, b) { return [x, a, b]; }) | ||
.subscribe(function (x) { | ||
expect(x).toEqual(['d', 'a', 'c']); | ||
}, null, done); | ||
describe('Observable.prototype.withLatestFrom()', function () { | ||
it('should merge the value with the latest values from the other observables into arrays', function () { | ||
var e1 = hot('--a--^---b---c---d--|'); | ||
var e2 = hot('--e--^-f---g---h----|'); | ||
var e3 = hot('--i--^-j---k---l----|'); | ||
var expected = '----x---y---z--|'; | ||
var values = { | ||
x: ['b', 'f', 'j'], | ||
y: ['c', 'g', 'k'], | ||
z: ['d', 'h', 'l'] | ||
}; | ||
expectObservable(e1.withLatestFrom(e2, e3)).toBe(expected, values); | ||
}); | ||
|
||
it('should emit nothing if the other observables never emit', function (done) { | ||
var a = Observable.of('a'); | ||
var b = Observable.never(); | ||
|
||
Observable.of('d').delay(100) | ||
.withLatestFrom(a, b, function (x, a, b) { return [x, a, b]; }) | ||
.subscribe(function (x) { | ||
expect('this was called').toBe(false); | ||
it('should merge the value with the latest values from the other observables into arrays and a project argument', function () { | ||
var e1 = hot('--a--^---b---c---d--|'); | ||
var e2 = hot('--e--^-f---g---h----|'); | ||
var e3 = hot('--i--^-j---k---l----|'); | ||
var expected = '----x---y---z--|'; | ||
var values = { | ||
x: 'bfj', | ||
y: 'cgk', | ||
z: 'dhl' | ||
}; | ||
var project = function(a, b, c) { return a + b + c }; | ||
expectObservable(e1.withLatestFrom(e2, e3, project)).toBe(expected, values); | ||
}); | ||
|
||
it('should handle empty', function (){ | ||
var e1 = Observable.empty(); | ||
var e2 = hot('--e--^-f---g---h----|'); | ||
var e3 = hot('--i--^-j---k---l----|'); | ||
var expected = '|'; // empty | ||
expectObservable(e1.withLatestFrom(e2, e3)).toBe(expected); | ||
}); | ||
|
||
it('should handle never', function (){ | ||
var e1 = Observable.never(); | ||
var e2 = hot('--e--^-f---g---h----|'); | ||
var e3 = hot('--i--^-j---k---l----|'); | ||
var expected = '--------------------'; // never | ||
expectObservable(e1.withLatestFrom(e2, e3)).toBe(expected); | ||
}); | ||
|
||
it('should handle throw', function (){ | ||
var e1 = Observable.throw(new Error('sad')); | ||
var e2 = hot('--e--^-f---g---h----|'); | ||
var e3 = hot('--i--^-j---k---l----|'); | ||
var expected = '#'; // throw | ||
expectObservable(e1.withLatestFrom(e2, e3)).toBe(expected, null, new Error('sad')); | ||
}); | ||
|
||
it('should handle error', function (){ | ||
var e1 = hot('--a--^---b---#', undefined, new Error('boo-hoo')); | ||
var e2 = hot('--e--^-f---g---h----|'); | ||
var e3 = hot('--i--^-j---k---l----|'); | ||
var expected = '----x---#'; // throw | ||
var values = { | ||
x: ['b','f','j'] | ||
}; | ||
expectObservable(e1.withLatestFrom(e2, e3)).toBe(expected, values, new Error('boo-hoo')); | ||
}); | ||
|
||
it('should handle error with project argument', function (){ | ||
var e1 = hot('--a--^---b---#', undefined, new Error('boo-hoo')); | ||
var e2 = hot('--e--^-f---g---h----|'); | ||
var e3 = hot('--i--^-j---k---l----|'); | ||
var expected = '----x---#'; // throw | ||
var values = { | ||
x: 'bfj' | ||
}; | ||
var project = function(a, b, c) { return a + b + c; }; | ||
expectObservable(e1.withLatestFrom(e2, e3, project)).toBe(expected, values, new Error('boo-hoo')); | ||
}); | ||
|
||
it('should handle merging with empty', function (){ | ||
var e1 = hot('--a--^---b---c---d--|'); | ||
var e2 = Observable.empty(); | ||
var e3 = hot('--i--^-j---k---l----|'); | ||
var expected = '---------------|'; | ||
expectObservable(e1.withLatestFrom(e2, e3)).toBe(expected); | ||
}); | ||
|
||
it('should handle merging with never', function (){ | ||
var e1 = hot('--a--^---b---c---d--|'); | ||
var e2 = Observable.never(); | ||
var e3 = hot('--i--^-j---k---l----|'); | ||
var expected = '---------------|'; | ||
expectObservable(e1.withLatestFrom(e2, e3)).toBe(expected); | ||
}); | ||
|
||
it('should handle promises', function (done){ | ||
Observable.of(1).delay(1).withLatestFrom(Promise.resolve(2), Promise.resolve(3)) | ||
.subscribe(function(x) { | ||
expect(x).toEqual([1,2,3]); | ||
}, null, done); | ||
}); | ||
|
||
it('should handle arrays', function() { | ||
Observable.of(1).delay(1).withLatestFrom([2,3,4], [4,5,6]) | ||
.subscribe(function(x) { | ||
expect(x).toEqual([1,4,6]); | ||
}); | ||
}); | ||
|
||
it('should handle lowercase-o observables', function (){ | ||
Observable.of(1).delay(1).withLatestFrom(lowerCaseO(2, 3, 4), lowerCaseO(4, 5, 6)) | ||
.subscribe(function(x) { | ||
expect(x).toEqual([1,4,6]); | ||
}); | ||
}); | ||
}); |
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