Skip to content

Commit 2a93195

Browse files
committed
address feedback
1 parent bfd40e8 commit 2a93195

File tree

5 files changed

+27
-14
lines changed

5 files changed

+27
-14
lines changed

src/cdk/testing/change-detection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async function batchChangeDetection<T>(fn: () => Promise<T>, triggerBeforeAndAft
9696
* @param fn The function to disable auto change detection for.
9797
* @return The result of the given function.
9898
*/
99-
export async function noAutoChangeDetection<T>(fn: () => Promise<T>) {
99+
export async function manualChangeDetection<T>(fn: () => Promise<T>) {
100100
return batchChangeDetection(fn, false);
101101
}
102102

src/cdk/testing/harness-environment.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,6 @@ export abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFac
166166
const skipSelectorCheck = (elementQueries.length === 0 && harnessTypes.size === 1) ||
167167
harnessQueries.length === 0;
168168

169-
// We want to batch change detection while we're filtering harnesses, because harness predicates
170-
// may trigger change detection by reading state from DOM elements. If not batched these change
171-
// detections would be triggered once per potential match element which could cause significant
172-
// slowdown.
173169
const perElementMatches = await parallel(rawElements.map(async rawElement => {
174170
const testElement = this.createTestElement(rawElement);
175171
const allResultsForElement = await Promise.all(

src/cdk/testing/test-harnesses.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,13 @@ By default, test harnesses will run Angular's change detection before reading th
150150
element and after interacting with a DOM element. While this is convenient in most cases, there may
151151
be times that you need finer-grained control over change detection. (e.g. to check the state of
152152
something while an async operation is in progress). In these cases you can use the
153-
`noAutoChangeDetection` function to disable automatic handling of change detection for a block of
153+
`manualChangeDetection` function to disable automatic handling of change detection for a block of
154154
code. For example:
155155

156156
```ts
157157
it('checks state while async action is in progress', async () => {
158158
const buttonHarness = loader.getHarness(MyButtonHarness);
159-
await noAutoChangeDetection(async () => {
159+
await manualChangeDetection(async () => {
160160
await buttonHarness.click();
161161
fixture.detectChanges();
162162
// Check expectations while async click operation is in progress.
@@ -652,3 +652,20 @@ The
652652
and
653653
[`ProtractorHarnessEnvironment`](https://github.com/angular/components/blob/master/src/cdk/testing/protractor/protractor-harness-environment.ts#L16)
654654
implementations in Angular CDK serve as good examples of implementations of this interface.
655+
656+
#### Handling change detection batching
657+
In order to support the `manualChangeDetection` and `parallel` APIs, your environment should install
658+
a handler for change detection batching.
659+
660+
When your environment wants to start handling change detection batching it can call
661+
`handleChangeDetectionBatching(handler)`. The handler function will receive a
662+
`ChangeDetectionBatchingStatus` which has two properties:
663+
664+
* `isBatching: boolean` - Indicates whether change detection is batching. When change detection is
665+
batching, your environment's `forceStabilize` method should act as a no-op. This allows users to
666+
trigger change detection manually instead.
667+
* `onDetectChangesNow?: () => void` - If this optional callback is specified, your environment
668+
should trigger change detection immediately and call the callback when change detection finishes.
669+
670+
If your environment wants to stop handling change detection batching it can call
671+
`stopHandlingChangeDetectionBatching()`.

src/cdk/testing/tests/testbed.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {_supportsShadowDom} from '@angular/cdk/platform';
22
import {
3-
HarnessLoader, noAutoChangeDetection, parallel,
3+
HarnessLoader, manualChangeDetection, parallel,
44
} from '@angular/cdk/testing';
55
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
66
import {waitForAsync, ComponentFixture, fakeAsync, TestBed} from '@angular/core/testing';
@@ -81,12 +81,12 @@ describe('TestbedHarnessEnvironment', () => {
8181
});
8282

8383
describe('change detection behavior', () => {
84-
it('noAutoChangeDetection should prevent auto change detection', async () => {
84+
it('manualChangeDetection should prevent auto change detection', async () => {
8585
const detectChangesSpy = spyOn(fixture, 'detectChanges').and.callThrough();
8686
const harness =
8787
await TestbedHarnessEnvironment.harnessForFixture(fixture, MainComponentHarness);
8888
detectChangesSpy.calls.reset();
89-
await noAutoChangeDetection(async () => {
89+
await manualChangeDetection(async () => {
9090
const button = await harness.button();
9191
await button.text();
9292
await button.click();
@@ -115,12 +115,12 @@ describe('TestbedHarnessEnvironment', () => {
115115
expect(detectChangesSpy).toHaveBeenCalledTimes(2);
116116
});
117117

118-
it('parallel inside noAutoChangeDetection should not cause change detection', async () => {
118+
it('parallel inside manualChangeDetection should not cause change detection', async () => {
119119
const detectChangesSpy = spyOn(fixture, 'detectChanges').and.callThrough();
120120
const harness =
121121
await TestbedHarnessEnvironment.harnessForFixture(fixture, MainComponentHarness);
122122
detectChangesSpy.calls.reset();
123-
await noAutoChangeDetection(async () => {
123+
await manualChangeDetection(async () => {
124124
await parallel(Array.from({length: 5}, () => harness.button().then(b => b.click())));
125125
});
126126
expect(detectChangesSpy).toHaveBeenCalledTimes(0);

tools/public_api_guard/cdk/testing.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,15 @@ export declare type LocatorFnResult<T extends (HarnessQuery<any> | string)[]> =
115115
} ? C : T[I] extends string ? TestElement : never;
116116
}[number];
117117

118+
export declare function manualChangeDetection<T>(fn: () => Promise<T>): Promise<T>;
119+
118120
export interface ModifierKeys {
119121
alt?: boolean;
120122
control?: boolean;
121123
meta?: boolean;
122124
shift?: boolean;
123125
}
124126

125-
export declare function noAutoChangeDetection<T>(fn: () => Promise<T>): Promise<T>;
126-
127127
export declare function parallel<T>(values: Iterable<T | PromiseLike<T>>): Promise<T[]>;
128128

129129
export declare function stopHandlingChangeDetectionBatching(): void;

0 commit comments

Comments
 (0)