2019-05-31 11:56:07 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2019-05-31 11:56:07 -04:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
import {asapScheduler, concat, Observable, of, range} from 'rxjs';
|
2019-05-31 11:56:07 -04:00
|
|
|
import {concatAll, concatMap, concatMapTo, map} from 'rxjs/operators';
|
|
|
|
|
|
|
|
import {asyncTest} from '../test-util';
|
|
|
|
|
|
|
|
describe('Observable instance method concat', () => {
|
|
|
|
let log: any[];
|
|
|
|
const constructorZone1: Zone = Zone.current.fork({name: 'Constructor Zone1'});
|
|
|
|
const constructorZone2: Zone = Zone.current.fork({name: 'Constructor Zone2'});
|
|
|
|
const constructorZone3: Zone = Zone.current.fork({name: 'Constructor Zone3'});
|
|
|
|
const subscriptionZone: Zone = Zone.current.fork({name: 'Subscription Zone'});
|
|
|
|
let observable1: Observable<any>;
|
|
|
|
let observable2: any;
|
|
|
|
|
|
|
|
let concatObservable: any;
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
log = [];
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
it('concat func callback should run in the correct zone', () => {
|
|
|
|
observable1 = constructorZone1.run(() => {
|
|
|
|
return new Observable(subscriber => {
|
|
|
|
expect(Zone.current.name).toEqual(constructorZone1.name);
|
|
|
|
subscriber.next(1);
|
|
|
|
subscriber.next(2);
|
|
|
|
subscriber.complete();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
observable2 = constructorZone2.run(() => {
|
|
|
|
return range(3, 4);
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
constructorZone3.run(() => {
|
|
|
|
concatObservable = concat(observable1, observable2);
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
subscriptionZone.run(() => {
|
|
|
|
concatObservable.subscribe((concat: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
log.push(concat);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(log).toEqual([1, 2, 3, 4, 5, 6]);
|
|
|
|
});
|
|
|
|
|
|
|
|
xit('concat func callback should run in the correct zone with scheduler',
|
|
|
|
asyncTest((done: any) => {
|
|
|
|
const constructorZone1: Zone = Zone.current.fork({name: 'Constructor Zone1'});
|
|
|
|
const constructorZone2: Zone = Zone.current.fork({name: 'Constructor Zone2'});
|
|
|
|
const constructorZone3: Zone = Zone.current.fork({name: 'Constructor Zone3'});
|
|
|
|
const subscriptionZone: Zone = Zone.current.fork({name: 'Subscription Zone'});
|
2020-04-13 19:40:21 -04:00
|
|
|
observable1 = constructorZone1.run(() => {
|
|
|
|
return of(1, 2);
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
observable2 = constructorZone2.run(() => {
|
|
|
|
return range(3, 4);
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
constructorZone3.run(() => {
|
|
|
|
concatObservable = concat(observable1, observable2, asapScheduler);
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
subscriptionZone.run(() => {
|
|
|
|
concatObservable.subscribe(
|
|
|
|
(concat: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
log.push(concat);
|
|
|
|
},
|
2020-04-13 19:40:21 -04:00
|
|
|
(error: any) => {
|
|
|
|
fail('subscribe failed' + error);
|
|
|
|
},
|
2019-05-31 11:56:07 -04:00
|
|
|
() => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
expect(log).toEqual([1, 2, 3, 4, 5, 6]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(log).toEqual([]);
|
|
|
|
}, Zone.root));
|
|
|
|
|
|
|
|
it('concatAll func callback should run in the correct zone', asyncTest((done: any) => {
|
|
|
|
const constructorZone1: Zone = Zone.current.fork({name: 'Constructor Zone1'});
|
|
|
|
const constructorZone2: Zone = Zone.current.fork({name: 'Constructor Zone2'});
|
|
|
|
const subscriptionZone: Zone = Zone.current.fork({name: 'Subscription Zone'});
|
2020-04-13 19:40:21 -04:00
|
|
|
observable1 = constructorZone1.run(() => {
|
|
|
|
return of(0, 1, 2);
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
constructorZone2.run(() => {
|
|
|
|
const highOrder = observable1.pipe(map((v: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(constructorZone2.name);
|
2020-04-13 19:40:21 -04:00
|
|
|
return of(v + 1);
|
2019-05-31 11:56:07 -04:00
|
|
|
}));
|
|
|
|
concatObservable = highOrder.pipe(concatAll());
|
|
|
|
});
|
|
|
|
|
|
|
|
subscriptionZone.run(() => {
|
|
|
|
concatObservable.subscribe(
|
|
|
|
(concat: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
log.push(concat);
|
|
|
|
},
|
2020-04-13 19:40:21 -04:00
|
|
|
(error: any) => {
|
|
|
|
fail('subscribe failed' + error);
|
|
|
|
},
|
2019-05-31 11:56:07 -04:00
|
|
|
() => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
expect(log).toEqual([1, 2, 3]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, Zone.root));
|
|
|
|
|
|
|
|
it('concatMap func callback should run in the correct zone', asyncTest((done: any) => {
|
|
|
|
const constructorZone1: Zone = Zone.current.fork({name: 'Constructor Zone1'});
|
|
|
|
const constructorZone2: Zone = Zone.current.fork({name: 'Constructor Zone2'});
|
|
|
|
const subscriptionZone: Zone = Zone.current.fork({name: 'Subscription Zone'});
|
|
|
|
observable1 = constructorZone1.run(() => {
|
|
|
|
return new Observable(subscriber => {
|
|
|
|
expect(Zone.current.name).toEqual(constructorZone1.name);
|
|
|
|
subscriber.next(1);
|
|
|
|
subscriber.next(2);
|
|
|
|
subscriber.next(3);
|
|
|
|
subscriber.next(4);
|
|
|
|
subscriber.complete();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
constructorZone2.run(() => {
|
|
|
|
concatObservable = observable1.pipe(concatMap((v: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(constructorZone2.name);
|
2020-04-13 19:40:21 -04:00
|
|
|
return of(0, 1);
|
2019-05-31 11:56:07 -04:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
subscriptionZone.run(() => {
|
|
|
|
concatObservable.subscribe(
|
|
|
|
(concat: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
log.push(concat);
|
|
|
|
},
|
2020-04-13 19:40:21 -04:00
|
|
|
(error: any) => {
|
|
|
|
fail('subscribe failed' + error);
|
|
|
|
},
|
2019-05-31 11:56:07 -04:00
|
|
|
() => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
expect(log).toEqual([0, 1, 0, 1, 0, 1, 0, 1]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, Zone.root));
|
|
|
|
|
|
|
|
it('concatMapTo func callback should run in the correct zone', asyncTest((done: any) => {
|
|
|
|
const constructorZone1: Zone = Zone.current.fork({name: 'Constructor Zone1'});
|
|
|
|
const constructorZone2: Zone = Zone.current.fork({name: 'Constructor Zone2'});
|
|
|
|
const subscriptionZone: Zone = Zone.current.fork({name: 'Subscription Zone'});
|
|
|
|
observable1 = constructorZone1.run(() => {
|
|
|
|
return new Observable(subscriber => {
|
|
|
|
expect(Zone.current.name).toEqual(constructorZone1.name);
|
|
|
|
subscriber.next(1);
|
|
|
|
subscriber.next(2);
|
|
|
|
subscriber.next(3);
|
|
|
|
subscriber.next(4);
|
|
|
|
subscriber.complete();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
constructorZone2.run(() => {
|
|
|
|
concatObservable = observable1.pipe(concatMapTo(of(0, 1)));
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
subscriptionZone.run(() => {
|
|
|
|
concatObservable.subscribe(
|
|
|
|
(concat: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
log.push(concat);
|
|
|
|
},
|
2020-04-13 19:40:21 -04:00
|
|
|
(error: any) => {
|
|
|
|
fail('subscribe failed' + error);
|
|
|
|
},
|
2019-05-31 11:56:07 -04:00
|
|
|
() => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
expect(log).toEqual([0, 1, 0, 1, 0, 1, 0, 1]);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, Zone.root));
|
|
|
|
});
|