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/connectivity.md.

Bluetooth, NFC and Wi-Fi fakes

Put a Bluetooth device in range, tap an NFC tag, or put the phone on a Wi-Fi network in a unit test. import { addBluetoothDevice, tapNfcTag, setWifiNetwork } from '@blotch/angular-lynx-testing-library';

Bluetooth

Fakes the radio behind LynxBluetooth. Each test starts with no devices and the radio on.

addBluetoothDevice()

function addBluetoothDevice(
  device: FakeBluetoothDevice,
): FakeBluetoothDeviceRef;

Puts a Bluetooth LE device in range of the app. LynxBluetooth finds it, connects to it and talks to it as it would a real one.

const frame = addBluetoothDevice({
  id: 'frame',
  name: 'Blotch - XyZ1',
  services: {
    [SETUP]: {
      [CREDENTIALS]: {
        properties: ['write'],
        onWrite: (_value, device) => device.notify(SETUP, STATUS, ONLINE),
      },
      [STATUS]: { properties: ['notify'] },
    },
  },
});
await render(SetUpFrame);

fireEvent.tap(await screen.findByText('Blotch - XyZ1'));
await waitForUpdate();

expect(frame.writes).toEqual([
  {
    service: SETUP,
    characteristic: CREDENTIALS,
    value: WIFI,
    withResponse: true,
  },
]);
  • Scans see it. A running scan sees it at once, and one that starts later sees it as it starts.
  • It advertises every service it has, unless advertisedServices says otherwise.
  • A characteristic can be read, written and notified, unless its properties say otherwise.
  • onWrite is called with each value the app writes, and the device. The spec can then answer as the real device does.
  • A write longer than the device takes is refused, as on a phone. maxWriteLength sets the limit for a write without response.
  • UUIDs are spelled either way, as canonicalBluetoothUUID() reads them.
  • Adding an id a second time throws.

The device it returns:

MemberDescription
connectedWhether the app is connected to it
writesWhat the app wrote, oldest first
notify(service, characteristic, value)Sends a value, which reaches the app while it's subscribed
disconnect()Ends the connection from the device's side, as going out of range does

setBluetoothState()

function setBluetoothState(state: BluetoothState): void;

Sets the state of the phone's Bluetooth, as the person turning it off or refusing permission does. A spec can then check what the app shows.

setBluetoothState('poweredOff');
await render(SetUpFrame);

expect(await screen.findByText('Turn on Bluetooth')).toBeInTheDocument();
  • Anything but 'poweredOn' ends a running scan and every connection, as it does on a phone.
  • 'unknown' stands for not asked yet. The person allows it when LynxBluetooth asks, as requestPermission(), scan() and connect() do.

NFC

Fakes LynxNfc's native module. Each test starts with a phone that reads tags, no session open and no writes.

Each answer reaches the app through the background thread, as on a phone. Wait for it with waitForUpdate().

await render(MemberCard);
fireEvent.tap(screen.getByText('Scan your card'));
tapNfcTag({ records: [{ type: 'url', value: 'https://example.com/m/42' }] });
await waitForUpdate();

expect(
  screen.getByText('Member: https://example.com/m/42'),
).toBeInTheDocument();

tapNfcTag()

function tapNfcTag(tag?: FakeNfcTag): void;

Brings a tag near the phone.

  • The open read() resolves with its records.
  • The open write() writes to it.
  • An open scan() calls back with it.
  • With no session open yet, the next one takes it.
FakeNfcTag propertyDefaultDescription
recordsnoneWhat the tag holds, which a read resolves with
writabletruefalse for a locked tag
capacity888The bytes its NDEF message takes
idnoneIts serial number
ndeftruefalse for a tag without NDEF records

failNfcSession()

function failNfcSession(
  code: 'canceled' | 'timeout' | 'busy' | 'failed',
  message?: string,
): void;

Ends the open session, or the next one, as iOS does: canceled when the person closes the sheet, timeout after a minute.

setNfcState()

function setNfcState(state: 'ready' | 'disabled' | 'unsupported'): void;

Turns NFC off (disabled), or takes the reader away (unsupported).

state() reads it at startup, and each time the app comes back. Send the app away and back with setAppState().

nfcSession()

function nfcSession(): NfcSessionInfo | undefined;

Says what's open: its alertMessage, whether it's a scan() (multiple), and whether it's writing.

nfcWrites()

function nfcWrites(): readonly (readonly NfcRecord[])[];

Lists the records each write() wrote, as a read gives them back.

Wi-Fi

Fakes the network behind LynxWifi. Each test starts off Wi-Fi, with no joins.

setWifiNetwork()

function setWifiNetwork(network: { ssid: string; bssid?: string } | null): void;

Puts the spec's phone on network, which LynxWifi.currentNetwork() resolves from then on. null takes it off Wi-Fi.

setWifiNetwork({ ssid: 'Home' });
await render(FrameSetup);
await waitForUpdate();

expect(screen.getByText('Home')).toBeInTheDocument();
  • A successful join() puts the phone on the network it joined.
  • leave() puts it back where it was.

denyWifiNetworkRead()

function denyWifiNetworkRead(
  code?: 'denied' | 'notFound' | 'canceled' | 'unavailable' | 'failed',
  message?: string,
): void;

Makes currentNetwork() reject with a WifiError of code from then on.

  • code is denied by default.
  • The default message for denied is the one an Android app gets when the person refuses the location prompt.

allowWifiNetworkRead()

function allowWifiNetworkRead(): void;

Lets currentNetwork() read the network again, after denyWifiNetworkRead().

queueWifiJoinResult()

function queueWifiJoinResult(
  result:
    'joined' | 'denied' | 'notFound' | 'canceled' | 'unavailable' | 'failed',
  message?: string,
): void;

Answers the next LynxWifi.join() with result: joined, which puts the phone on the network, or a WifiError of that code.

queueWifiJoinResult('notFound');
fireEvent.tap(screen.getByText('Connect'));
await waitForUpdate();

expect(screen.getByText('Is the frame switched on?')).toBeInTheDocument();
  • Each call answers one join, in order.
  • Without one, a join succeeds.

wifiJoins()

function wifiJoins(): readonly {
  ssid: string;
  password?: string;
  joinOnce: boolean;
}[];

The networks the app joined through LynxWifi.join() in this test, oldest first, answered or not. Each test starts with none.

wifiLeaves()

function wifiLeaves(): readonly string[];

The names of the networks the app left in this test. Each test starts with none.

Types

TypeDescription
FakeBluetoothDeviceWhat addBluetoothDevice() takes: id, name, rssi, advertisedServices, serviceData, manufacturerData, services, maxWriteLength
FakeBluetoothCharacteristicOne characteristic in services: its properties, value (what a read gets) and onWrite
FakeBluetoothDeviceRefThe device addBluetoothDevice() returns
FakeBluetoothWriteOne of its writes: service, characteristic, value and withResponse
FakeNfcTagWhat tapNfcTag() takes
NfcSessionInfoWhat nfcSession() returns: alertMessage, multiple and writing
NfcSessionFailureThe codes failNfcSession() takes
FakeWifiNetworkWhat setWifiNetwork() takes: ssid and an optional bssid
FakeWifiFailureThe WifiError codes denyWifiNetworkRead() and queueWifiJoinResult() take
WifiJoinRequestOne of wifiJoins(): ssid, password and joinOnce