diff --git a/modules/component/spec/core/utils/has-zone.spec.ts b/modules/component/spec/core/utils/has-zone.spec.ts new file mode 100644 index 0000000000..ae64193786 --- /dev/null +++ b/modules/component/spec/core/utils/has-zone.spec.ts @@ -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: '
', + }) + 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 }); + }); +}); diff --git a/modules/component/spec/core/utils/zone-check.spec.ts b/modules/component/spec/core/utils/zone-check.spec.ts deleted file mode 100644 index d9ed62fb73..0000000000 --- a/modules/component/spec/core/utils/zone-check.spec.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { isNgZone } from '../../../src/core/utils'; -import { - manualInstanceNgZone, - manualInstanceNoopNgZone, -} from '../../fixtures/fixtures'; - -describe('envZonePatched', () => { - it('should return true if `zone.js` did patch the global API', () => { - expect(isNgZone(manualInstanceNgZone)).toBe(true); - }); - - it('should return false if `zone.js` did not patch the global API', () => { - expect(isNgZone(manualInstanceNoopNgZone)).toBe(false); - }); -}); diff --git a/modules/component/spec/fixtures/fixtures.ts b/modules/component/spec/fixtures/fixtures.ts index eec051efd7..67814d87cf 100644 --- a/modules/component/spec/fixtures/fixtures.ts +++ b/modules/component/spec/fixtures/fixtures.ts @@ -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 */ diff --git a/modules/component/spec/fixtures/mock-ng-zone.ts b/modules/component/spec/fixtures/mock-ng-zone.ts deleted file mode 100644 index 57d1ed9b79..0000000000 --- a/modules/component/spec/fixtures/mock-ng-zone.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { MockEventEmitter } from './mock-event-emitter'; - -/** - * source: https://github.com/angular/angular/blob/master/packages/core/src/zone/ng_zone.ts#L88 - */ -export class MockNgZone { - readonly hasPendingMacrotasks: boolean = false; - readonly hasPendingMicrotasks: boolean = false; - readonly isStable: boolean = true; - readonly onUnstable: MockEventEmitter = new MockEventEmitter(false); - readonly onMicrotaskEmpty: MockEventEmitter = new MockEventEmitter( - false - ); - readonly onStable: MockEventEmitter = new MockEventEmitter(false); - readonly onError: MockEventEmitter = new MockEventEmitter(false); - - static isInAngularZone(): boolean { - return true; - } - - static assertInAngularZone(): void {} - - static assertNotInAngularZone(): void {} - - constructor({ - enableLongStackTrace = false, - shouldCoalesceEventChangeDetection = false, - }) {} - - run(fn: Function): any { - return fn(); - } - - runTask( - fn: (...args: any[]) => T, - applyThis?: any, - applyArgs?: any[], - name?: string - ): T { - return undefined as any; - } - - runGuarded( - fn: (...args: any[]) => T, - applyThis?: any, - applyArgs?: any[] - ): T { - return undefined as any; - } - - runOutsideAngular(fn: Function): any { - return fn(); - } -} diff --git a/modules/component/src/core/cd-aware/creator_render.ts b/modules/component/src/core/cd-aware/creator_render.ts index 467c7bb541..98733b4c27 100644 --- a/modules/component/src/core/cd-aware/creator_render.ts +++ b/modules/component/src/core/cd-aware/creator_render.ts @@ -1,5 +1,5 @@ import { ChangeDetectorRef, NgZone } from '@angular/core'; -import { isNgZone } from '../utils'; +import { hasZone } from '../utils'; export interface RenderConfig { ngZone: NgZone; @@ -8,7 +8,7 @@ export interface RenderConfig { export function createRender(config: RenderConfig): () => void { function render() { - if (isNgZone(config.ngZone)) { + if (hasZone(config.ngZone)) { config.cdRef.markForCheck(); } else { config.cdRef.detectChanges(); diff --git a/modules/component/src/core/utils/has-zone.ts b/modules/component/src/core/utils/has-zone.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/modules/component/src/core/utils/zone-checks.ts b/modules/component/src/core/utils/zone-checks.ts deleted file mode 100644 index f1438f5373..0000000000 --- a/modules/component/src/core/utils/zone-checks.ts +++ /dev/null @@ -1,22 +0,0 @@ -/** - * isNgZone - * - * @description - * - * This function takes any instance of a class and checks - * if the constructor name is equal to `NgZone`. - * This means the Angular application that instantiated this service assumes it runs in a ZuneLess environment, - * and therefor it's change detection will not be triggered by zone related logic. - * - * However, keep in mind this does not mean `zone.js` is not present. - * The environment could still run in ZoneFull mode even if Angular turned it off. - * Consider the situation of a Angular element configured for ZoneLess - * environments is used in an Angular application relining on the zone mechanism. - * - * @param instance - The instance to check for constructor name of `NgZone`. - * @return boolean - true if instance is of type `NgZone`. - * - */ -export function isNgZone(instance: any): boolean { - return instance?.constructor?.name === 'NgZone'; -}