# Ui.HoldDelays

> 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.

<!-- id: testing-ui-hold-delays · area: testing · stability: stable · html: https://osysharp.com/reference/testing/ui-hold-delays/ -->

## Summary        {#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      {#signature}
```osy syntax
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    {#description}

### Why a test needs it   {#why}
`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   {#what-is-held}
| 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](https://osysharp.com/reference/testing/ui-tick/)) and the time the page reads (move it with [TestClock.Advance and TestClock.Set](https://osysharp.com/reference/testing/clock-advance/)) |

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   {#verbs-while-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   {#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](https://osysharp.com/reference/testing/clock-advance/). The two compose.

## Examples       {#examples}

```osy title="a button that says it is working while its action waits" test app=testing-ui-hold-delays
[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       {#see-also}
- [Ui.HoldServer](https://osysharp.com/reference/testing/ui-hold-server/) — the same idea for a slow server: its answers wait until released
- [Pending](https://osysharp.com/reference/ui/pending/) — an action's busy state, which a held delay keeps on
- [Wait in place (Task.Delay)](https://osysharp.com/reference/function/task-delay/) — `await Task.Delay(…)` itself
- [Ui.Tick](https://osysharp.com/reference/testing/ui-tick/) — make a poll happen when the test says so
- [TestClock.Advance and TestClock.Set](https://osysharp.com/reference/testing/clock-advance/) — move the time the page and the server read
