From 280b8c6d3f904a5a7b4ad69416838e07e6869e34 Mon Sep 17 00:00:00 2001 From: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Date: Thu, 15 Dec 2022 19:20:38 +0100 Subject: [PATCH] fix: fix: regression autoDetectChanges --- .../testing-library/src/lib/testing-library.ts | 16 +++++++++++----- .../tests/issues/issue-346.spec.ts | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 projects/testing-library/tests/issues/issue-346.spec.ts diff --git a/projects/testing-library/src/lib/testing-library.ts b/projects/testing-library/src/lib/testing-library.ts index 1b1428d..387ae10 100644 --- a/projects/testing-library/src/lib/testing-library.ts +++ b/projects/testing-library/src/lib/testing-library.ts @@ -66,7 +66,16 @@ export async function render( defaultImports = [], } = { ...globalConfig, ...renderOptions }; - dtlConfigure(domConfig); + dtlConfigure({ + eventWrapper: (cb) => { + const result = cb(); + if (autoDetectChanges) { + detectChangesForMountedFixtures(); + } + return result; + }, + ...domConfig, + }); TestBed.configureTestingModule({ declarations: addAutoDeclarations(sut, { @@ -183,6 +192,7 @@ export async function render( result = doNavigate(); } + detectChanges(); return result ?? false; }; @@ -234,10 +244,6 @@ export async function render( fixture.componentInstance.ngOnChanges(changes); } - if (autoDetectChanges) { - fixture.autoDetectChanges(true); - } - detectChanges = () => { if (isAlive) { fixture.detectChanges(); diff --git a/projects/testing-library/tests/issues/issue-346.spec.ts b/projects/testing-library/tests/issues/issue-346.spec.ts new file mode 100644 index 0000000..ef1b7a3 --- /dev/null +++ b/projects/testing-library/tests/issues/issue-346.spec.ts @@ -0,0 +1,17 @@ +import { Component } from '@angular/core'; +import { render } from '../../src/public_api'; + +test('issue 364 detectChangesOnRender', async () => { + @Component({ + selector: 'atl-fixture', + template: `{{ myObj.myProp }}`, + }) + class MyComponent { + myObj: any = null; + } + + // autoDetectChanges invokes change detection, which makes the test fail + await render(MyComponent, { + detectChangesOnRender: false, + }); +});