|
| 1 | +import * as localAuthentication from '../lib/methods/helpers/localAuthentication'; |
| 2 | +import { ScreenLockConfigView } from './ScreenLockConfigView'; |
| 3 | + |
| 4 | +// Mock localAuthentication helpers used by the component |
| 5 | +jest.mock('../lib/methods/helpers/localAuthentication', () => ({ |
| 6 | + handleLocalAuthentication: jest.fn(), |
| 7 | + changePasscode: jest.fn(), |
| 8 | + checkHasPasscode: jest.fn(), |
| 9 | + supportedBiometryLabel: jest.fn() |
| 10 | +})); |
| 11 | + |
| 12 | +// Mock the database to avoid hitting real DB in constructor.init() |
| 13 | +jest.mock('../lib/database', () => ({ |
| 14 | + servers: { |
| 15 | + get: jest.fn(() => ({ |
| 16 | + find: jest.fn(() => ({ autoLock: true, autoLockTime: null })) |
| 17 | + })) |
| 18 | + } |
| 19 | +})); |
| 20 | + |
| 21 | +// Mock i18n to avoid initialization side-effects in tests |
| 22 | +jest.mock('../i18n', () => ({ |
| 23 | + __esModule: true, |
| 24 | + default: { t: jest.fn((k: string) => k) } |
| 25 | +})); |
| 26 | + |
| 27 | +describe('ScreenLockConfigView - integration tests', () => { |
| 28 | + beforeEach(() => { |
| 29 | + jest.clearAllMocks(); |
| 30 | + jest.useFakeTimers(); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + jest.useRealTimers(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should add 300ms delay between authentication and passcode change (real component)', async () => { |
| 38 | + const handleLocalAuthenticationMock = (localAuthentication.handleLocalAuthentication as jest.Mock).mockResolvedValue(undefined); |
| 39 | + const changePasscodeMock = (localAuthentication.changePasscode as jest.Mock).mockResolvedValue(undefined); |
| 40 | + |
| 41 | + // Create a mock instance of the component |
| 42 | + const mockInstance: any = new (ScreenLockConfigView as any)({ theme: 'light', server: '', Force_Screen_Lock: false, Force_Screen_Lock_After: 0 }); |
| 43 | + |
| 44 | + // Set the state we want for this test |
| 45 | + mockInstance.state = { autoLock: true }; |
| 46 | + |
| 47 | + const promise = mockInstance.changePasscode({ force: false }); |
| 48 | + |
| 49 | + // microtask flush so the handleLocalAuthentication call happens |
| 50 | + await Promise.resolve(); |
| 51 | + expect(handleLocalAuthenticationMock).toHaveBeenCalledWith(true); |
| 52 | + |
| 53 | + // changePasscode should NOT be called before the delay |
| 54 | + expect(changePasscodeMock).not.toHaveBeenCalled(); |
| 55 | + |
| 56 | + // advance timers and flush all promises |
| 57 | + jest.runAllTimers(); |
| 58 | + await promise; |
| 59 | + |
| 60 | + expect(changePasscodeMock).toHaveBeenCalledWith({ force: false }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should return early when authentication is cancelled (real component)', async () => { |
| 64 | + const handleLocalAuthenticationMock = (localAuthentication.handleLocalAuthentication as jest.Mock).mockRejectedValue(new Error('cancel')); |
| 65 | + const changePasscodeMock = (localAuthentication.changePasscode as jest.Mock).mockResolvedValue(undefined); |
| 66 | + |
| 67 | + const mockInstance: any = new (ScreenLockConfigView as any)({ theme: 'light', server: '', Force_Screen_Lock: false, Force_Screen_Lock_After: 0 }); |
| 68 | + mockInstance.state = { autoLock: true }; |
| 69 | + |
| 70 | + await mockInstance.changePasscode({ force: false }); expect(handleLocalAuthenticationMock).toHaveBeenCalledWith(true); |
| 71 | + expect(changePasscodeMock).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should proceed directly to passcode change when autoLock is disabled (real component)', async () => { |
| 75 | + const handleLocalAuthenticationMock = (localAuthentication.handleLocalAuthentication as jest.Mock); |
| 76 | + const changePasscodeMock = (localAuthentication.changePasscode as jest.Mock).mockResolvedValue(undefined); |
| 77 | + |
| 78 | + const mockInstance: any = new (ScreenLockConfigView as any)({ theme: 'light', server: '', Force_Screen_Lock: false, Force_Screen_Lock_After: 0 }); |
| 79 | + mockInstance.state = { autoLock: false }; |
| 80 | + |
| 81 | + await mockInstance.changePasscode({ force: false }); expect(handleLocalAuthenticationMock).not.toHaveBeenCalled(); |
| 82 | + expect(changePasscodeMock).toHaveBeenCalledWith({ force: false }); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
0 commit comments