Summary#
Ui.HoldDelays() makes the page's delays wait. From then on, an action that reaches await Task.Delay(…) stops
there and stays pending until the test says Ui.ReleaseDelays(), however short the delay is. In between, every
assertion reads the page as a person sees it while the action runs. The release then ends those delays at once, as if
their time had run out, and waits for the actions to finish.
Signature#
Ui.HoldDelays(); // from now on, a Task.Delay the page starts waits for the release
Ui.ReleaseDelays(); // end every held delay now, and wait for the actions waiting on themDescription#
Why a test needs it#
Ui.Click waits for the action it started to finish, which is what makes the next assertion trustworthy. So an
action that awaits a delay has already finished when that assertion runs: its busy words are gone and .Pending is
false again. A longer delay does not help. It only makes the click wait longer. Holding the delays turns the moment
in between into a screen the test can stand on, with no race against a timer.
What is held, and what is not#
| Held until released | Never held |
|---|---|
a Task.Delay that a component's action, on mount or event handler starts in the browser after the hold | a delay that was already running when the hold began: it ends on its own timer |
| a delay the page started on another visit, while the hold is on (it is sticky) | a server function's Task.Delay, which runs on the server and is not the page's to hold |
an on every clock (fire it with Ui.Tick) and the time the page reads (move it with TestClock.Advance and TestClock.Set) |
A held delay with a CancellationToken still ends when its token is cancelled, as it would in production.
How the other verbs behave while it is held#
Ui.Clickreturns once every running action is waiting on a held delay and nothing else is outstanding (no request on the wire, no list still to draw). An action that is also waiting on the server is waited for until only the delay is left.- An assertion does not wait out its bound for a held delay.
Assert.Hidden("Saved")answers at once, because nothing will save until the release. - It is sticky. It stays on across
Ui.VisituntilUi.ReleaseDelays(), and it may be said before the first visit.
Releasing#
Ui.ReleaseDelays() ends every held delay and stops holding. It then waits as after a click: the actions run on from
their delays, their writes land and the page shows the result. A delay that an action starts after the release runs
on its timer again. Releasing with nothing held is not an error.
⚑ It does not move time. To make a status line that appears "after a second" show while the action is held, move the clock with TestClock.Advance and TestClock.Set. The two compose.
Examples#
[Route("/kiln")]
[AllowAnonymous]
[Render(CSR)]
component Kiln() {
int fired = 0;
action Fire() {
await Task.Delay(TimeSpan.FromSeconds(3)); // the kiln takes its time
fired = fired + 1;
}
render {
Stack(gap: 3) {
Button("Fire", onPress: Fire, busyLabel: "Firing…");
Text($"Fired {fired} times");
}
}
}
[Test]
void the_button_says_it_is_working_until_the_kiln_is_done() {
Ui.Visit("/kiln");
Ui.HoldDelays();
Ui.Click("Fire");
Assert.Visible("Firing…");
Assert.Disabled("Fire");
Assert.Visible("Fired 0 times");
Ui.ReleaseDelays();
Assert.Visible("Fired 1 times");
Assert.Hidden("Firing…");
Assert.Enabled("Fire");
}See also#
- Ui.HoldServer — the same idea for a slow server: its answers wait until released
- Pending — an action's busy state, which a held delay keeps on
- Wait in place (Task.Delay) —
await Task.Delay(…)itself - Ui.Tick — make a poll happen when the test says so
- TestClock.Advance and TestClock.Set — move the time the page and the server read