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

# Camera, photo and file fakes

Pick or take a photo, scan a code, save to Photos or pick a file in a unit test. `import { setPickedImage, queueBarcodeScan, setPickedFiles } from '@blotch/angular-lynx-testing-library';`

## Image picker

Answers [`LynxImagePicker.pick()`](/api/angular-lynx/media/image-picker.md), since a spec has no library to pick from or camera to point. Each test starts with nothing to pick and no requests.

```typescript
await render(ExpenseForm);

setTakenPhoto({ name: 'receipt.jpg', content: jpegBytes });
fireEvent.tap(screen.getByText('Take photo'));
await waitForUpdate();

expect(screen.getByText('Receipt added')).toBeInTheDocument();
```

- **Each answer lasts for the rest of the test.**
- **Until a spec gives it an image**, `pick()` rejects with `unavailable`.
- **The fake hands back the bytes as you gave them.** It doesn't scale or re-encode them for `maxDimension` or `quality`.

### `setPickedImage()`

```typescript
function setPickedImage(image: FakeImage | null): void;

type FakeImage = {
  readonly name: string;
  readonly type?: string; // image/jpeg without it
  readonly content: string | Uint8Array; // text is stored as UTF-8
};
```

Has the person pick `image` from the library: `pick()` resolves with it. `null` closes the picker, and `pick()` resolves with `null`.

### `setTakenPhoto()`

```typescript
function setTakenPhoto(image: FakeImage | null): void;
```

Has the person take `image` with the camera (`source: 'camera'`): `pick()` resolves with it. `null` closes the camera, and `pick()` resolves with `null`.

### `failImagePicker()`

```typescript
function failImagePicker(
  code: 'unavailable' | 'denied' | 'failed',
  source?: 'library' | 'camera', // both when it's left out
): void;
```

Has `pick()` reject with `code`, such as `denied` for a person who won't let the app use the camera.

### `imagePickerRequests()`

```typescript
function imagePickerRequests(): readonly {
  readonly source: 'library' | 'camera';
  readonly maxDimension?: number;
  readonly quality?: number;
}[];
```

Lists the picks the app asked for, oldest first.

## Barcode scanner

Answers [`LynxBarcodeScanner.scan()`](/api/angular-lynx/hardware/barcode-scanner.md), since a spec has no camera to point. Each test starts with no scan open and nothing queued.

```typescript
await render(CheckIn);

queueBarcodeScan({ format: 'qr', value: 'TICKET-42' });
fireEvent.tap(screen.getByText('Scan ticket'));
await waitForUpdate();

expect(screen.getByText('Welcome, ticket 42')).toBeInTheDocument();
```

- **A scan stays open** until one of these functions answers it.
- **Each call answers the open scan.** When none is open, it answers the next one as soon as it's called, in order.
- **The answer reaches the app through the [background thread](/guide/glossary.md#background-thread)**, as on a phone. Wait for it with `waitForUpdate()`.

### `queueBarcodeScan()`

```typescript
function queueBarcodeScan(scan: {
  readonly format: string;
  readonly value: string;
}): void;
```

Reads `scan`, as if the person pointed the camera at it: `scan()` resolves with it.

### `cancelBarcodeScan()`

```typescript
function cancelBarcodeScan(): void;
```

Closes the scanner, as the Cancel button does: `scan()` rejects with `canceled`.

### `failBarcodeScan()`

```typescript
function failBarcodeScan(
  code: 'unavailable' | 'denied' | 'failed',
  message?: string,
): void;
```

Rejects with `code`, such as `denied` for a person who won't let the app use the camera.

## Photo library

Stands in for the phone's photo library behind [`LynxPhotoLibrary`](/api/angular-lynx/media/photo-library.md).

```typescript
await render(Editor);

fireEvent.tap(screen.getByText('Save to Photos'));
await waitForUpdate();

expect(savedPhotos()).toMatchObject([{ name: 'edit.jpg', type: 'image/jpeg' }]);
```

- **Saves reach the app through the background thread**, so wait with `waitForUpdate()`.
- **Set the permission before rendering.** The service reads it when it's created, after each save, and when the app comes back to the front ([`setAppState()`](/api/testing-library/fakes/device.md#setappstate)).

### `savedPhotos()`

```typescript
function savedPhotos(): readonly {
  readonly id: string;
  readonly name: string;
  readonly type: string;
  readonly bytes: Uint8Array;
}[];
```

Lists what `save()` added in this test, oldest first, with the ID it resolved with.

### `setPhotoLibraryPermission()`

```typescript
function setPhotoLibraryPermission(
  permission: 'granted' | 'limited' | 'denied' | 'undetermined' | 'unsupported',
): void;
```

Sets what the person has answered.

- **`denied`** makes `save()` reject with `permission-denied`.
- **`unsupported`** makes it reject with `unavailable`.
- **Each test starts `undetermined`**: the first save asks, and the person allows it.

## File picker

Stands in for the person using [`LynxFilePicker`](/api/angular-lynx/media/file-picker.md)'s picker and save screen.

### `setPickedFiles()`

```typescript
function setPickedFiles(files: readonly FakePickedFile[]): void;

type FakePickedFile = {
  readonly name: string;
  readonly type?: string; // application/octet-stream without it
  readonly content: string | Uint8Array; // text is stored as UTF-8
};
```

Has the person pick `files` each time the app opens the picker in this test.

```typescript
setPickedFiles([
  { name: 'receipt.pdf', type: 'application/pdf', content: pdfBytes },
]);
fireEvent.tap(screen.getByText('Attach receipt'));
await waitForUpdate();

expect(screen.getByText('Attached receipt.pdf')).toBeInTheDocument();
```

- **Until a spec calls it**, the person closes the picker, and `pick()` resolves with `[]`.
- **Files over 20 MiB together** reject with `too-large`, as on a phone.

### `setFilePickerCanceled()`

```typescript
function setFilePickerCanceled(): void;
```

Has the person close the picker and the save screen for the rest of the test. `pick()` resolves with `[]`, and `save()` with `false`. `setPickedFiles()` undoes it.

### `savedFiles()`

```typescript
function savedFiles(): readonly SavedFile[];

type SavedFile = {
  readonly name: string;
  readonly type: string;
  readonly bytes: Uint8Array;
  readonly text: string; // the bytes read as UTF-8
};
```

The files the app saved with `LynxFilePicker.save()` in this test, oldest first. Each test starts with none.

```typescript
fireEvent.tap(screen.getByText('Export CSV'));
await waitForUpdate();

expect(savedFiles()).toMatchObject([
  { name: 'expenses.csv', type: 'text/csv' },
]);
expect(savedFiles()[0]!.text).toContain('Total,42');
```

## Types

| Type                         | Description                                                         |
| ---------------------------- | ------------------------------------------------------------------- |
| `FakeImage`                  | What `setPickedImage()` and `setTakenPhoto()` take                  |
| `FakeImageSource`            | `'library'` or `'camera'`                                           |
| `ImagePickFailure`           | The codes `failImagePicker()` takes                                 |
| `ImagePickerRequest`         | One of `imagePickerRequests()`                                      |
| `QueuedBarcodeScan`          | What `queueBarcodeScan()` takes: `format` and `value`, both strings |
| `BarcodeScanFailure`         | The codes `failBarcodeScan()` takes                                 |
| `SavedPhoto`                 | One of `savedPhotos()`                                              |
| `FakePhotoLibraryPermission` | What `setPhotoLibraryPermission()` takes                            |
| `FakePickedFile`             | What `setPickedFiles()` takes                                       |
| `SavedFile`                  | One of `savedFiles()`                                               |

## Related

- [Device fakes](/api/testing-library/fakes/index.md)
- [`LynxImagePicker`](/api/angular-lynx/media/image-picker.md)
- [`LynxPhotoLibrary`](/api/angular-lynx/media/photo-library.md)
- [`LynxFilePicker`](/api/angular-lynx/media/file-picker.md)
- [`LynxBarcodeScanner`](/api/angular-lynx/hardware/barcode-scanner.md)
