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 {interval, Observable, of} from 'rxjs';
|
2019-05-31 11:56:07 -04:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
afterEach(() => {
|
|
|
|
jasmine.DEFAULT_TIMEOUT_INTERVAL = defaultTimeout;
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
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'});
|
2020-04-13 19:40:21 -04:00
|
|
|
observable1 = constructorZone1.run(() => {
|
|
|
|
return of(1, 2, 3).pipe(take(1));
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
subscriptionZone.run(() => {
|
|
|
|
observable1.subscribe(
|
|
|
|
(result: any) => {
|
|
|
|
log.push(result);
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
},
|
2020-04-13 19:40:21 -04:00
|
|
|
(err: any) => {
|
|
|
|
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([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'});
|
2020-04-13 19:40:21 -04:00
|
|
|
observable1 = constructorZone1.run(() => {
|
|
|
|
return of(1, 2, 3).pipe(takeLast(1));
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
subscriptionZone.run(() => {
|
|
|
|
observable1.subscribe(
|
|
|
|
(result: any) => {
|
|
|
|
log.push(result);
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
},
|
2020-04-13 19:40:21 -04:00
|
|
|
(err: any) => {
|
|
|
|
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([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'});
|
2020-04-13 19:40:21 -04:00
|
|
|
observable1 = constructorZone1.run(() => {
|
|
|
|
return interval(10).pipe(takeUntil(interval(25)));
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
subscriptionZone.run(() => {
|
|
|
|
observable1.subscribe(
|
|
|
|
(result: any) => {
|
|
|
|
log.push(result);
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
},
|
2020-04-13 19:40:21 -04:00
|
|
|
(err: any) => {
|
|
|
|
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([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'});
|
2020-04-13 19:40:21 -04:00
|
|
|
observable1 = constructorZone1.run(() => {
|
|
|
|
return interval(10);
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
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);
|
|
|
|
},
|
2020-04-13 19:40:21 -04:00
|
|
|
(err: any) => {
|
|
|
|
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([0, 1, 'completed']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, Zone.root));
|
|
|
|
});
|