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

# Contact and calendar fakes

Pick a contact, fill the address book or answer the calendar's event editor in a unit test. `import { answerContactPicker, addContact, answerCalendarSheet } from '@blotch/angular-lynx-testing-library';`

## Contacts

Fakes [`LynxContacts`](/api/angular-lynx/media/contacts.md)' two native modules: the address book and the picker.

Each test starts with an empty address book and no picker open. The person hasn't been asked yet: the first `getAll()` asks, and they allow it.

```typescript
await render(Invite);
fireEvent.tap(screen.getByText('Invite a friend'));
answerContactPicker({ name: 'Ada', phones: ['+1 555 0100'] });
await waitForUpdate();

expect(screen.getByText('Inviting Ada at +1 555 0100')).toBeInTheDocument();
```

A **`FakeContact`** is a `name`, with any of `id`, `phones`, `emails` and `postalAddresses`.

### `answerContactPicker()`

```typescript
function answerContactPicker(
  contact: FakeContact | null,
  options?: { value?: string; nameOnly?: boolean },
): void;
```

Closes the open picker as the person would, or queues the answer for the next one. `null` closes it without picking.

It picks as the phones do, for the `select` the app asked for:

- **`phone` or `email`**: one of the contact's numbers or addresses, its first or `value`.
  - It throws for a contact without one, which the real picker won't let the person tap.
  - A queued answer can't throw in the spec. The picker it answers fails instead, with a `ContactsError` of code `failed` whose message says why.
- **`contact`**: the whole contact, as iOS hands it over, and Android to an app that may read the address book.
  - Pass `nameOnly: true` to answer as Android does for an app that can't: the id and name, with empty lists.

### `failContactPicker()`

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

Ends the open picker, or the next one, with a `ContactsError` of that code.

### `contactPicker()`

```typescript
function contactPicker(): { select: 'contact' | 'phone' | 'email' } | undefined;
```

Says whether a picker is open, and what it asked the person to pick.

### `addContact()`

```typescript
function addContact(
  contact: FakeContact,
  options?: { shared?: boolean },
): Contact;
```

Adds a contact to the address book `getAll()` reads. It returns the contact as the app gets it, with an id of its own unless it has one.

`shared` marks one the person shared under iOS 18's limited access.

### `setContactsPermission()`

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

Sets what the person answered.

- **`limited`** has `getAll()` read only the `shared` contacts.
- **`denied`** rejects it with `permission-denied`.
- **`unsupported`** rejects it with `unavailable`, as in an app without `permissions.contacts`.
- **The picker needs no permission**, so this doesn't change it.
- **Set it before rendering.** `permission()` reads it at startup, and each time the app comes back.

## Calendar

Fakes the event editor [`LynxCalendar.addEvent()`](/api/angular-lynx/media/calendar.md) opens. Until a spec answers it, the editor stays open.

### `calendarEvents()`

```typescript
function calendarEvents(): readonly CalendarSheetEvent[];
```

The events the app opened the event editor with in this test, oldest first. Each test starts with none.

Each has `title`, `start` and `end` (as `Date`s), and `allDay`. It has `location` and `notes` when the app gave them.

```typescript
fireEvent.tap(screen.getByText('Add to calendar'));
await waitForUpdate();

expect(calendarEvents()).toEqual([
  expect.objectContaining({ title: 'Table for 2' }),
]);
```

### `answerCalendarSheet()`

```typescript
function answerCalendarSheet(result?: 'saved' | 'canceled' | 'unknown'): void;
```

Closes the event editor as the person would: the open `addEvent()` resolves with `result`, `saved` by default.

- **Called before the editor opens**, it closes the next one as soon as it opens.
- **Each call answers one editor**, in order.

### `failCalendarSheet()`

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

Ends the open `addEvent()`, or the next one, with a `CalendarError` of `code`: `unavailable` for a phone without an editor, or `failed`.

```typescript
failCalendarSheet('unavailable');
fireEvent.tap(screen.getByText('Add to calendar'));
await waitForUpdate();

expect(screen.getByText('Email me the booking')).toBeInTheDocument();
```

## Types

| Type                         | Description                                                 |
| ---------------------------- | ----------------------------------------------------------- |
| `FakeContact`                | A contact for `answerContactPicker()` and `addContact()`    |
| `AnswerContactPickerOptions` | `answerContactPicker()`'s `options`: `value` and `nameOnly` |
| `AddContactOptions`          | `addContact()`'s `options`: `shared`                        |
| `FakeContactPickerInfo`      | What `contactPicker()` returns while a picker is open       |
| `FakeContactsPermission`     | What `setContactsPermission()` takes                        |
| `CalendarSheetEvent`         | One of `calendarEvents()`                                   |
| `CalendarSheetAnswer`        | What `answerCalendarSheet()` takes                          |
| `CalendarSheetFailure`       | The codes `failCalendarSheet()` takes                       |

## Related

- [Device fakes](/api/testing-library/fakes/index.md)
- [`LynxContacts`](/api/angular-lynx/media/contacts.md)
- [`LynxCalendar`](/api/angular-lynx/media/calendar.md)
