Skip to content

Commit 50aa0ac

Browse files
feat: add ability to turn off auto detect changes (#329)
1 parent ce3a612 commit 50aa0ac

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

Diff for: projects/testing-library/src/lib/models.ts

+13
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ export interface RenderResult<ComponentType, WrapperType = ComponentType> extend
7676
}
7777

7878
export interface RenderComponentOptions<ComponentType, Q extends Queries = typeof queries> {
79+
/**
80+
* @description
81+
* Automatically detect changes as a "real" running component would do.
82+
*
83+
* @default
84+
* true
85+
*
86+
* @example
87+
* const component = await render(AppComponent, {
88+
* autoDetectChanges: false
89+
* })
90+
*/
91+
autoDetectChanges?: boolean;
7992
/**
8093
* @description
8194
* Will call detectChanges when the component is compiled

Diff for: projects/testing-library/src/lib/testing-library.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export async function render<SutType, WrapperType = SutType>(
4747
const { dom: domConfig, ...globalConfig } = getConfig();
4848
const {
4949
detectChanges: detectChangesOnRender = true,
50+
autoDetectChanges = true,
5051
declarations = [],
5152
imports = [],
5253
providers = [],
@@ -64,14 +65,7 @@ export async function render<SutType, WrapperType = SutType>(
6465
defaultImports = [],
6566
} = { ...globalConfig, ...renderOptions };
6667

67-
dtlConfigure({
68-
eventWrapper: (cb) => {
69-
const result = cb();
70-
detectChangesForMountedFixtures();
71-
return result;
72-
},
73-
...domConfig,
74-
});
68+
dtlConfigure(domConfig);
7569

7670
TestBed.configureTestingModule({
7771
declarations: addAutoDeclarations(sut, {
@@ -193,7 +187,6 @@ export async function render<SutType, WrapperType = SutType>(
193187
result = doNavigate();
194188
}
195189

196-
detectChanges();
197190
return result ?? false;
198191
};
199192

@@ -223,7 +216,6 @@ export async function render<SutType, WrapperType = SutType>(
223216
}
224217

225218
fixture = await createComponent(componentContainer);
226-
227219
setComponentProperties(fixture, properties);
228220
setComponentInputs(fixture, inputs);
229221
setComponentOutputs(fixture, outputs);
@@ -245,6 +237,10 @@ export async function render<SutType, WrapperType = SutType>(
245237
fixture.componentInstance.ngOnChanges(changes);
246238
}
247239

240+
if (autoDetectChanges) {
241+
fixture.autoDetectChanges(true);
242+
}
243+
248244
detectChanges = () => {
249245
if (isAlive) {
250246
fixture.detectChanges();
@@ -395,8 +391,6 @@ async function waitForWrapper<T>(
395391
inFakeAsync = false;
396392
}
397393

398-
detectChanges();
399-
400394
return await dtlWaitFor(() => {
401395
setTimeout(() => detectChanges(), 0);
402396
if (inFakeAsync) {

Diff for: projects/testing-library/tests/fire-event.spec.ts

+41-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,48 @@
11
import { Component } from '@angular/core';
22
import { render, fireEvent, screen } from '../src/public_api';
3+
import { FormsModule } from '@angular/forms';
34

4-
@Component({
5-
selector: 'atl-fixture',
6-
template: ` <input type="text" data-testid="input" /> `,
7-
})
8-
class FixtureComponent {}
5+
describe('fireEvent', () => {
6+
@Component({
7+
selector: 'atl-fixture',
8+
template: ` <input type="text" data-testid="input" [(ngModel)]="name" name="name" />
9+
<div>Hello {{ name }}</div>`,
10+
})
11+
class FixtureComponent {
12+
name = '';
13+
}
914

10-
test('does not call detect changes when fixture is destroyed', async () => {
11-
const { fixture } = await render(FixtureComponent);
15+
it('automatically detect changes when event is fired', async () => {
16+
await render(FixtureComponent, {
17+
imports: [FormsModule],
18+
});
1219

13-
fixture.destroy();
20+
fireEvent.input(screen.getByTestId('input'), { target: { value: 'Tim' } });
1421

15-
// should otherwise throw
16-
fireEvent.input(screen.getByTestId('input'), { target: { value: 'Bonjour' } });
22+
expect(screen.getByText('Hello Tim')).toBeInTheDocument();
23+
});
24+
25+
it('can disable automatic detect changes when event is fired', async () => {
26+
const { detectChanges } = await render(FixtureComponent, {
27+
imports: [FormsModule],
28+
autoDetectChanges: false,
29+
});
30+
31+
fireEvent.input(screen.getByTestId('input'), { target: { value: 'Tim' } });
32+
33+
expect(screen.queryByText('Hello Tim')).not.toBeInTheDocument();
34+
35+
detectChanges();
36+
37+
expect(screen.getByText('Hello Tim')).toBeInTheDocument();
38+
});
39+
40+
it('does not call detect changes when fixture is destroyed', async () => {
41+
const { fixture } = await render(FixtureComponent);
42+
43+
fixture.destroy();
44+
45+
// should otherwise throw
46+
fireEvent.input(screen.getByTestId('input'), { target: { value: 'Bonjour' } });
47+
});
1748
});

0 commit comments

Comments
 (0)