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 {Observable, of, Subject} from 'rxjs';
|
2019-05-31 11:56:07 -04:00
|
|
|
import {mapTo, multicast, tap} from 'rxjs/operators';
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: @JiaLiPassion, Observable.prototype.multicast return a readonly _subscribe
|
|
|
|
// should find another way to patch subscribe
|
|
|
|
describe('Observable.multicast', () => {
|
|
|
|
let log: any[];
|
|
|
|
const constructorZone1: Zone = Zone.current.fork({name: 'Constructor Zone1'});
|
|
|
|
const doZone1: Zone = Zone.current.fork({name: 'Do Zone1'});
|
|
|
|
const mapZone1: Zone = Zone.current.fork({name: 'Map Zone1'});
|
|
|
|
const multicastZone1: Zone = Zone.current.fork({name: 'Multicast Zone1'});
|
|
|
|
const subscriptionZone: Zone = Zone.current.fork({name: 'Subscription Zone'});
|
|
|
|
let observable1: Observable<any>;
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
log = [];
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
it('multicast func callback should run in the correct zone', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
observable1 = constructorZone1.run(() => {
|
|
|
|
return of(1, 2, 3);
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
observable1 = doZone1.run(() => {
|
|
|
|
return observable1.pipe(tap((v: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(doZone1.name);
|
|
|
|
log.push('do' + v);
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
observable1 = mapZone1.run(() => {
|
|
|
|
return observable1.pipe(mapTo('test'));
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
const multi: any = multicastZone1.run(() => {
|
|
|
|
return observable1.pipe(multicast(() => {
|
|
|
|
expect(Zone.current.name).toEqual(multicastZone1.name);
|
|
|
|
return new Subject();
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
multi.subscribe((val: any) => {
|
|
|
|
log.push('one' + val);
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
multi.subscribe((val: any) => {
|
|
|
|
log.push('two' + val);
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
multi.connect();
|
|
|
|
|
|
|
|
subscriptionZone.run(() => {
|
|
|
|
observable1.subscribe(
|
|
|
|
(result: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
log.push(result);
|
|
|
|
},
|
2020-04-13 19:40:21 -04:00
|
|
|
() => {
|
|
|
|
fail('should not call error');
|
|
|
|
},
|
2019-05-31 11:56:07 -04:00
|
|
|
() => {
|
|
|
|
log.push('completed');
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(log).toEqual([
|
|
|
|
'do1', 'onetest', 'twotest', 'do2', 'onetest', 'twotest', 'do3', 'onetest', 'twotest', 'do1',
|
|
|
|
'test', 'do2', 'test', 'do3', 'test', 'completed'
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|