Skip to content
This repository was archived by the owner on Sep 16, 2022. It is now read-only.

Commit 828cfe8

Browse files
matanlureyalorenzen
authored andcommitted
refactor(Test): Change a bit of the timing in NgTestBed.
This prepares for the new stabilizers, without hopefully being a breaking change. * Simplifies what `bootstrapForTest` does with beforeComponentCreated, moving most of the logic (running this callback within the correct zone) as the responsibility of NgTestBed itself. * This allows us to create the root stabilizer before calling beforeComponentCreated, which in turn lets us use the stabilizer to await for async side-effects created in beforeComponentCreated. PiperOrigin-RevId: 221675112
1 parent 0df7c39 commit 828cfe8

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

angular_test/lib/src/bootstrap.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,9 @@ Future<ComponentRef<E>> bootstrapForTest<E>(
6262
});
6363

6464
if (beforeComponentCreated != null) {
65-
var completer = Completer<void>();
66-
ngZone.runGuarded(() => new Future(() {})
67-
.then((_) => beforeComponentCreated(injector))
68-
.then((_) => completer.complete(), onError: completer.completeError));
69-
await completer.future;
65+
await beforeComponentCreated(injector);
7066
}
67+
7168
// Code works improperly when .run is typed to return FutureOr:
7269
// https://github.com/dart-lang/sdk/issues/32285.
7370
return appRef.run<ComponentRef<E>>(() {

angular_test/lib/src/frontend/bed.dart

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,25 +316,51 @@ class NgTestBed<T> {
316316

317317
// Create a zone to intercept timer creation.
318318
final timerHookZone = TimerHookZone();
319+
NgZone ngZoneInstance;
319320
NgZone ngZoneFactory() {
320-
return timerHookZone.run(() => NgZone(enableLongStackTrace: true));
321+
return timerHookZone.run(() {
322+
return ngZoneInstance = NgZone(enableLongStackTrace: true);
323+
});
324+
}
325+
326+
// Created within "createStabilizersAndRunUserHook".
327+
NgTestStabilizer allStabilizers;
328+
329+
Future<void> createStabilizersAndRunUserHook(Injector injector) async {
330+
// Some internal stabilizers get access to the TimerHookZone.
331+
// Most (i.e. user-land) stabilizers do not.
332+
final createStabilizer = _createStabilizer;
333+
allStabilizers = createStabilizer is AllowTimerHookZoneAccess
334+
? createStabilizer(injector, timerHookZone)
335+
: createStabilizer(injector);
336+
337+
// If there is no user hook, we are done.
338+
if (beforeComponentCreated == null) {
339+
return null;
340+
}
341+
342+
// If there is a user hook, execute it within the ngZone:
343+
final completer = Completer<void>();
344+
ngZoneInstance.runGuarded(() async {
345+
try {
346+
await beforeComponentCreated(injector);
347+
completer.complete();
348+
} catch (e, s) {
349+
completer.completeError(e, s);
350+
}
351+
});
352+
return completer.future.whenComplete(() => allStabilizers.update());
321353
}
322354

323355
return bootstrapForTest<T>(
324356
_componentFactory ?? typeToFactory(type),
325357
_host ?? _defaultHost(),
326358
_createRootInjectorFactory(),
327-
beforeComponentCreated: beforeComponentCreated,
359+
beforeComponentCreated: createStabilizersAndRunUserHook,
328360
beforeChangeDetection: beforeChangeDetection,
329361
createNgZone: ngZoneFactory,
330362
).then((componentRef) async {
331363
_checkForActiveTest();
332-
// Some internal stabilizers get access to the TimerHookZone.
333-
// Most (i.e. user-land) stabilizers do not.
334-
final allStabilizers = _createStabilizer is NgTestStabilizerFactory
335-
? _createStabilizer(componentRef.injector)
336-
: (_createStabilizer as AllowTimerHookZoneAccess)(
337-
componentRef.injector, timerHookZone);
338364
await allStabilizers.stabilize();
339365
final testFixture = NgTestFixture(
340366
componentRef.injector.get(ApplicationRef),

0 commit comments

Comments
 (0)