Files
self/app/tests/utils/proving/provingMachine.test.ts
Justin Hernandez 20fa5c5adc [SELF-700] feat: add mock document generator demo (#995)
* feat: add mock document generator demo

* feat: add mock document generator

* fixes

* chore: refresh workflow cache

* update lock

* build

* updates

* more fixes

* code rabbit feedback

* compiles

* save wip

* updates

* merge with dev and fixes

* fix: align hoisting and demo Jest resolver (#1003)

* chore(app): map common src paths and declare svg flag module

* ci fix

* update lock

* save wip

* chore: address yarn lock issues (#1004)

* address yarn lock issues

* fix postinstall

* fix ci

* use metro js proxy

* android build working for /app

* save wip

* fix merge

* pipeline fixes

* format

* fix pipelines

* bump limit and split

* fix pipeline issues

* chore: decouple demo app build (#1013)

* chore: decouple demo app build

* chore: move demo app to workspace

* chore: unpublish demo workspace

* fix mobile sdk tests

* updates

* remove polyfills

* update merge

* update resolutions

* update resolutions

* fix merge

* fix paths

* save wip

* save wip fixes rd2

* working android

* update lock

* save wip ios building

* fix merge

* readd public key

* fixes

* ci fixes

* fixes

* fix web building

* fix ci

* fix tests

* update lock

* fix ci rd2

* formatting and fix ci

* fix

* finalize ci fixes

* fix tests and metro config paths for building

* save wip

* install missing package for pipeline

* fix wip app building

* wip react config

* save working emulator compile

* first round of pr fixes and feedback

* clean up demo app artifacts from sdk

* Add Gradle wrapper files for mobile-sdk-demo Android build

- Added gradlew, gradlew.bat, and gradle/wrapper/ directory
- Updated .gitignore to allow committing Gradle wrapper files
- Fixes Android build error: spawn ./gradlew ENOENT

* codex feedback and fixes

* fix tests

* file renames

* revert back to dev

* add types

* coderabbit fixes

* fix tests

* fix tests

* fix test

* fixes

* fix wip coderabbit issues

* coderabbit suggestions rd 2

* fix ci pipelines and addresss warnings

* cr fixes

* convert kebab to camelCase

* save wip fixes

* update reinstall and lock files

* fixes

* remove file

* fix lint

* fix polyfill fallback issues

* ensure that mock document is not on ofac list

* prettier
2025-09-27 13:59:47 -07:00

187 lines
5.2 KiB
TypeScript

// SPDX-FileCopyrightText: 2025 Social Connect Labs, Inc.
// SPDX-License-Identifier: BUSL-1.1
// NOTE: Converts to Apache-2.0 on 2029-06-11 per LICENSE.
import { act, renderHook } from '@testing-library/react-native';
import { PassportData } from '@selfxyz/common/types';
import { SdkEvents, type SelfClient } from '@selfxyz/mobile-sdk-alpha';
import { useProvingStore } from '@/utils/proving/provingMachine';
jest.mock('@/navigation', () => ({
navigationRef: {
isReady: jest.fn(() => true),
navigate: jest.fn(),
},
}));
jest.mock('@selfxyz/mobile-sdk-alpha', () => ({
// Mock only the exports needed by this test
SdkEvents: {
DOCUMENT_SELECTED: 'DOCUMENT_SELECTED',
DOCUMENT_LOADED: 'DOCUMENT_LOADED',
REGISTRATION_COMPLETED: 'REGISTRATION_COMPLETED',
PROVING_PASSPORT_DATA_NOT_FOUND: 'PROVING_PASSPORT_DATA_NOT_FOUND',
PROVING_PASSPORT_NOT_SUPPORTED: 'PROVING_PASSPORT_NOT_SUPPORTED',
PROVING_ACCOUNT_RECOVERY_REQUIRED: 'PROVING_ACCOUNT_RECOVERY_REQUIRED',
PROVING_ACCOUNT_VERIFIED_SUCCESS: 'PROVING_ACCOUNT_VERIFIED_SUCCESS',
PROVING_REGISTER_ERROR_OR_FAILURE: 'PROVING_REGISTER_ERROR_OR_FAILURE',
},
loadSelectedDocument: jest.fn().mockResolvedValue(null),
hasAnyValidRegisteredDocument: jest.fn().mockResolvedValue(true),
}));
describe('provingMachine registration completion', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('initializes proving machine for confirmed registration - no document found', async () => {
const { result: initHook } = renderHook(() =>
useProvingStore(state => state.init),
);
const emitMock = jest.fn();
const selfClient = {
trackEvent: jest.fn(),
emit: emitMock,
} as unknown as SelfClient;
expect(initHook.current).toBeDefined();
await act(async () => {
await initHook.current(selfClient, 'register');
});
const { result: provingStoreHook } = renderHook(() =>
useProvingStore(state => state.currentState),
);
expect(provingStoreHook.current).toBe('passport_data_not_found');
expect(emitMock).toHaveBeenCalledWith(
SdkEvents.PROVING_PASSPORT_DATA_NOT_FOUND,
);
});
});
describe('events', () => {
it('emits PROVING_MACHINE_PASSPORT_NOT_SUPPORTED', async () => {
const emitMock = jest.fn();
const mockPassportData = {
mrz: 'mrz',
dsc: 'dsc',
eContent: [1, 2, 3],
signedAttr: [1, 2, 3],
encryptedDigest: [1, 2, 3],
passportMetadata: {
countryCode: 'test',
},
documentCategory: 'passport',
} as PassportData;
const selfClient = {
emit: emitMock,
} as unknown as SelfClient;
await act(async () => {
useProvingStore.setState({ passportData: mockPassportData });
useProvingStore.getState()._handlePassportNotSupported(selfClient);
});
expect(emitMock).toHaveBeenCalledWith(
SdkEvents.PROVING_PASSPORT_NOT_SUPPORTED,
{
countryCode: 'test',
documentCategory: 'passport',
},
);
});
it('emits PROVING_MACHINE_PASSPORT_NOT_SUPPORTED with no passport data', async () => {
const emitMock = jest.fn();
const mockPassportData = {
passportMetadata: {},
} as PassportData;
const selfClient = {
emit: emitMock,
} as unknown as SelfClient;
await act(async () => {
useProvingStore.setState({ passportData: mockPassportData });
useProvingStore.getState()._handlePassportNotSupported(selfClient);
});
expect(emitMock).toHaveBeenCalledWith(
SdkEvents.PROVING_PASSPORT_NOT_SUPPORTED,
{
countryCode: null,
documentCategory: null,
},
);
});
it('emits PROVING_MACHINE_ACCOUNT_RECOVERY_CHOICE', async () => {
const emitMock = jest.fn();
const selfClient = {
emit: emitMock,
} as unknown as SelfClient;
await act(async () => {
useProvingStore.getState()._handleAccountRecoveryChoice(selfClient);
});
expect(emitMock).toHaveBeenCalledWith(
SdkEvents.PROVING_ACCOUNT_RECOVERY_REQUIRED,
);
});
it('emits PROVING_MACHINE_ACCOUNT_VERIFIED_SUCCESS', async () => {
const emitMock = jest.fn();
const selfClient = {
emit: emitMock,
} as unknown as SelfClient;
await act(async () => {
useProvingStore.getState()._handleAccountVerifiedSuccess(selfClient);
});
expect(emitMock).toHaveBeenCalledWith(
SdkEvents.PROVING_ACCOUNT_VERIFIED_SUCCESS,
);
});
it('emits PROVING_MACHINE_PASSPORT_DATA_NOT_FOUND', async () => {
const emitMock = jest.fn();
const selfClient = {
emit: emitMock,
} as unknown as SelfClient;
await act(async () => {
useProvingStore.getState()._handlePassportDataNotFound(selfClient);
});
expect(emitMock).toHaveBeenCalledWith(
SdkEvents.PROVING_PASSPORT_DATA_NOT_FOUND,
);
});
it('emits PROVING_MACHINE_REGISTER_ERROR_OR_FAILURE', async () => {
const emitMock = jest.fn();
const selfClient = {
emit: emitMock,
} as unknown as SelfClient;
await act(async () => {
useProvingStore.getState()._handleRegisterErrorOrFailure(selfClient);
});
expect(emitMock).toHaveBeenCalledWith(
SdkEvents.PROVING_REGISTER_ERROR_OR_FAILURE,
{
hasValidDocument: true,
},
);
});
});