fix(native): the native question reads no ref while rendering, and its tests type and lint clean

This commit is contained in:
Maurice
2026-11-09 09:16:58 +01:00
parent d06168d94a
commit c9a91bbdad
4 changed files with 20 additions and 12 deletions
+1 -1
View File
@@ -3,7 +3,7 @@ import { prepareField, prepareKeyboardFields } from './keyboardFields';
function field(html: string): HTMLInputElement {
document.body.innerHTML = html;
return document.body.querySelector('input')!;
return document.body.querySelector('input') as HTMLInputElement;
}
afterEach(() => {
+3 -3
View File
@@ -34,7 +34,7 @@ describe('canAskNatively', () => {
describe('askNatively', () => {
it('asks a destroying question on a sheet with the action in red, yes only for that action', async () => {
vi.mocked(ActionSheet.showActions).mockResolvedValue({ index: 0 });
vi.mocked(ActionSheet.showActions).mockResolvedValue({ index: 0, canceled: false });
expect(await askNatively({ ...question, danger: true })).toBe(true);
expect(ActionSheet.showActions).toHaveBeenCalledWith({
title: 'Delete place?',
@@ -44,9 +44,9 @@ describe('askNatively', () => {
{ title: 'Cancel', style: 'CANCEL' },
],
});
vi.mocked(ActionSheet.showActions).mockResolvedValue({ index: 1 });
vi.mocked(ActionSheet.showActions).mockResolvedValue({ index: 1, canceled: false });
expect(await askNatively({ ...question, danger: true })).toBe(false);
vi.mocked(ActionSheet.showActions).mockResolvedValue({ index: -1 });
vi.mocked(ActionSheet.showActions).mockResolvedValue({ index: -1, canceled: true });
expect(await askNatively({ ...question, danger: true })).toBe(false);
});
+9 -2
View File
@@ -15,15 +15,22 @@ export function useNativeQuestion(
): boolean {
const native = question !== null && canAskNatively();
const [failed, setFailed] = useState(false);
// A closed question forgets that the system failed it, so the next one is asked natively again.
const [wasOpen, setWasOpen] = useState(open);
if (open !== wasOpen) {
setWasOpen(open);
if (!open) setFailed(false);
}
// Asked once per opening, also when StrictMode runs the effect twice.
const asked = useRef(false);
const latest = useRef({ question, answer });
latest.current = { question, answer };
useEffect(() => {
latest.current = { question, answer };
});
useEffect(() => {
if (!open) {
asked.current = false;
setFailed(false);
return;
}
if (!native || asked.current || !latest.current.question) return;