Skip to content

Commit

Permalink
fix(component): detect zone.js using instanceof comparison (#2547)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguttandin authored Jun 15, 2020
1 parent ba0818e commit 7128667
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 102 deletions.
42 changes: 42 additions & 0 deletions modules/component/spec/core/utils/has-zone.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { hasZone } from '../../../src/core/utils/has-zone';
import { ApplicationRef, Component, NgModule, NgZone } from '@angular/core';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { getTestBed } from '@angular/core/testing';

describe('hasZone', () => {
async function setup({ defaultZone }: { defaultZone: boolean }) {
@Component({
selector: 'body',
template: '<div></div>',
})
class NgZoneTestComponent {
checkNgZone = hasZone(this.ngZone);
constructor(readonly ngZone: NgZone) {}
}

@NgModule({
declarations: [NgZoneTestComponent],
exports: [NgZoneTestComponent],
bootstrap: [NgZoneTestComponent],
imports: [NoopAnimationsModule],
})
class MyAppModule {}

const platform = getTestBed().platform;
const moduleRef = defaultZone
? await platform.bootstrapModule(MyAppModule)
: await platform.bootstrapModule(MyAppModule, { ngZone: 'noop' });
const appRef = moduleRef.injector.get(ApplicationRef);
const testComp = appRef.components[0].instance;

return { hasZone: testComp.checkNgZone };
}

it('returns false when default zone is used', async () => {
expect(await setup({ defaultZone: true })).toEqual({ hasZone: true });
});

it('returns true when noop zone is chosen', async () => {
expect(await setup({ defaultZone: false })).toEqual({ hasZone: false });
});
});
15 changes: 0 additions & 15 deletions modules/component/spec/core/utils/zone-check.spec.ts

This file was deleted.

8 changes: 1 addition & 7 deletions modules/component/spec/fixtures/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import createSpy = jasmine.createSpy;
import { ChangeDetectorRef } from '@angular/core';
import { MockNgZone } from './mock-ng-zone';
import { ChangeDetectorRef, NgZone } from '@angular/core';
import { MockNoopNgZone } from './mock-noop-ng-zone';

/**
* this is not exposed as NgZone should never be exposed to get miss matched with the real one
*/
class NgZone extends MockNgZone {}

/**
* this is not exposed as NgZone should never be exposed to get miss matched with the real one
*/
Expand Down
54 changes: 0 additions & 54 deletions modules/component/spec/fixtures/mock-ng-zone.ts

This file was deleted.

4 changes: 2 additions & 2 deletions modules/component/src/core/cd-aware/creator_render.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChangeDetectorRef, NgZone } from '@angular/core';
import { isNgZone } from '../utils';
import { hasZone } from '../utils';

export interface RenderConfig {
ngZone: NgZone;
Expand All @@ -8,7 +8,7 @@ export interface RenderConfig {

export function createRender<T>(config: RenderConfig): () => void {
function render() {
if (isNgZone(config.ngZone)) {
if (hasZone(config.ngZone)) {
config.cdRef.markForCheck();
} else {
config.cdRef.detectChanges();
Expand Down
2 changes: 1 addition & 1 deletion modules/component/src/core/utils/has-zone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ import { NgZone } from '@angular/core';
* ```
*/
export function hasZone(z: NgZone): boolean {
return z.constructor.name !== 'NoopNgZone';
return z instanceof NgZone;
}
2 changes: 1 addition & 1 deletion modules/component/src/core/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './get-global-this';
export * from './zone-checks';
export * from './has-zone';
export * from './is-ivy';
22 changes: 0 additions & 22 deletions modules/component/src/core/utils/zone-checks.ts

This file was deleted.

0 comments on commit 7128667

Please sign in to comment.