|
| 1 | +import * as localAuthentication from '../lib/methods/helpers/localAuthentication'; |
| 2 | + |
| 3 | +// Mock the localAuthentication module |
| 4 | +jest.mock('../lib/methods/helpers/localAuthentication', () => ({ |
| 5 | + handleLocalAuthentication: jest.fn(), |
| 6 | + changePasscode: jest.fn(), |
| 7 | + checkHasPasscode: jest.fn(), |
| 8 | + supportedBiometryLabel: jest.fn() |
| 9 | +})); |
| 10 | + |
| 11 | +describe('ScreenLockConfigView - Passcode Change with Modal Delay', () => { |
| 12 | + beforeEach(() => { |
| 13 | + jest.clearAllMocks(); |
| 14 | + jest.useFakeTimers(); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + jest.useRealTimers(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should add 300ms delay between authentication and passcode change', async () => { |
| 22 | + const handleLocalAuthenticationMock = localAuthentication.handleLocalAuthentication as jest.Mock; |
| 23 | + const changePasscodeMock = localAuthentication.changePasscode as jest.Mock; |
| 24 | + |
| 25 | + handleLocalAuthenticationMock.mockResolvedValue(undefined); |
| 26 | + changePasscodeMock.mockResolvedValue(undefined); |
| 27 | + |
| 28 | + // Simulate the flow: authentication -> delay -> passcode change |
| 29 | + const simulatePasscodeChange = async (autoLock: boolean) => { |
| 30 | + if (autoLock) { |
| 31 | + try { |
| 32 | + await handleLocalAuthenticationMock(true); |
| 33 | + // Add 300ms delay |
| 34 | + await new Promise(resolve => setTimeout(resolve, 300)); |
| 35 | + } catch { |
| 36 | + return; |
| 37 | + } |
| 38 | + } |
| 39 | + await changePasscodeMock({ force: false }); |
| 40 | + }; |
| 41 | + |
| 42 | + const promise = simulatePasscodeChange(true); |
| 43 | + |
| 44 | + // Verify handleLocalAuthentication was called |
| 45 | + await Promise.resolve(); // Flush microtasks |
| 46 | + expect(handleLocalAuthenticationMock).toHaveBeenCalledWith(true); |
| 47 | + |
| 48 | + // Fast-forward timers to simulate the 300ms delay |
| 49 | + jest.runAllTimers(); |
| 50 | + |
| 51 | + await promise; |
| 52 | + |
| 53 | + // Verify changePasscode was called after the delay |
| 54 | + expect(changePasscodeMock).toHaveBeenCalledWith({ force: false }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should handle authentication cancellation gracefully', async () => { |
| 58 | + const handleLocalAuthenticationMock = localAuthentication.handleLocalAuthentication as jest.Mock; |
| 59 | + const changePasscodeMock = localAuthentication.changePasscode as jest.Mock; |
| 60 | + |
| 61 | + // Simulate user canceling authentication |
| 62 | + handleLocalAuthenticationMock.mockRejectedValue(new Error('Authentication cancelled')); |
| 63 | + |
| 64 | + // Simulate the flow with error handling |
| 65 | + const simulatePasscodeChange = async (autoLock: boolean) => { |
| 66 | + if (autoLock) { |
| 67 | + try { |
| 68 | + await handleLocalAuthenticationMock(true); |
| 69 | + await new Promise(resolve => setTimeout(resolve, 300)); |
| 70 | + } catch { |
| 71 | + // User cancelled - should return early |
| 72 | + return; |
| 73 | + } |
| 74 | + } |
| 75 | + await changePasscodeMock({ force: false }); |
| 76 | + }; |
| 77 | + |
| 78 | + await simulatePasscodeChange(true); |
| 79 | + |
| 80 | + // Verify handleLocalAuthentication was called |
| 81 | + expect(handleLocalAuthenticationMock).toHaveBeenCalledWith(true); |
| 82 | + |
| 83 | + // Verify that changePasscode was NOT called when authentication fails |
| 84 | + expect(changePasscodeMock).not.toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should proceed directly to passcode change when autoLock is disabled', async () => { |
| 88 | + const handleLocalAuthenticationMock = localAuthentication.handleLocalAuthentication as jest.Mock; |
| 89 | + const changePasscodeMock = localAuthentication.changePasscode as jest.Mock; |
| 90 | + |
| 91 | + changePasscodeMock.mockResolvedValue(undefined); |
| 92 | + |
| 93 | + // Simulate the flow without authentication |
| 94 | + const simulatePasscodeChange = async (autoLock: boolean) => { |
| 95 | + if (autoLock) { |
| 96 | + try { |
| 97 | + await handleLocalAuthenticationMock(true); |
| 98 | + await new Promise(resolve => setTimeout(resolve, 300)); |
| 99 | + } catch { |
| 100 | + return; |
| 101 | + } |
| 102 | + } |
| 103 | + await changePasscodeMock({ force: false }); |
| 104 | + }; |
| 105 | + |
| 106 | + await simulatePasscodeChange(false); |
| 107 | + |
| 108 | + // Verify handleLocalAuthentication was NOT called |
| 109 | + expect(handleLocalAuthenticationMock).not.toHaveBeenCalled(); |
| 110 | + |
| 111 | + // Verify changePasscode was called directly |
| 112 | + expect(changePasscodeMock).toHaveBeenCalledWith({ force: false }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should use 300ms as the delay duration', () => { |
| 116 | + const delay = 300; |
| 117 | + |
| 118 | + // Create a promise that resolves after the delay |
| 119 | + const delayPromise = new Promise(resolve => setTimeout(resolve, delay)); |
| 120 | + |
| 121 | + // Advance timers by exactly 300ms |
| 122 | + jest.advanceTimersByTime(delay); |
| 123 | + |
| 124 | + // The promise should resolve |
| 125 | + return expect(delayPromise).resolves.toBeUndefined(); |
| 126 | + }); |
| 127 | +}); |
0 commit comments