Osy#betaRun it on Osyrin: osyrin.com

Reference / Testing

Ui.HoldDelays

Ui.HoldDelays(); … Ui.ReleaseDelays();

In a UI test, keeps every `Task.Delay` the page starts open until `Ui.ReleaseDelays()`, so the test can stand on the screen a slow action leaves a person on (its busy button, its `.Pending`, a dialog that cannot be cancelled) and then prove how the action finishes.

stable1 example compiled by CItestinguitime

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 them

Description#

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 releasedNever held
a Task.Delay that a component's action, on mount or event handler starts in the browser after the holda 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.Click returns 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.Visit until Ui.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#

Related

Ui.HoldServer

In a UI test, keeps the server's answers to the page's reads back until `Ui.ReleaseServer()`, so the test can stand on…

Pending

When a control's action waits on the server, the platform shows a busy spinner and disables the control — but only…

Wait in place (Task.Delay)

Wait before carrying on — between checks on a job another system is still running, before trying a call again, or for a…

Ui — drive the app's UI from a test

Drive the real UI from a test: navigate to a route, click what a person would click, and assert on what the screen…

Ui.Tick

In a UI test, fires every `on every` clock armed on the screen once, now, and waits for their bodies — server calls…

TestClock.Advance and TestClock.Set

In a test, moves the run's clock FORWARD by a duration, then lets the next Workflow.Settle fire every workflow timer…