Skip to content

Commit 9f2981e

Browse files
committed
address comments
1 parent 4dc6dc6 commit 9f2981e

File tree

6 files changed

+73
-73
lines changed

6 files changed

+73
-73
lines changed

src/cdk/testing/change-detection.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,50 @@
88

99
import {BehaviorSubject, Subscription} from 'rxjs';
1010

11-
/** Represents the status of change detection batching. */
12-
export interface ChangeDetectionBatchingStatus {
13-
/** Whether change detection is batching. */
14-
isBatching: boolean;
11+
/** Represents the status of auto change detection. */
12+
export interface AutoChangeDetectionStatus {
13+
/** Whether auto change detection is disabled. */
14+
isDisabled: boolean;
1515
/**
1616
* An optional callback, if present it indicates that change detection should be run immediately,
17-
* while handling the batching status change. The callback should then be called as soon as change
17+
* while handling the status change. The callback should then be called as soon as change
1818
* detection is done.
1919
*/
2020
onDetectChangesNow?: () => void;
2121
}
2222

23-
/** Subject used to dispatch and listen for changes to the change detection batching status . */
24-
const batchChangeDetectionSubject = new BehaviorSubject<ChangeDetectionBatchingStatus>({
25-
isBatching: false
23+
/** Subject used to dispatch and listen for changes to the auto change detection status . */
24+
const autoChangeDetectionSubject = new BehaviorSubject<AutoChangeDetectionStatus>({
25+
isDisabled: false
2626
});
2727

28-
/** The current subscription to `batchChangeDetectionSubject`. */
29-
let batchChangeDetectionSubscription: Subscription | null;
28+
/** The current subscription to `autoChangeDetectionSubject`. */
29+
let autoChangeDetectionSubscription: Subscription | null;
3030

3131
/**
32-
* The default handler for change detection batching status changes. This handler will be used if
33-
* the specific environment does not install its own.
34-
* @param status The new change detection batching status.
32+
* The default handler for auto change detection status changes. This handler will be used if the
33+
* specific environment does not install its own.
34+
* @param status The new auto change detection status.
3535
*/
36-
function defaultBatchChangeDetectionHandler(status: ChangeDetectionBatchingStatus) {
36+
function defaultAutoChangeDetectionHandler(status: AutoChangeDetectionStatus) {
3737
status.onDetectChangesNow?.();
3838
}
3939

4040
/**
41-
* Allows a test `HarnessEnvironment` to install its own handler for change detection batching
42-
* status changes.
43-
* @param handler The handler for the change detection batching status.
41+
* Allows a test `HarnessEnvironment` to install its own handler for auto change detection status
42+
* changes.
43+
* @param handler The handler for the auto change detection status.
4444
*/
45-
export function handleChangeDetectionBatching(
46-
handler: (status: ChangeDetectionBatchingStatus) => void) {
47-
stopHandlingChangeDetectionBatching();
48-
batchChangeDetectionSubscription = batchChangeDetectionSubject.subscribe(handler);
45+
export function handleAutoChangeDetectionStatus(
46+
handler: (status: AutoChangeDetectionStatus) => void) {
47+
stopHandlingAutoChangeDetectionStatus();
48+
autoChangeDetectionSubscription = autoChangeDetectionSubject.subscribe(handler);
4949
}
5050

51-
/** Allows a `HarnessEnvironment` to stop handling change detection batching status changes. */
52-
export function stopHandlingChangeDetectionBatching() {
53-
batchChangeDetectionSubscription?.unsubscribe();
54-
batchChangeDetectionSubscription = null;
51+
/** Allows a `HarnessEnvironment` to stop handling auto change detection status changes. */
52+
export function stopHandlingAutoChangeDetectionStatus() {
53+
autoChangeDetectionSubscription?.unsubscribe();
54+
autoChangeDetectionSubscription = null;
5555
}
5656

5757
/**
@@ -63,30 +63,30 @@ export function stopHandlingChangeDetectionBatching() {
6363
*/
6464
async function batchChangeDetection<T>(fn: () => Promise<T>, triggerBeforeAndAfter: boolean) {
6565
// If change detection batching is already in progress, just run the function.
66-
if (batchChangeDetectionSubject.getValue().isBatching) {
66+
if (autoChangeDetectionSubject.getValue().isDisabled) {
6767
return await fn();
6868
}
6969

7070
// If nothing is handling change detection batching, install the default handler.
71-
if (!batchChangeDetectionSubscription) {
72-
batchChangeDetectionSubject.subscribe(defaultBatchChangeDetectionHandler);
71+
if (!autoChangeDetectionSubscription) {
72+
autoChangeDetectionSubject.subscribe(defaultAutoChangeDetectionHandler);
7373
}
7474

7575
if (triggerBeforeAndAfter) {
76-
await new Promise(resolve => batchChangeDetectionSubject.next({
77-
isBatching: true,
76+
await new Promise(resolve => autoChangeDetectionSubject.next({
77+
isDisabled: true,
7878
onDetectChangesNow: resolve,
7979
}));
8080
const result = await fn();
81-
await new Promise(resolve => batchChangeDetectionSubject.next({
82-
isBatching: false,
81+
await new Promise(resolve => autoChangeDetectionSubject.next({
82+
isDisabled: false,
8383
onDetectChangesNow: resolve,
8484
}));
8585
return result;
8686
} else {
87-
batchChangeDetectionSubject.next({isBatching: true});
87+
autoChangeDetectionSubject.next({isDisabled: true});
8888
const result = await fn();
89-
batchChangeDetectionSubject.next({isBatching: false});
89+
autoChangeDetectionSubject.next({isDisabled: false});
9090
return result;
9191
}
9292
}

src/cdk/testing/component-harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ export class HarnessPredicate<T extends ComponentHarness> {
501501
* and resolves to false otherwise.
502502
*/
503503
async evaluate(harness: T): Promise<boolean> {
504-
const results = await parallel((this._predicates.map(p => p(harness))));
504+
const results = await parallel(this._predicates.map(p => p(harness)));
505505
return results.reduce((combined, current) => combined && current, true);
506506
}
507507

src/cdk/testing/test-harnesses.md

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ created manually.
147147

148148
#### Change detection
149149
By default, test harnesses will run Angular's change detection before reading the state of a DOM
150-
element and after interacting with a DOM element. While this is convenient in most cases, there may
151-
be times that you need finer-grained control over change detection. (e.g. to check the state of
152-
something while an async operation is in progress). In these cases you can use the
150+
element and after interacting with a DOM element. While convenient in most cases, there may be times
151+
that you need finer-grained control over change detection. For example, you may want to check the
152+
state of a component while an async operation is pending. In these cases you can use the
153153
`manualChangeDetection` function to disable automatic handling of change detection for a block of
154154
code. For example:
155155

@@ -177,11 +177,12 @@ therefore, the Angular team recommends using
177177
to improve the test readability.
178178

179179
Note that `await` statements block the execution of your test until the associated `Promise`
180-
resolves. There are often times when you want to perform multiple actions simultaneously and wait
181-
until they're all done rather than blocking. For example, reading multiple properties off a harness.
182-
In these situations use the `parallel` function to parallelize the operations. The parallel function
183-
works similarly to `Promise.all`, while also optimizing change detection, so it is not run an
184-
excessive number of times. For example:
180+
resolves. Occasionally, you may want to perform multiple actions simultaneously and wait until
181+
they're all done rather than performing each action sequentially. For example, reading multiple
182+
properties off a single component. In these situations use the `parallel` function to parallelize
183+
the operations. The parallel function works similarly to `Promise.all`, while also optimizing change
184+
detection, so it is not run an excessive number of times. The following code demonstrates how you
185+
can read multiple properties from a harness with `parallel`:
185186

186187
```ts
187188
it('reads properties in parallel', async () => {
@@ -445,7 +446,7 @@ class MyMenuHarness extends ComponentHarness {
445446
protected getPopupHarness = this.locatorFor(MyPopupHarness);
446447

447448
/** Gets the text of the menu trigger. */
448-
getTriggerText(): Promise<string> {
449+
async getTriggerText(): Promise<string> {
449450
const popupHarness = await this.getPopupHarness();
450451
return popupHarness.getTriggerText();
451452
}
@@ -653,19 +654,19 @@ and
653654
[`ProtractorHarnessEnvironment`](https://github.com/angular/components/blob/master/src/cdk/testing/protractor/protractor-harness-environment.ts#L16)
654655
implementations in Angular CDK serve as good examples of implementations of this interface.
655656

656-
#### Handling change detection batching
657+
#### Handling auto change detection status
657658
In order to support the `manualChangeDetection` and `parallel` APIs, your environment should install
658-
a handler for change detection batching.
659+
a handler for the auto change detection status.
659660

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:
661+
When your environment wants to start handling the auto change detection status it can call
662+
`handleAutoChangeDetectionStatus(handler)`. The handler function will receive a
663+
`AutoChangeDetectionStatus` which has two properties:
663664

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.
665+
* `isDisabled: boolean` - Indicates whether auto change detection is currently disabled. When true,
666+
your environment's `forceStabilize` method should act as a no-op. This allows users to trigger
667+
change detection manually instead.
667668
* `onDetectChangesNow?: () => void` - If this optional callback is specified, your environment
668669
should trigger change detection immediately and call the callback when change detection finishes.
669670

670-
If your environment wants to stop handling change detection batching it can call
671-
`stopHandlingChangeDetectionBatching()`.
671+
If your environment wants to stop handling auto change detection status it can call
672+
`stopHandlingAutoChangeDetectionStatus()`.

src/cdk/testing/testbed/testbed-harness-environment.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import {
1010
ComponentHarness,
1111
ComponentHarnessConstructor,
12-
handleChangeDetectionBatching,
12+
handleAutoChangeDetectionStatus,
1313
HarnessEnvironment,
1414
HarnessLoader,
15-
stopHandlingChangeDetectionBatching,
15+
stopHandlingAutoChangeDetectionStatus,
1616
TestElement
1717
} from '@angular/cdk/testing';
1818
import {ComponentFixture, flush} from '@angular/core/testing';
@@ -44,10 +44,10 @@ const activeFixtures = new Set<ComponentFixture<unknown>>();
4444
* Installs a handler for change detection batching status changes for a specific fixture.
4545
* @param fixture The fixture to handle change detection batching for.
4646
*/
47-
function installChangeDetectionBatchingHandler(fixture: ComponentFixture<unknown>) {
47+
function installAutoChangeDetectionStatusHandler(fixture: ComponentFixture<unknown>) {
4848
if (!activeFixtures.size) {
49-
handleChangeDetectionBatching(({isBatching, onDetectChangesNow}) => {
50-
disableAutoChangeDetection = isBatching;
49+
handleAutoChangeDetectionStatus(({isDisabled, onDetectChangesNow}) => {
50+
disableAutoChangeDetection = isDisabled;
5151
if (onDetectChangesNow) {
5252
Promise.all(Array.from(activeFixtures).map(detectChanges)).then(onDetectChangesNow);
5353
}
@@ -60,10 +60,10 @@ function installChangeDetectionBatchingHandler(fixture: ComponentFixture<unknown
6060
* Uninstalls a handler for change detection batching status changes for a specific fixture.
6161
* @param fixture The fixture to stop handling change detection batching for.
6262
*/
63-
function uninstallChangeDetectionBatchingHandler(fixture: ComponentFixture<unknown>) {
63+
function uninstallAutoChangeDetectionStatusHandler(fixture: ComponentFixture<unknown>) {
6464
activeFixtures.delete(fixture);
6565
if (!activeFixtures.size) {
66-
stopHandlingChangeDetectionBatching();
66+
stopHandlingAutoChangeDetectionStatus();
6767
}
6868
}
6969

@@ -92,9 +92,9 @@ export class TestbedHarnessEnvironment extends HarnessEnvironment<Element> {
9292
super(rawRootElement);
9393
this._options = {...defaultEnvironmentOptions, ...options};
9494
this._taskState = TaskStateZoneInterceptor.setup();
95-
installChangeDetectionBatchingHandler(_fixture);
95+
installAutoChangeDetectionStatusHandler(_fixture);
9696
_fixture.componentRef.onDestroy(() => {
97-
uninstallChangeDetectionBatchingHandler(_fixture);
97+
uninstallAutoChangeDetectionStatusHandler(_fixture);
9898
this._destroyed = true;
9999
});
100100
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,8 @@ describe('TestbedHarnessEnvironment', () => {
120120
const harness =
121121
await TestbedHarnessEnvironment.harnessForFixture(fixture, MainComponentHarness);
122122
detectChangesSpy.calls.reset();
123-
await manualChangeDetection(async () => {
124-
await parallel(Array.from({length: 5}, () => harness.button().then(b => b.click())));
125-
});
123+
await manualChangeDetection(() => parallel(
124+
Array.from({length: 5}, () => harness.button().then(b => b.click()))));
126125
expect(detectChangesSpy).toHaveBeenCalledTimes(0);
127126
});
128127
});

tools/public_api_guard/cdk/testing.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ export declare type AsyncOptionPredicate<T, O> = (item: T, option: O) => Promise
66

77
export declare type AsyncPredicate<T> = (item: T) => Promise<boolean>;
88

9+
export interface AutoChangeDetectionStatus {
10+
isDisabled: boolean;
11+
onDetectChangesNow?: () => void;
12+
}
13+
914
export interface BaseHarnessFilters {
1015
ancestor?: string;
1116
selector?: string;
1217
}
1318

14-
export interface ChangeDetectionBatchingStatus {
15-
isBatching: boolean;
16-
onDetectChangesNow?: () => void;
17-
}
18-
1919
export declare abstract class ComponentHarness {
2020
protected readonly locatorFactory: LocatorFactory;
2121
constructor(locatorFactory: LocatorFactory);
@@ -47,7 +47,7 @@ export interface ElementDimensions {
4747
width: number;
4848
}
4949

50-
export declare function handleChangeDetectionBatching(handler: (status: ChangeDetectionBatchingStatus) => void): void;
50+
export declare function handleAutoChangeDetectionStatus(handler: (status: AutoChangeDetectionStatus) => void): void;
5151

5252
export declare abstract class HarnessEnvironment<E> implements HarnessLoader, LocatorFactory {
5353
protected rawRootElement: E;
@@ -126,7 +126,7 @@ export interface ModifierKeys {
126126

127127
export declare function parallel<T>(values: Iterable<T | PromiseLike<T>>): Promise<T[]>;
128128

129-
export declare function stopHandlingChangeDetectionBatching(): void;
129+
export declare function stopHandlingAutoChangeDetectionStatus(): void;
130130

131131
export interface TestElement {
132132
blur(): Promise<void>;

0 commit comments

Comments
 (0)