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, bindCallback, bindNodeCallback, Observable} from 'rxjs';
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
import {asyncTest} from '../test-util';
|
|
|
|
|
|
|
|
describe('Observable.bindNodeCallback', () => {
|
|
|
|
let log: any[];
|
|
|
|
const constructorZone: Zone = Zone.root.fork({name: 'Constructor Zone'});
|
|
|
|
const subscriptionZone: Zone = Zone.root.fork({name: 'Subscription Zone'});
|
|
|
|
let func: any;
|
|
|
|
let boundFunc: any;
|
|
|
|
let observable: any;
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
log = [];
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
|
|
|
|
it('bindNodeCallback func callback should run in the correct zone', () => {
|
|
|
|
constructorZone.run(() => {
|
|
|
|
func = function(arg: any, callback: (error: any, result: any) => any) {
|
|
|
|
expect(Zone.current.name).toEqual(constructorZone.name);
|
|
|
|
callback(null, arg);
|
|
|
|
};
|
|
|
|
boundFunc = bindNodeCallback(func);
|
|
|
|
observable = boundFunc('test');
|
|
|
|
});
|
|
|
|
|
|
|
|
subscriptionZone.run(() => {
|
|
|
|
observable.subscribe((arg: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
log.push('next' + arg);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(log).toEqual(['nexttest']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('bindNodeCallback with selector should run in correct zone', () => {
|
|
|
|
constructorZone.run(() => {
|
|
|
|
func = function(arg: any, callback: (error: any, result: any) => any) {
|
|
|
|
expect(Zone.current.name).toEqual(constructorZone.name);
|
|
|
|
callback(null, arg);
|
|
|
|
};
|
|
|
|
boundFunc = bindNodeCallback(func, (arg: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(constructorZone.name);
|
|
|
|
return 'selector' + arg;
|
|
|
|
});
|
|
|
|
observable = boundFunc('test');
|
|
|
|
});
|
|
|
|
|
|
|
|
subscriptionZone.run(() => {
|
|
|
|
observable.subscribe((arg: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
log.push('next' + arg);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(log).toEqual(['nextselectortest']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('bindNodeCallback with async scheduler should run in correct zone', asyncTest((done: any) => {
|
|
|
|
constructorZone.run(() => {
|
|
|
|
func = function(arg: any, callback: (error: any, result: any) => any) {
|
|
|
|
expect(Zone.current.name).toEqual(constructorZone.name);
|
|
|
|
callback(null, arg);
|
|
|
|
};
|
|
|
|
boundFunc = bindCallback(func, () => true, asapScheduler);
|
|
|
|
observable = boundFunc('test');
|
|
|
|
});
|
|
|
|
|
|
|
|
subscriptionZone.run(() => {
|
|
|
|
observable.subscribe((arg: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
log.push('next' + arg);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(log).toEqual([]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('bindNodeCallback call with error should run in correct zone', () => {
|
|
|
|
constructorZone.run(() => {
|
|
|
|
func = function(arg: any, callback: (error: any, result: any) => any) {
|
|
|
|
expect(Zone.current.name).toEqual(constructorZone.name);
|
|
|
|
callback(arg, null);
|
|
|
|
};
|
|
|
|
boundFunc = bindCallback(func);
|
|
|
|
observable = boundFunc('test');
|
|
|
|
});
|
|
|
|
|
|
|
|
subscriptionZone.run(() => {
|
|
|
|
observable.subscribe(
|
|
|
|
(arg: any) => {
|
|
|
|
expect(Zone.current.name).toEqual(subscriptionZone.name);
|
|
|
|
log.push('next' + arg);
|
|
|
|
},
|
2020-04-13 19:40:21 -04:00
|
|
|
(error: any) => {
|
|
|
|
log.push('error' + error);
|
|
|
|
});
|
2019-05-31 11:56:07 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(log).toEqual(['nexttest,']);
|
|
|
|
});
|
|
|
|
});
|