> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Device and host fakes

Send the app to the background, take the phone offline, or press Android's back button in a unit test. `import { setAppState, setOnline, pressBackButton } from '@blotch/angular-lynx-testing-library';`

## App state

Fakes what [`LynxAppState`](/api/angular-lynx/device/app-state.md) reports. Each test starts `'active'`.

### `setAppState()`

```typescript
function setAppState(state: 'active' | 'inactive' | 'background'): void;
```

Sends the app to the background, or brings it back, as the person does by leaving the app and returning. A spec can then check what the app does on its return.

```typescript
await render(Schedule);

setAppState('background');
await waitForUpdate();
vi.setSystemTime(new Date('2026-01-02T09:00'));
setAppState('active');
await waitForUpdate();

expect(screen.getByText('Today: 2 January')).toBeInTheDocument();
```

- **`LynxAppState.state()`** becomes `state`, and `active()` is `true` only for `'active'`.
- **`'inactive'`** is an app on screen that isn't taking touches, such as with Control Center pulled over it, or a call coming in.
- **It arrives as a change to the host's [global props](/guide/glossary.md#globaldata)**, as on a phone. Wait for it with `waitForUpdate()`.
- **Wait between two states too.** Otherwise an `effect()` runs once for both, and never sees the app leave.

## Network

Fakes what [`LynxNetwork`](/api/angular-lynx/device/network.md) reports. Each test starts online.

### `setOnline()`

```typescript
function setOnline(online: boolean): void;
```

Takes the phone offline, or brings it back online. A spec can then check what the app shows without a connection, and what it does once it's back.

```typescript
await render(Orders);

setOnline(false);
await waitForUpdate();

expect(
  screen.getByText('An internet connection is required'),
).toBeInTheDocument();
```

- **`LynxNetwork.online()`** becomes `online`.
- **It arrives as a change to the host's global props**, as on a phone. Wait for it with `waitForUpdate()`.
- **Wait between two changes too.** Otherwise an `effect()` runs once for both, and never sees the phone go offline.
- **It changes only what `LynxNetwork` reports.** Requests go on as the spec's fakes answer them.

## Back button

Presses Android's back button for [`LynxBackButton`](/api/angular-lynx/host/back-button.md) and the router. Each test starts with back left to Android.

### `pressBackButton()`

```typescript
function pressBackButton(): boolean;
```

Presses Android's back button, so a spec can check what the app does with it. The app may close what's open on top, with a handler registered with [`LynxBackButton.on()`](/api/angular-lynx/host/back-button.md#lynxbackbuttonon). Or it may go back a screen through the router.

```typescript
await render(Search);
fireEvent.tap(screen.getByText('Search'));
await waitForUpdate();

expect(pressBackButton()).toBe(true);
await waitForUpdate();

expect(screen.queryByText('Results')).toBeNull();
```

- **It returns `false` when the app doesn't take back.** As on Android, the press reaches the app only while the app takes back. Otherwise Android would leave the app.
- **It arrives through the [background thread](/guide/glossary.md#background-thread)**, as on a phone. Wait for it with `waitForUpdate()`.
- **An `<overlay>` takes back before the app** on a phone. Fire its [`requestclose`](/api/testing-library/event-reference.md#overlays) event for that instead.

## Types

| Type       | Description                                                            |
| ---------- | ---------------------------------------------------------------------- |
| `AppState` | What `setAppState()` takes: `'active'`, `'inactive'` or `'background'` |

## Related

- [Device fakes](/api/testing-library/fakes/index.md)
- [`LynxAppState`](/api/angular-lynx/device/app-state.md)
- [`LynxNetwork`](/api/angular-lynx/device/network.md)
- [`LynxBackButton`](/api/angular-lynx/host/back-button.md)
