Skip to content

Commit

Permalink
fix(component): detect zone.js using instanceof comparison (ngrx#2547)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguttandin authored and BioPhoton committed Jun 23, 2020
1 parent 303b8aa commit 458204c
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 100 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
Empty file.
22 changes: 0 additions & 22 deletions modules/component/src/core/utils/zone-checks.ts

This file was deleted.

0 comments on commit 458204c

Please sign in to comment.