angular-cn/packages/zone.js/test/rxjs/rxjs.Observable.take.spec.ts

133 lines
4.4 KiB
TypeScript

/**
* @license
* Copyright Google LLC 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
*/
import {interval, Observable, of} from 'rxjs';
import {take, takeLast, takeUntil, takeWhile} from 'rxjs/operators';
import {asyncTest} from '../test-util';
describe('Observable.take', () => {
let log: any[];
let observable1: Observable<any>;
let defaultTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
beforeEach(() => {
log = [];
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
});
afterEach(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = defaultTimeout;
});
it('take func callback should run in the correct zone', () => {
const constructorZone1: Zone = Zone.current.fork({name: 'Constructor Zone1'});
const subscriptionZone: Zone = Zone.current.fork({name: 'Subscription Zone'});
observable1 = constructorZone1.run(() => {
return of(1, 2, 3).pipe(take(1));
});
subscriptionZone.run(() => {
observable1.subscribe(
(result: any) => {
log.push(result);
expect(Zone.current.name).toEqual(subscriptionZone.name);
},
(err: any) => {
fail('should not call error');
},
() => {
log.push('completed');
expect(Zone.current.name).toEqual(subscriptionZone.name);
expect(log).toEqual([1, 'completed']);
});
});
});
it('takeLast func callback should run in the correct zone', () => {
const constructorZone1: Zone = Zone.current.fork({name: 'Constructor Zone1'});
const subscriptionZone: Zone = Zone.current.fork({name: 'Subscription Zone'});
observable1 = constructorZone1.run(() => {
return of(1, 2, 3).pipe(takeLast(1));
});
subscriptionZone.run(() => {
observable1.subscribe(
(result: any) => {
log.push(result);
expect(Zone.current.name).toEqual(subscriptionZone.name);
},
(err: any) => {
fail('should not call error');
},
() => {
log.push('completed');
expect(Zone.current.name).toEqual(subscriptionZone.name);
expect(log).toEqual([3, 'completed']);
});
});
});
xit('takeUntil func callback should run in the correct zone', asyncTest((done: any) => {
const constructorZone1: Zone = Zone.current.fork({name: 'Constructor Zone1'});
const subscriptionZone: Zone = Zone.current.fork({name: 'Subscription Zone'});
observable1 = constructorZone1.run(() => {
return interval(10).pipe(takeUntil(interval(25)));
});
subscriptionZone.run(() => {
observable1.subscribe(
(result: any) => {
log.push(result);
expect(Zone.current.name).toEqual(subscriptionZone.name);
},
(err: any) => {
fail('should not call error');
},
() => {
log.push('completed');
expect(Zone.current.name).toEqual(subscriptionZone.name);
expect(log).toEqual([0, 1, 'completed']);
done();
});
});
}, Zone.root));
it('takeWhile func callback should run in the correct zone', asyncTest((done: any) => {
const constructorZone1: Zone = Zone.current.fork({name: 'Constructor Zone1'});
const takeZone1: Zone = Zone.current.fork({name: 'Take Zone1'});
const subscriptionZone: Zone = Zone.current.fork({name: 'Subscription Zone'});
observable1 = constructorZone1.run(() => {
return interval(10);
});
observable1 = takeZone1.run(() => {
return observable1.pipe(takeWhile((val: any) => {
expect(Zone.current.name).toEqual(takeZone1.name);
return val < 2;
}));
});
subscriptionZone.run(() => {
observable1.subscribe(
(result: any) => {
log.push(result);
expect(Zone.current.name).toEqual(subscriptionZone.name);
},
(err: any) => {
fail('should not call error');
},
() => {
log.push('completed');
expect(Zone.current.name).toEqual(subscriptionZone.name);
expect(log).toEqual([0, 1, 'completed']);
done();
});
});
}, Zone.root));
});