Skip to main content
‹ /jest
Types

Types of /jest

The types of the mock’s controls: the simulated device, a prompt it showed, an item of an old library to plant, and what is left of one after a migration.

Import
import { type KeychainMock, type MockDevice, type MockPrompt } from '@apexnative/keychain/jest';

@apexnative/keychain/jest also exports every type of the package, the same as @apexnative/keychain does; they are on the pages of their sections. These are the types of the mock's own controls, keychainMock.

KeychainMock​

The type of keychainMock.

type KeychainMock = {
reset(): void;
setDevice(device: Partial<MockDevice>): void;
failNextAuthentication(code: KeychainErrorCode): void;
changeBiometry(): void;
prompts(): MockPrompt[];
addMigrationSource(item: MockMigrationItem): void;
migrationSources(): MockMigrationSource[];
};
MethodTypeDescription
reset() => voidForgets every item, prompt, queued failure and planted old item, and restores the default device. More
setDevice(device) => voidChanges the fields of the device it is given; the others stay. More
failNextAuthentication(code) => voidThe next prompt fails with the code. Calls queue up; KEY_INVALIDATED also deletes the item. More
changeBiometry() => voidA new finger or face: biometryCurrentSet* items throw KEY_INVALIDATED at their next read. More
prompts() => MockPrompt[]The prompts shown since the last reset, oldest first. More
addMigrationSource(item) => voidPlants an item of react-native-keychain or expo-secure-store for migrate. More
migrationSources() => MockMigrationSource[]The planted items that are still there. More

MockDevice​

The device the mock simulates. setDevice takes any part of it.

type MockDevice = {
defaultService: string;
biometryTypes: BiometryType[];
biometryEnrolled: boolean;
passcodeSet: boolean;
faceIDUsageDescription: boolean;
androidApi: number | null;
hardwareSecurityLevel: HardwareSecurityLevel | null;
};
FieldTypeDescription
defaultServicestringThe service of the default store, keychain: the bundle identifier or the application id of a real app. A react-native-keychain generic password planted under it is the one migrateFromReactNativeKeychain moves by default. Default 'dev.apexnative.keychain.mock'.
biometryTypesBiometryType[]The biometric hardware: what getBiometryTypes returns. [] is a device without a sensor, where the biometric access controls are noHardware. Default ['faceID'].
biometryEnrolledbooleanWhether biometry is enrolled. Without it getBiometryState() is null, and biometryAny, biometryCurrentSet and biometryCurrentSetOrPasscode are notEnrolled. Default true.
passcodeSetbooleanWhether the device has a passcode: what isPasscodeSet returns. Without it every access control is passcodeNotSet, and whenPasscodeSetThisDeviceOnly can't be written. Default true.
faceIDUsageDescriptionbooleaniOS: whether the app has NSFaceIDUsageDescription in its Info.plist. Without it, on a device with faceID, a biometric write and canUseAccessControl for a biometric access control reject with MISSING_USAGE_DESCRIPTION. Default true.
androidApinumber | nullAndroid: the API level. null is a device that isn't an Android one. Below 30 the passcode variants are unsupportedOS, and a write with one, or with authValidity above 0, rejects with UNSUPPORTED. Default null.
hardwareSecurityLevelHardwareSecurityLevel | nullAndroid: the best hardware a new key can live in, what getHardwareSecurityLevel returns. null is iOS, where keyStorage means nothing. With a level, getItemInfo reports android.securityLevel: tee stays in the TEE even with StrongBox, strongbox falls back to the TEE, and strongboxRequired rejects with HARDWARE_UNAVAILABLE unless the level is strongbox. Default null.

MockPrompt​

A prompt the mock showed, as prompts() returns it.

type MockPrompt = {
key: string;
title?: string;
subtitle?: string;
description?: string;
cancel?: string;
fallback?: string;
confirmationRequired: boolean;
};
FieldTypeDescription
keystringThe key of the item the prompt was for. For an old item that migrateItem read: the user name of react-native-keychain, the key of expo-secure-store.
title, subtitle, description, cancel, fallbackstringThe texts of the prompt: the store's `prompt` and the call's, merged field by field. A text neither of them set is missing.
confirmationRequiredbooleanAlways there: the call's, the store's, or true.

MockMigrationItem​

An item of an old library, as that library left it on the device: what addMigrationSource plants. One of three shapes, with three optional fields shared by all of them.

type MockMigrationItem = {
requiresAuth?: boolean;
corrupted?: boolean;
accessGroup?: string;
} & (
| {
// A generic password of react-native-keychain.
library: 'reactNativeKeychain';
service: string;
username: string;
password: string;
}
| {
// Internet credentials of react-native-keychain.
library: 'reactNativeKeychain';
server: string;
username: string;
password: string;
}
| {
// An item of expo-secure-store.
library: 'expoSecureStore';
key: string;
value: string;
keychainService?: string;
}
);
FieldTypeDescription
libraryRequired'reactNativeKeychain' | 'expoSecureStore'The library that wrote it.
servicestringreact-native-keychain, a generic password: its service. It becomes the credentials of the store of that service.
serverstringreact-native-keychain, internet credentials: the server. They become the credentials with that server.
username, passwordstringreact-native-keychain: the credentials.
key, valuestringexpo-secure-store: the key and the value. The item keeps its key.
keychainServicestringexpo-secure-store: the keychainService the item was stored with. Default 'app'.
requiresAuthbooleanThe old item is protected: migrateFrom… skips it as requiresAuth, and migrateItem reads it with a prompt. Default false.
corruptedbooleanReading it fails with DATA_CORRUPTED, as bytes that aren't text do. Default false.
accessGroupstringiOS: the access group the old item is in. A migration that names an accessGroup finds only the items of that group.

MockMigrationSource​

A planted item that is still there, as migrationSources() returns it.

type MockMigrationSource = {
library: 'reactNativeKeychain' | 'expoSecureStore';
kind: 'genericPassword' | 'internetPassword' | 'secureStore';
service: string;
account: string;
requiresAuth: boolean;
migrated: boolean;
};
FieldTypeDescription
library'reactNativeKeychain' | 'expoSecureStore'The library that wrote it.
kind'genericPassword' | 'internetPassword' | 'secureStore'What it becomes in the package: the kinds of a migration report.
servicestringThe service or the server; for expo-secure-store, the keychainService.
accountstringThe user name; for expo-secure-store, the key.
requiresAuthbooleanWhether reading it needs a prompt.
migratedbooleanA run moved it and kept it: a run without removeSource. A later run with removeSource deletes it, if the new item is still there.

See also​