|
| 1 | +import React from 'react'; |
| 2 | +import { fireEvent, render, waitFor } from '@testing-library/react-native'; |
| 3 | + |
| 4 | +import PasscodeEnter from './PasscodeEnter'; |
| 5 | +import { biometryAuth } from '../../lib/methods/helpers/localAuthentication'; |
| 6 | +import { biometricTrustStore } from '../../lib/biometricTrustStore'; |
| 7 | +import UserPreferences from '../../lib/methods/userPreferences'; |
| 8 | +import { BIOMETRY_ENABLED_KEY } from '../../lib/constants/localAuthentication'; |
| 9 | + |
| 10 | +jest.mock('../../lib/methods/helpers/localAuthentication', () => ({ |
| 11 | + biometryAuth: jest.fn(), |
| 12 | + resetAttempts: jest.fn(() => Promise.resolve()) |
| 13 | +})); |
| 14 | + |
| 15 | +jest.mock('../../lib/biometricTrustStore', () => ({ |
| 16 | + biometricTrustStore: { |
| 17 | + enrol: jest.fn(), |
| 18 | + disenrol: jest.fn(() => Promise.resolve()), |
| 19 | + verify: jest.fn(), |
| 20 | + probeExists: jest.fn() |
| 21 | + } |
| 22 | +})); |
| 23 | + |
| 24 | +jest.mock('../../lib/methods/userPreferences', () => ({ |
| 25 | + __esModule: true, |
| 26 | + default: { |
| 27 | + getBool: jest.fn(), |
| 28 | + setBool: jest.fn(), |
| 29 | + getString: jest.fn(), |
| 30 | + setString: jest.fn() |
| 31 | + }, |
| 32 | + useUserPreferences: () => [null, jest.fn()] |
| 33 | +})); |
| 34 | + |
| 35 | +jest.mock('../../i18n', () => ({ t: (key: string) => key })); |
| 36 | + |
| 37 | +const mockedBiometryAuth = biometryAuth as jest.Mock; |
| 38 | +const mockedDisenrol = biometricTrustStore.disenrol as jest.Mock; |
| 39 | +const mockedSetBool = UserPreferences.setBool as jest.Mock; |
| 40 | + |
| 41 | +describe('PasscodeEnter biometry button', () => { |
| 42 | + beforeEach(() => { |
| 43 | + jest.clearAllMocks(); |
| 44 | + mockedDisenrol.mockResolvedValue(undefined); |
| 45 | + }); |
| 46 | + |
| 47 | + it('enrollmentChanged from button press → disenrols, clears flag, hides biometry button', async () => { |
| 48 | + mockedBiometryAuth.mockResolvedValueOnce({ kind: 'enrollmentChanged' }); |
| 49 | + const finishProcess = jest.fn(); |
| 50 | + |
| 51 | + const { getByTestId, queryByTestId } = render(<PasscodeEnter hasBiometry skipAutoBiometry finishProcess={finishProcess} />); |
| 52 | + |
| 53 | + await waitFor(() => expect(getByTestId('biometry-button')).toBeTruthy()); |
| 54 | + |
| 55 | + fireEvent.press(getByTestId('biometry-button')); |
| 56 | + |
| 57 | + await waitFor(() => expect(mockedDisenrol).toHaveBeenCalledTimes(1)); |
| 58 | + expect(mockedSetBool).toHaveBeenCalledWith(BIOMETRY_ENABLED_KEY, false); |
| 59 | + expect(finishProcess).not.toHaveBeenCalled(); |
| 60 | + await waitFor(() => expect(queryByTestId('biometry-button')).toBeNull()); |
| 61 | + }); |
| 62 | + |
| 63 | + it('success from button press → finishes process, no invalidation', async () => { |
| 64 | + mockedBiometryAuth.mockResolvedValueOnce({ kind: 'success' }); |
| 65 | + const finishProcess = jest.fn(); |
| 66 | + |
| 67 | + const { getByTestId } = render(<PasscodeEnter hasBiometry skipAutoBiometry finishProcess={finishProcess} />); |
| 68 | + |
| 69 | + await waitFor(() => expect(getByTestId('biometry-button')).toBeTruthy()); |
| 70 | + |
| 71 | + fireEvent.press(getByTestId('biometry-button')); |
| 72 | + |
| 73 | + await waitFor(() => expect(finishProcess).toHaveBeenCalledTimes(1)); |
| 74 | + expect(mockedDisenrol).not.toHaveBeenCalled(); |
| 75 | + expect(mockedSetBool).not.toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('canceled from button press → flag untouched, biometry button stays', async () => { |
| 79 | + mockedBiometryAuth.mockResolvedValueOnce({ kind: 'canceled' }); |
| 80 | + const finishProcess = jest.fn(); |
| 81 | + |
| 82 | + const { getByTestId } = render(<PasscodeEnter hasBiometry skipAutoBiometry finishProcess={finishProcess} />); |
| 83 | + |
| 84 | + await waitFor(() => expect(getByTestId('biometry-button')).toBeTruthy()); |
| 85 | + |
| 86 | + fireEvent.press(getByTestId('biometry-button')); |
| 87 | + |
| 88 | + await waitFor(() => expect(mockedBiometryAuth).toHaveBeenCalled()); |
| 89 | + expect(mockedDisenrol).not.toHaveBeenCalled(); |
| 90 | + expect(mockedSetBool).not.toHaveBeenCalled(); |
| 91 | + expect(finishProcess).not.toHaveBeenCalled(); |
| 92 | + expect(getByTestId('biometry-button')).toBeTruthy(); |
| 93 | + }); |
| 94 | +}); |
0 commit comments