Skip to content

Commit c17e832

Browse files
kwonojbenlesh
authored andcommitted
fix(concat): let observable concat instead of merge
- change behavior of concat instead of merge - migrate test case for concat, concatall
1 parent e4c287e commit c17e832

File tree

5 files changed

+198
-4
lines changed

5 files changed

+198
-4
lines changed

spec/observables/concat-spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* globals describe, it, expect, expectObservable, cold */
2+
var Rx = require('../../dist/cjs/Rx');
3+
var Observable = Rx.Observable;
4+
5+
describe('Observable.concat', function () {
6+
it('should emit elements from multiple sources', function() {
7+
var e1 = cold('-a-b-c-|');
8+
var e2 = cold('-0-1-|');
9+
var e3 = cold('-w-x-y-z-|');
10+
var expected = '-a-b-c--0-1--w-x-y-z-|';
11+
12+
expectObservable(Observable.concat(e1, e2, e3)).toBe(expected);
13+
});
14+
});

spec/operators/concat-spec.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/* globals describe, it, expect, expectObservable, hot, cold */
2+
var Rx = require('../../dist/cjs/Rx');
3+
4+
describe('Observable.prototype.concat()', function () {
5+
it('should complete without emit if both sources are empty', function() {
6+
var e1 = hot('--|');
7+
var e2 = hot('----|');
8+
var expected = '----|';
9+
10+
expectObservable(e1.concat(e2)).toBe(expected);
11+
});
12+
13+
it('should not complete if first source does not completes', function() {
14+
var e1 = hot('-');
15+
var e2 = hot('--|');
16+
var expected = '-';
17+
18+
expectObservable(e1.concat(e2)).toBe(expected);
19+
});
20+
21+
it('should not complete if second source does not completes', function() {
22+
var e1 = hot('--|');
23+
var e2 = hot('-');
24+
var expected = '-';
25+
26+
expectObservable(e1.concat(e2)).toBe(expected);
27+
});
28+
29+
it('should not complete if both sources do not complete', function() {
30+
var e1 = hot('-');
31+
var e2 = hot('-');
32+
var expected = '-';
33+
34+
expectObservable(e1.concat(e2)).toBe(expected);
35+
});
36+
37+
it('should raise error when first source is empty, second source raises error', function() {
38+
var e1 = hot('--|');
39+
var e2 = hot('----#');
40+
var expected = '----#';
41+
42+
expectObservable(e1.concat(e2)).toBe(expected);
43+
});
44+
45+
it('should raise error when first source raises error, second source is empty', function(){
46+
var e1 = hot('---#');
47+
var e2 = hot('----|');
48+
var expected = '---#';
49+
50+
expectObservable(e1.concat(e2)).toBe(expected);
51+
});
52+
53+
it('should raise first error when both source raise error', function() {
54+
var e1 = hot('---#');
55+
var e2 = hot('------#');
56+
var expected = '---#';
57+
58+
expectObservable(e1.concat(e2)).toBe(expected);
59+
});
60+
61+
it('should concat if first source emits once, second source is empty', function() {
62+
var e1 = hot('--a--|');
63+
var e2 = hot('--------|');
64+
var expected = '--a-----|';
65+
66+
expectObservable(e1.concat(e2)).toBe(expected);
67+
});
68+
69+
it('should concat if first source is empty, second source emits once', function() {
70+
var e1 = hot('--|');
71+
var e2 = hot('--a--|');
72+
var expected = '--a--|';
73+
74+
expectObservable(e1.concat(e2)).toBe(expected);
75+
});
76+
77+
it('should emit element from first source, and should not complete if second source does not completes', function() {
78+
var e1 = hot('--a--|');
79+
var e2 = hot('-');
80+
var expected = '--a-';
81+
82+
expectObservable(e1.concat(e2)).toBe(expected);
83+
});
84+
85+
it('should not complete if first source does not complete', function() {
86+
var e1 = hot('-');
87+
var e2 = hot('--a--|');
88+
var expected = '-';
89+
90+
expectObservable(e1.concat(e2)).toBe(expected);
91+
});
92+
93+
it('should emit elements from each source when source emit once', function() {
94+
var e1 = hot('---a|');
95+
var e2 = hot('-----b--|');
96+
var expected = '---a-b--|';
97+
98+
expectObservable(e1.concat(e2)).toBe(expected);
99+
});
100+
101+
it('should raise error from first source and does not emit from second source', function() {
102+
var e1 = hot('--#');
103+
var e2 = hot('----a--|');
104+
var expected = '--#';
105+
106+
expectObservable(e1.concat(e2)).toBe(expected);
107+
});
108+
109+
it('should emit element from first source then raise error from second source', function() {
110+
var e1 = hot('--a--|');
111+
var e2 = hot('-------#');
112+
var expected = '--a----#';
113+
114+
expectObservable(e1.concat(e2)).toBe(expected);
115+
});
116+
117+
it('should emit all elements from both hot observable source if first source complets before second source starts emit', function() {
118+
var e1 = hot('--a--b-|');
119+
var e2 = hot('--------x--y--|');
120+
var expected = '--a--b--x--y--|';
121+
122+
expectObservable(e1.concat(e2)).toBe(expected);
123+
});
124+
125+
it('should emit elements from second source regardless of completion time when second source is cold observable', function() {
126+
var e1 = hot('--a--b--c---|');
127+
var e2 = cold('-x-y-z-|');
128+
var expected = '--a--b--c----x-y-z-|';
129+
130+
expectObservable(e1.concat(e2)).toBe(expected);
131+
});
132+
133+
it('should not emit collapsing element from second source', function() {
134+
var e1 = hot('--a--b--c--|');
135+
var e2 = hot('--------x--y--z--|');
136+
var expected = '--a--b--c--y--z--|';
137+
138+
expectObservable(e1.concat(e2)).toBe(expected);
139+
});
140+
});

spec/operators/concatAll-spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* globals describe, it, expect, expectObservable, hot, cold */
2+
var Rx = require('../../dist/cjs/Rx');
3+
4+
describe('Observable.prototype.concatAll()', function () {
5+
it('should concat sources from promise', function(done) {
6+
var sources = Rx.Observable.fromArray([
7+
new Promise(function (res) { res(0); }),
8+
new Promise(function (res) { res(1); }),
9+
new Promise(function (res) { res(2); }),
10+
new Promise(function (res) { res(3); }),
11+
]);
12+
13+
var res = [];
14+
sources.concatAll().subscribe(
15+
function (x) { res.push(x) },
16+
null,
17+
function () {
18+
expect(res).toEqual([0,1,2,3]);
19+
done();
20+
});
21+
}, 2000);
22+
23+
it('should concat and raise error from promise', function(done) {
24+
var sources = Rx.Observable.fromArray([
25+
new Promise(function (res) { res(0); }),
26+
new Promise(function (res, rej) { rej(1); }),
27+
new Promise(function (res) { res(2); }),
28+
new Promise(function (res) { res(3); }),
29+
]);
30+
31+
var res = [];
32+
sources.concatAll().subscribe(
33+
function (x) { res.push(x) },
34+
function (err) {
35+
expect(res.length).toBe(1);
36+
expect(err).toBe(1);
37+
done();
38+
}, null);
39+
}, 2000);
40+
});

src/operators/concat-static.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import merge from './merge-static';
1+
import mergeAll from './mergeAll';
22
import Observable from '../Observable';
33
import Scheduler from '../Scheduler';
44
import immediate from '../schedulers/immediate';
@@ -11,5 +11,5 @@ export default function concat<R>(...observables: (Observable<any>|Scheduler)[])
1111
scheduler = args.pop();
1212
args.push(1, scheduler);
1313
}
14-
return merge.apply(this, observables);
14+
return Observable.fromArray(observables).mergeAll(1);
1515
}

src/operators/concat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import merge from './merge-static';
1+
import mergeAll from './mergeAll';
22
import Observable from '../Observable';
33
import Scheduler from '../Scheduler';
44

@@ -8,5 +8,5 @@ export default function concatProto<R>(...observables:(Observable<any>|Scheduler
88
if(args.length > 1 && typeof args[args.length - 1].schedule === 'function') {
99
args.splice(args.length - 2, 0, 1);
1010
}
11-
return merge.apply(this, args);
11+
return Observable.fromArray(args).mergeAll(1);
1212
}

0 commit comments

Comments
 (0)