2019-05-31 11:56:07 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* 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, of} from 'rxjs';
|
2019-05-31 11:56:07 -04:00
|
|
|
import {map, observeOn} from 'rxjs/operators';
|
|
|
|
|
|
|
|
import {asyncTest} from '../test-util';
|
|
|
|
|
|
|
|
describe('Scheduler.asap', () => {
|
|
|
|
let log: any[];
|
|
|
|
let errorCallback: Function;
|
|
|
|
const constructorZone: Zone = Zone.root.fork({name: 'Constructor Zone'});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
log = [];
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
it('scheduler asap should run in correct zone', asyncTest((done: any) => {
|
|
|
|
let observable: any;
|
2020-04-13 19:40:21 -04:00
|
|
|
constructorZone.run(() => {
|
|
|
|
observable = of(1, 2, 3).pipe(observeOn(asapScheduler));
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
const zone = Zone.current.fork({name: 'subscribeZone'});
|
|
|
|
|
|
|
|
zone.run(() => {
|
2020-04-13 19:40:21 -04:00
|
|
|
observable
|
|
|
|
.pipe(map((value: number) => {
|
|
|
|
return value;
|
|
|
|
}))
|
2019-05-31 11:56:07 -04:00
|
|
|
.subscribe(
|
|
|
|
(value: number) => {
|
|
|
|
expect(Zone.current.name).toEqual(zone.name);
|
|
|
|
if (value === 3) {
|
|
|
|
setTimeout(done);
|
|
|
|
}
|
|
|
|
},
|
2020-04-13 19:40:21 -04:00
|
|
|
(err: any) => {
|
|
|
|
fail('should not be here');
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
});
|
|
|
|
}, Zone.root));
|
|
|
|
|
|
|
|
it('scheduler asap error should run in correct zone', asyncTest((done: any) => {
|
|
|
|
let observable: any;
|
2020-04-13 19:40:21 -04:00
|
|
|
constructorZone.run(() => {
|
|
|
|
observable = of(1, 2, 3).pipe(observeOn(asapScheduler));
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
Zone.root.run(() => {
|
|
|
|
observable
|
|
|
|
.pipe(map((value: number) => {
|
|
|
|
if (value === 3) {
|
|
|
|
throw new Error('oops');
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}))
|
|
|
|
.subscribe((value: number) => {}, (err: any) => {
|
|
|
|
expect(err.message).toEqual('oops');
|
|
|
|
expect(Zone.current.name).toEqual('<root>');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, Zone.root));
|
|
|
|
});
|