2016-10-19 16:41:04 -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
|
|
|
|
*/
|
|
|
|
|
2019-03-22 05:42:52 -04:00
|
|
|
import {Component, Directive, ElementRef, Injector, Input, NgModule, NgZone, SimpleChanges, destroyPlatform} from '@angular/core';
|
2016-10-19 16:41:04 -04:00
|
|
|
import {async} from '@angular/core/testing';
|
|
|
|
import {BrowserModule} from '@angular/platform-browser';
|
|
|
|
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
|
2016-10-20 22:35:35 -04:00
|
|
|
import {UpgradeComponent, UpgradeModule, downgradeComponent} from '@angular/upgrade/static';
|
2016-10-19 16:41:04 -04:00
|
|
|
|
2019-03-22 05:42:52 -04:00
|
|
|
import * as angular from '../../../src/common/src/angular1';
|
|
|
|
import {html, withEachNg1Version} from '../../../src/common/test/helpers/common_test_helpers';
|
|
|
|
|
|
|
|
import {bootstrap} from './static_test_helpers';
|
2016-10-19 16:41:04 -04:00
|
|
|
|
2018-02-15 12:21:18 -05:00
|
|
|
withEachNg1Version(() => {
|
fix(upgrade): do not break if `onMicrotaskEmpty` emits while a `$digest` is in progress (#29794)
Previously, under certain circumstances, `NgZone#onMicrotaskEmpty` could
emit while a `$digest` was in progress, thus triggering another
`$digest`, which in turn would throw a `$digest already in progress`
error. Furthermore, throwing an error from inside the `onMicrotaskEmpty`
subscription would result in unsubscribing and stop triggering further
`$digest`s, when `onMicrotaskEmpty` emitted.
Usually, emitting while a `$digest` was already in progress was a result
of unintentionally running some part of AngularJS outside the Angular
zone, but there are valid (if rare) usecases where this can happen
(see #24680 for details).
This commit addresses the issue as follows:
- If a `$digest` is in progress when `onMicrotaskEmpty` emits, do not
trigger another `$digest` (to avoid the error). `$evalAsync()` is used
instead, to ensure that the bindings are evaluated at least once more.
- Since there is still a high probability that the situation is a result
of programming error (i.e. some AngularJS part running outside the
Angular Zone), a warning will be logged, but only if the app is in
[dev mode][1].
[1]: https://github.com/angular/angular/blob/78146c189/packages/core/src/util/ng_dev_mode.ts#L12
Fixes #24680
PR Close #29794
2019-04-23 04:13:50 -04:00
|
|
|
describe('change-detection', () => {
|
2016-10-19 16:41:04 -04:00
|
|
|
beforeEach(() => destroyPlatform());
|
|
|
|
afterEach(() => destroyPlatform());
|
|
|
|
|
fix(upgrade): do not break if `onMicrotaskEmpty` emits while a `$digest` is in progress (#29794)
Previously, under certain circumstances, `NgZone#onMicrotaskEmpty` could
emit while a `$digest` was in progress, thus triggering another
`$digest`, which in turn would throw a `$digest already in progress`
error. Furthermore, throwing an error from inside the `onMicrotaskEmpty`
subscription would result in unsubscribing and stop triggering further
`$digest`s, when `onMicrotaskEmpty` emitted.
Usually, emitting while a `$digest` was already in progress was a result
of unintentionally running some part of AngularJS outside the Angular
zone, but there are valid (if rare) usecases where this can happen
(see #24680 for details).
This commit addresses the issue as follows:
- If a `$digest` is in progress when `onMicrotaskEmpty` emits, do not
trigger another `$digest` (to avoid the error). `$evalAsync()` is used
instead, to ensure that the bindings are evaluated at least once more.
- Since there is still a high probability that the situation is a result
of programming error (i.e. some AngularJS part running outside the
Angular Zone), a warning will be logged, but only if the app is in
[dev mode][1].
[1]: https://github.com/angular/angular/blob/78146c189/packages/core/src/util/ng_dev_mode.ts#L12
Fixes #24680
PR Close #29794
2019-04-23 04:13:50 -04:00
|
|
|
it('should not break if a $digest is already in progress', async(() => {
|
|
|
|
const element = html('<my-app></my-app>');
|
|
|
|
|
|
|
|
@Component({selector: 'my-app', template: ''})
|
|
|
|
class AppComponent {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [AppComponent],
|
|
|
|
entryComponents: [AppComponent],
|
|
|
|
imports: [BrowserModule, UpgradeModule]
|
|
|
|
})
|
|
|
|
class Ng2Module {
|
|
|
|
ngDoBootstrap() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ng1Module = angular.module('ng1', []).directive(
|
|
|
|
'myApp', downgradeComponent({component: AppComponent}));
|
|
|
|
|
|
|
|
bootstrap(platformBrowserDynamic(), Ng2Module, element, ng1Module).then((upgrade) => {
|
|
|
|
const $rootScope = upgrade.$injector.get('$rootScope') as angular.IRootScopeService;
|
|
|
|
const ngZone: NgZone = upgrade.ngZone;
|
|
|
|
|
|
|
|
// Wrap in a setTimeout to ensure all boostrap operations have completed.
|
|
|
|
setTimeout(
|
|
|
|
// Run inside the Angular zone, so that operations such as emitting
|
|
|
|
// `onMicrotaskEmpty` do not trigger entering/existing the zone (and thus another
|
|
|
|
// `$digest`). This also closer simulates what would happen in a real app.
|
|
|
|
() => ngZone.run(() => {
|
|
|
|
const digestSpy = spyOn($rootScope, '$digest').and.callThrough();
|
|
|
|
|
|
|
|
// Step 1: Ensure `$digest` is run on `onMicrotaskEmpty`.
|
|
|
|
ngZone.onMicrotaskEmpty.emit(null);
|
|
|
|
expect(digestSpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
digestSpy.calls.reset();
|
|
|
|
|
|
|
|
// Step 2: Cause the issue.
|
|
|
|
$rootScope.$apply(() => ngZone.onMicrotaskEmpty.emit(null));
|
|
|
|
|
|
|
|
// With the fix, `$digest` will only be run once (for `$apply()`).
|
|
|
|
// Without the fix, `$digest()` would have been run an extra time
|
|
|
|
// (`onMicrotaskEmpty`).
|
|
|
|
expect(digestSpy).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
digestSpy.calls.reset();
|
|
|
|
|
|
|
|
// Step 3: Ensure that `$digest()` is still executed on `onMicrotaskEmpty`.
|
|
|
|
ngZone.onMicrotaskEmpty.emit(null);
|
|
|
|
expect(digestSpy).toHaveBeenCalledTimes(1);
|
|
|
|
}),
|
|
|
|
0);
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2019-01-11 19:45:23 -05:00
|
|
|
it('should interleave scope and component expressions', async(() => {
|
|
|
|
const log: string[] = [];
|
|
|
|
const l = (value: string) => {
|
|
|
|
log.push(value);
|
|
|
|
return value + ';';
|
|
|
|
};
|
|
|
|
|
|
|
|
@Directive({selector: 'ng1a'})
|
|
|
|
class Ng1aComponent extends UpgradeComponent {
|
|
|
|
constructor(elementRef: ElementRef, injector: Injector) {
|
|
|
|
super('ng1a', elementRef, injector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Directive({selector: 'ng1b'})
|
|
|
|
class Ng1bComponent extends UpgradeComponent {
|
|
|
|
constructor(elementRef: ElementRef, injector: Injector) {
|
|
|
|
super('ng1b', elementRef, injector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'ng2',
|
|
|
|
template: `{{l('2A')}}<ng1a></ng1a>{{l('2B')}}<ng1b></ng1b>{{l('2C')}}`
|
|
|
|
})
|
|
|
|
class Ng2Component {
|
|
|
|
l = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [Ng1aComponent, Ng1bComponent, Ng2Component],
|
|
|
|
entryComponents: [Ng2Component],
|
|
|
|
imports: [BrowserModule, UpgradeModule]
|
|
|
|
})
|
|
|
|
class Ng2Module {
|
|
|
|
ngDoBootstrap() {}
|
|
|
|
}
|
|
|
|
|
2019-04-23 11:00:04 -04:00
|
|
|
const ng1Module = angular.module_('ng1', [])
|
2019-01-11 19:45:23 -05:00
|
|
|
.directive('ng1a', () => ({template: '{{ l(\'ng1a\') }}'}))
|
|
|
|
.directive('ng1b', () => ({template: '{{ l(\'ng1b\') }}'}))
|
|
|
|
.directive('ng2', downgradeComponent({component: Ng2Component}))
|
|
|
|
.run(($rootScope: angular.IRootScopeService) => {
|
|
|
|
$rootScope.l = l;
|
|
|
|
$rootScope.reset = () => log.length = 0;
|
|
|
|
});
|
|
|
|
|
|
|
|
const element =
|
|
|
|
html('<div>{{reset(); l(\'1A\');}}<ng2>{{l(\'1B\')}}</ng2>{{l(\'1C\')}}</div>');
|
|
|
|
bootstrap(platformBrowserDynamic(), Ng2Module, element, ng1Module).then((upgrade) => {
|
|
|
|
expect(document.body.textContent).toEqual('1A;2A;ng1a;2B;ng1b;2C;1C;');
|
|
|
|
expect(log).toEqual(['1A', '1C', '2A', '2B', '2C', 'ng1a', 'ng1b']);
|
|
|
|
});
|
|
|
|
}));
|
2017-01-20 11:38:14 -05:00
|
|
|
|
2019-01-02 18:12:36 -05:00
|
|
|
it('should propagate changes to a downgraded component inside the ngZone', async(() => {
|
|
|
|
const element = html('<my-app></my-app>');
|
|
|
|
let appComponent: AppComponent;
|
|
|
|
|
|
|
|
@Component({selector: 'my-app', template: '<my-child [value]="value"></my-child>'})
|
|
|
|
class AppComponent {
|
|
|
|
value?: number;
|
|
|
|
constructor() { appComponent = this; }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'my-child',
|
|
|
|
template: '<div>{{ valueFromPromise }}</div>',
|
|
|
|
})
|
|
|
|
class ChildComponent {
|
|
|
|
valueFromPromise?: number;
|
|
|
|
@Input()
|
|
|
|
set value(v: number) { expect(NgZone.isInAngularZone()).toBe(true); }
|
|
|
|
|
|
|
|
constructor(private zone: NgZone) {}
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
|
|
if (changes['value'].isFirstChange()) return;
|
|
|
|
|
|
|
|
this.zone.onMicrotaskEmpty.subscribe(
|
|
|
|
() => { expect(element.textContent).toEqual('5'); });
|
|
|
|
|
|
|
|
// Create a micro-task to update the value to be rendered asynchronously.
|
|
|
|
Promise.resolve().then(() => this.valueFromPromise = changes['value'].currentValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
declarations: [AppComponent, ChildComponent],
|
|
|
|
entryComponents: [AppComponent],
|
|
|
|
imports: [BrowserModule, UpgradeModule]
|
|
|
|
})
|
|
|
|
class Ng2Module {
|
|
|
|
ngDoBootstrap() {}
|
|
|
|
}
|
|
|
|
|
2019-04-23 11:00:04 -04:00
|
|
|
const ng1Module = angular.module_('ng1', []).directive(
|
2019-01-02 18:12:36 -05:00
|
|
|
'myApp', downgradeComponent({component: AppComponent}));
|
|
|
|
|
|
|
|
bootstrap(platformBrowserDynamic(), Ng2Module, element, ng1Module).then((upgrade) => {
|
|
|
|
appComponent.value = 5;
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2017-01-20 11:38:14 -05:00
|
|
|
// This test demonstrates https://github.com/angular/angular/issues/6385
|
|
|
|
// which was invalidly fixed by https://github.com/angular/angular/pull/6386
|
|
|
|
// it('should not trigger $digest from an async operation in a watcher', async(() => {
|
|
|
|
// @Component({selector: 'my-app', template: ''})
|
|
|
|
// class AppComponent {
|
|
|
|
// }
|
|
|
|
|
|
|
|
// @NgModule({declarations: [AppComponent], imports: [BrowserModule]})
|
|
|
|
// class Ng2Module {
|
|
|
|
// }
|
|
|
|
|
|
|
|
// const adapter: UpgradeAdapter = new UpgradeAdapter(forwardRef(() => Ng2Module));
|
|
|
|
// const ng1Module = angular.module('ng1', []).directive(
|
|
|
|
// 'myApp', adapter.downgradeNg2Component(AppComponent));
|
|
|
|
|
|
|
|
// const element = html('<my-app></my-app>');
|
|
|
|
|
|
|
|
// adapter.bootstrap(element, ['ng1']).ready((ref) => {
|
|
|
|
// let doTimeout = false;
|
|
|
|
// let timeoutId: number;
|
|
|
|
// ref.ng1RootScope.$watch(() => {
|
|
|
|
// if (doTimeout && !timeoutId) {
|
|
|
|
// timeoutId = window.setTimeout(function() {
|
|
|
|
// timeoutId = null;
|
|
|
|
// }, 10);
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// doTimeout = true;
|
|
|
|
// });
|
|
|
|
// }));
|
2016-10-19 16:41:04 -04:00
|
|
|
});
|
2018-02-15 12:21:18 -05:00
|
|
|
});
|