Skip to content

Commit dbdbfb4

Browse files
committed
fix parallel to take a function
1 parent 9f2981e commit dbdbfb4

File tree

6 files changed

+20
-18
lines changed

6 files changed

+20
-18
lines changed

src/cdk/testing/change-detection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ export async function manualChangeDetection<T>(fn: () => Promise<T>) {
104104
* Resolves the given list of async values in parallel (i.e. via Promise.all) while batching change
105105
* detection over the entire operation such that change detection occurs exactly once before
106106
* resolving the values and once after.
107-
* @param values The async values to resolve in parallel with batched change detection.
107+
* @param values A getter for the async values to resolve in parallel with batched change detection.
108108
* @return The resolved values.
109109
*/
110-
export async function parallel<T>(values: Iterable<T | PromiseLike<T>>) {
111-
return batchChangeDetection(() => Promise.all(values), true);
110+
export async function parallel<T>(values: () => Iterable<T | PromiseLike<T>>) {
111+
return batchChangeDetection(() => Promise.all(values()), true);
112112
}

src/cdk/testing/component-harness.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ export class HarnessPredicate<T extends ComponentHarness> {
490490
if (harnesses.length === 0) {
491491
return [];
492492
}
493-
const results = await parallel(harnesses.map(h => this.evaluate(h)));
493+
const results = await parallel(() => harnesses.map(h => this.evaluate(h)));
494494
return harnesses.filter((_, i) => results[i]);
495495
}
496496

@@ -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/harness-environment.ts

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

169-
const perElementMatches = await parallel(rawElements.map(async rawElement => {
169+
const perElementMatches = await parallel(() => rawElements.map(async rawElement => {
170170
const testElement = this.createTestElement(rawElement);
171171
const allResultsForElement = await Promise.all(
172172
// For each query, get `null` if it doesn't match, or a `TestElement` or

src/cdk/testing/test-harnesses.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ can read multiple properties from a harness with `parallel`:
188188
it('reads properties in parallel', async () => {
189189
const checkboxHarness = loader.getHarness(MyCheckboxHarness);
190190
// Read the checked and intermediate properties simultaneously.
191-
const [checked, indeterminate] = await parallel([
191+
const [checked, indeterminate] = await parallel(() => [
192192
checkboxHarness.isChecked(),
193193
checkboxHarness.isIndeterminate()
194194
]);

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,21 @@ describe('TestbedHarnessEnvironment', () => {
9999
const harness =
100100
await TestbedHarnessEnvironment.harnessForFixture(fixture, MainComponentHarness);
101101

102-
// Chain together our promises to ensure the before clause runs first and the after clause
103-
// runs last.
104-
const before =
105-
Promise.resolve().then(() => expect(detectChangesSpy).toHaveBeenCalledTimes(1));
106-
const actions = before.then(() => Promise.all(Array.from({length: 5},
107-
() => harness.button().then(b => b.click()))));
108-
const after = actions.then(() => expect(detectChangesSpy).toHaveBeenCalledTimes(1));
109-
110102
// Run them in "parallel" (though the order is guaranteed because of how we constructed the
111103
// promises.
112104
detectChangesSpy.calls.reset();
113105
expect(detectChangesSpy).toHaveBeenCalledTimes(0);
114-
await parallel<unknown>([before, actions, after]);
106+
await parallel<unknown>(() => {
107+
// Chain together our promises to ensure the before clause runs first and the after clause
108+
// runs last.
109+
const before =
110+
Promise.resolve().then(() => expect(detectChangesSpy).toHaveBeenCalledTimes(1));
111+
const actions = before.then(() => Promise.all(Array.from({length: 5},
112+
() => harness.button().then(b => b.click()))));
113+
const after = actions.then(() => expect(detectChangesSpy).toHaveBeenCalledTimes(1));
114+
115+
return [before, actions, after];
116+
});
115117
expect(detectChangesSpy).toHaveBeenCalledTimes(2);
116118
});
117119

@@ -121,7 +123,7 @@ describe('TestbedHarnessEnvironment', () => {
121123
await TestbedHarnessEnvironment.harnessForFixture(fixture, MainComponentHarness);
122124
detectChangesSpy.calls.reset();
123125
await manualChangeDetection(() => parallel(
124-
Array.from({length: 5}, () => harness.button().then(b => b.click()))));
126+
() => Array.from({length: 5}, () => harness.button().then(b => b.click()))));
125127
expect(detectChangesSpy).toHaveBeenCalledTimes(0);
126128
});
127129
});

tools/public_api_guard/cdk/testing.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export interface ModifierKeys {
124124
shift?: boolean;
125125
}
126126

127-
export declare function parallel<T>(values: Iterable<T | PromiseLike<T>>): Promise<T[]>;
127+
export declare function parallel<T>(values: () => Iterable<T | PromiseLike<T>>): Promise<T[]>;
128128

129129
export declare function stopHandlingAutoChangeDetectionStatus(): void;
130130

0 commit comments

Comments
 (0)