For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/testing-library/fakes/media.md.

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(), since a spec has no library to pick from or camera to point. Each test starts with nothing to pick and no requests.

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()

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()

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()

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()

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(), since a spec has no camera to point. Each test starts with no scan open and nothing queued.

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, as on a phone. Wait for it with waitForUpdate().

queueBarcodeScan()

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()

function cancelBarcodeScan(): void;

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

failBarcodeScan()

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.

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

savedPhotos()

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()

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's picker and save screen.

setPickedFiles()

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.

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()

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()

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.

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

TypeDescription
FakeImageWhat setPickedImage() and setTakenPhoto() take
FakeImageSource'library' or 'camera'
ImagePickFailureThe codes failImagePicker() takes
ImagePickerRequestOne of imagePickerRequests()
QueuedBarcodeScanWhat queueBarcodeScan() takes: format and value, both strings
BarcodeScanFailureThe codes failBarcodeScan() takes
SavedPhotoOne of savedPhotos()
FakePhotoLibraryPermissionWhat setPhotoLibraryPermission() takes
FakePickedFileWhat setPickedFiles() takes
SavedFileOne of savedFiles()