|
| 1 | +import { describe, expect, it, jest } from '@jest/globals' |
| 2 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
| 3 | +import { fireEvent, render, waitFor } from '@testing-library/react-native' |
| 4 | +import * as React from 'react' |
| 5 | + |
| 6 | +import { SettingsScene } from '../../components/scenes/SettingsScene' |
| 7 | +import { FakeProviders, type FakeState } from '../../util/fake/FakeProviders' |
| 8 | +import { fakeEdgeAppSceneProps } from '../../util/fake/fakeSceneProps' |
| 9 | + |
| 10 | +// Stateful biometric backend, standing in for the keychain that |
| 11 | +// `edge-login-ui-rn` persists the "Use Biometrics" setting to. The `mock` |
| 12 | +// prefix is required for jest to allow referencing it inside the factory. |
| 13 | +// `read` is indirected so a test can hold a refetch in-flight; it defaults to |
| 14 | +// reading the persisted `enabled` flag. |
| 15 | +const mockBiometry: { |
| 16 | + enabled: boolean |
| 17 | + read: () => Promise<boolean> |
| 18 | +} = { |
| 19 | + enabled: true, |
| 20 | + read: async () => mockBiometry.enabled |
| 21 | +} |
| 22 | +jest.mock('edge-login-ui-rn', () => ({ |
| 23 | + getSupportedBiometryType: async () => 'FaceID', |
| 24 | + isTouchEnabled: async () => await mockBiometry.read(), |
| 25 | + enableTouchId: async () => { |
| 26 | + mockBiometry.enabled = true |
| 27 | + }, |
| 28 | + disableTouchId: async () => { |
| 29 | + mockBiometry.enabled = false |
| 30 | + } |
| 31 | +})) |
| 32 | + |
| 33 | +const ACCOUNT_ID = 'fake-account-id' |
| 34 | +const BIOMETRIC_QUERY_KEY = ['biometricState', ACCOUNT_ID] |
| 35 | + |
| 36 | +const mockState: FakeState = { |
| 37 | + core: { |
| 38 | + account: { |
| 39 | + id: ACCOUNT_ID, |
| 40 | + rootLoginId: 'XXX', |
| 41 | + currencyConfig: {}, |
| 42 | + username: 'some user', |
| 43 | + watch: () => () => {} |
| 44 | + }, |
| 45 | + context: { |
| 46 | + logSettings: { defaultLogLevel: 'silent' }, |
| 47 | + watch: () => () => {} |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +const getCachedTouchEnabled = (client: QueryClient): boolean | undefined => |
| 53 | + client.getQueryData<{ isTouchEnabled: boolean }>(BIOMETRIC_QUERY_KEY) |
| 54 | + ?.isTouchEnabled |
| 55 | + |
| 56 | +const renderSettings = (client: QueryClient): ReturnType<typeof render> => |
| 57 | + render( |
| 58 | + <FakeProviders initialState={mockState}> |
| 59 | + <QueryClientProvider client={client}> |
| 60 | + <SettingsScene |
| 61 | + {...fakeEdgeAppSceneProps('settingsOverview', undefined)} |
| 62 | + /> |
| 63 | + </QueryClientProvider> |
| 64 | + </FakeProviders> |
| 65 | + ) |
| 66 | + |
| 67 | +describe('SettingsScene biometric toggle persistence', () => { |
| 68 | + it('keeps the cached biometric state in sync after toggling so re-entry shows the set value', async () => { |
| 69 | + jest.useRealTimers() |
| 70 | + mockBiometry.enabled = true |
| 71 | + mockBiometry.read = async () => mockBiometry.enabled |
| 72 | + |
| 73 | + // A client we own and can inspect, shared across both scene mounts to |
| 74 | + // model the app-level query cache that survives a scene remount. Seed it |
| 75 | + // with the loaded biometric state so the toggle renders deterministically. |
| 76 | + const client = new QueryClient({ |
| 77 | + defaultOptions: { queries: { retry: false } } |
| 78 | + }) |
| 79 | + client.setQueryData(BIOMETRIC_QUERY_KEY, { |
| 80 | + isTouchEnabled: true, |
| 81 | + isTouchSupported: true, |
| 82 | + biometryType: 'FaceID' |
| 83 | + }) |
| 84 | + |
| 85 | + const rendered = renderSettings(client) |
| 86 | + |
| 87 | + // The toggle renders directly off the cached query value. |
| 88 | + await rendered.findByText('Use FaceID', {}, { timeout: 15000 }) |
| 89 | + expect(getCachedTouchEnabled(client)).toBe(true) |
| 90 | + |
| 91 | + // Toggle biometrics off. |
| 92 | + fireEvent.press(rendered.getByText('Use FaceID')) |
| 93 | + |
| 94 | + // The persisted backend flips off AND the query cache is kept in sync. |
| 95 | + // Before the fix the cache stayed `true`, so re-entering Settings showed |
| 96 | + // the stale (on) state. |
| 97 | + await waitFor( |
| 98 | + () => { |
| 99 | + expect(mockBiometry.enabled).toBe(false) |
| 100 | + expect(getCachedTouchEnabled(client)).toBe(false) |
| 101 | + }, |
| 102 | + { timeout: 15000 } |
| 103 | + ) |
| 104 | + |
| 105 | + rendered.unmount() |
| 106 | + |
| 107 | + // Re-enter Settings: a fresh mount reads the same (now-correct) cache and |
| 108 | + // shows the toggle in the state the user left it. |
| 109 | + const reentered = renderSettings(client) |
| 110 | + await reentered.findByText('Use FaceID', {}, { timeout: 15000 }) |
| 111 | + expect(getCachedTouchEnabled(client)).toBe(false) |
| 112 | + |
| 113 | + reentered.unmount() |
| 114 | + }, 60000) |
| 115 | + |
| 116 | + it('cancels an in-flight refetch so a stale read cannot overwrite a completed toggle', async () => { |
| 117 | + jest.useRealTimers() |
| 118 | + mockBiometry.enabled = true |
| 119 | + |
| 120 | + const client = new QueryClient({ |
| 121 | + defaultOptions: { queries: { retry: false } } |
| 122 | + }) |
| 123 | + // Seed the cache so the toggle renders immediately AND a background refetch |
| 124 | + // fires on mount (stale-while-revalidate). |
| 125 | + client.setQueryData(BIOMETRIC_QUERY_KEY, { |
| 126 | + isTouchEnabled: true, |
| 127 | + isTouchSupported: true, |
| 128 | + biometryType: 'FaceID' |
| 129 | + }) |
| 130 | + |
| 131 | + // Hold ONLY the first (mount) refetch in-flight, capturing its resolver so |
| 132 | + // we can complete it with the stale pre-toggle value after the toggle |
| 133 | + // persists. Any later refetch reads the real persisted flag. |
| 134 | + let resolveStaleRefetch: (value: boolean) => void = () => {} |
| 135 | + let signalStarted: () => void = () => {} |
| 136 | + const staleRefetchStarted = new Promise<void>(resolve => { |
| 137 | + signalStarted = resolve |
| 138 | + }) |
| 139 | + let firstRead = true |
| 140 | + mockBiometry.read = async () => { |
| 141 | + if (firstRead) { |
| 142 | + firstRead = false |
| 143 | + signalStarted() |
| 144 | + return await new Promise<boolean>(resolve => { |
| 145 | + resolveStaleRefetch = resolve |
| 146 | + }) |
| 147 | + } |
| 148 | + return mockBiometry.enabled |
| 149 | + } |
| 150 | + |
| 151 | + const rendered = renderSettings(client) |
| 152 | + |
| 153 | + // Toggle renders from the cached value while the mount refetch is pending. |
| 154 | + await rendered.findByText('Use FaceID', {}, { timeout: 15000 }) |
| 155 | + await staleRefetchStarted |
| 156 | + |
| 157 | + // Toggle biometrics off. handleUpdateTouchId cancels the in-flight refetch, |
| 158 | + // optimistically writes false, and persists. |
| 159 | + fireEvent.press(rendered.getByText('Use FaceID')) |
| 160 | + await waitFor( |
| 161 | + () => { |
| 162 | + expect(mockBiometry.enabled).toBe(false) |
| 163 | + expect(getCachedTouchEnabled(client)).toBe(false) |
| 164 | + }, |
| 165 | + { timeout: 15000 } |
| 166 | + ) |
| 167 | + |
| 168 | + // The stale mount refetch now resolves with the OLD (true) value. Because it |
| 169 | + // was cancelled, react-query must ignore it and leave the cache at false. |
| 170 | + // Without the cancelQueries guard this write flips the toggle back on. |
| 171 | + resolveStaleRefetch(true) |
| 172 | + await new Promise(resolve => setTimeout(resolve, 100)) |
| 173 | + expect(getCachedTouchEnabled(client)).toBe(false) |
| 174 | + |
| 175 | + rendered.unmount() |
| 176 | + }, 60000) |
| 177 | +}) |
0 commit comments