|
| 1 | +import type { UserInputQuestion } from "@t3tools/contracts"; |
| 2 | +import type { ReactNode } from "react"; |
| 3 | +import { act } from "react"; |
| 4 | +import { createRoot, type Root } from "react-dom/client"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 6 | + |
| 7 | +type InputProps = { value: string; onChangeText: (text: string) => void }; |
| 8 | + |
| 9 | +const field = vi.hoisted(() => ({ props: null as InputProps | null })); |
| 10 | + |
| 11 | +vi.mock("react-native", () => ({ |
| 12 | + View: ({ children }: { children: ReactNode }) => <div>{children}</div>, |
| 13 | +})); |
| 14 | +vi.mock("../../components/ui/AppText", () => ({ |
| 15 | + AppTextInput: (props: InputProps) => { |
| 16 | + field.props = props; |
| 17 | + return null; |
| 18 | + }, |
| 19 | +})); |
| 20 | +vi.mock("expo-paste-input", () => ({ |
| 21 | + TextInputWrapper: ({ children }: { children: ReactNode }) => <>{children}</>, |
| 22 | +})); |
| 23 | +vi.mock("@effect/atom-react", () => ({ useAtomValue: () => ({}) })); |
| 24 | +vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({ navigate: vi.fn() }) })); |
| 25 | +vi.mock("@t3tools/client-runtime/state/runtime", () => ({ |
| 26 | + settlePromise: vi.fn(), |
| 27 | + squashAtomCommandFailure: vi.fn(), |
| 28 | +})); |
| 29 | +vi.mock("../../components/ui/Dialog", () => ({ alert: vi.fn() })); |
| 30 | +vi.mock("../../lib/useNativePaste", () => ({ useNativePaste: () => vi.fn() })); |
| 31 | +vi.mock("../../lib/composerImages", () => ({ |
| 32 | + convertPastedImagesToAttachments: vi.fn(), |
| 33 | + pickComposerFiles: vi.fn(), |
| 34 | + pickComposerMedia: vi.fn(), |
| 35 | +})); |
| 36 | +vi.mock("./ComposerAttachmentButton", () => ({ ComposerAttachmentButton: () => null })); |
| 37 | +vi.mock("../../components/ComposerAttachmentStrip", () => ({ |
| 38 | + ComposerAttachmentStrip: () => null, |
| 39 | +})); |
| 40 | +vi.mock("../../components/FilePreviewModal", () => ({ FilePreviewModal: () => null })); |
| 41 | +vi.mock("../../components/VideoPreviewModal", () => ({ VideoPreviewModal: () => null })); |
| 42 | +vi.mock("../../state/use-thread-selection", () => ({ |
| 43 | + useThreadSelection: () => ({ selectedThread: { environmentId: "env-1", id: "thread-1" } }), |
| 44 | +})); |
| 45 | +vi.mock("../../state/entities", () => ({ useServerConfigs: () => new Map() })); |
| 46 | +vi.mock("../../state/atom-registry", () => ({ appAtomRegistry: { get: () => ({}) } })); |
| 47 | +vi.mock("../../state/use-composer-drafts", () => ({ |
| 48 | + appendComposerDraftAttachments: vi.fn(), |
| 49 | + composerDraftsAtom: {}, |
| 50 | + removeComposerDraftAttachment: vi.fn(), |
| 51 | + releaseUnusedComposerAttachmentFiles: vi.fn(), |
| 52 | +})); |
| 53 | +vi.mock("../../state/question-attachments", () => ({ |
| 54 | + changeQuestionAttachmentPreparation: vi.fn(), |
| 55 | + questionAttachmentDraftKey: () => "draft-key", |
| 56 | + questionAttachmentPreparationAtom: {}, |
| 57 | +})); |
| 58 | + |
| 59 | +import { QuestionAttachments } from "./QuestionAttachments"; |
| 60 | + |
| 61 | +const question = { |
| 62 | + id: "colour", |
| 63 | + header: "Colour", |
| 64 | + question: "Which colour?", |
| 65 | + options: [{ label: "Blue", description: "Blue" }], |
| 66 | + multiSelect: false, |
| 67 | +} as unknown as UserInputQuestion; |
| 68 | + |
| 69 | +let root: Root; |
| 70 | +const onChangeText = vi.fn(); |
| 71 | + |
| 72 | +// `draft` is what the thread screen passes back; it lags the input until that screen re-renders. |
| 73 | +function render(draft: string) { |
| 74 | + act(() => |
| 75 | + root.render( |
| 76 | + <QuestionAttachments |
| 77 | + requestId="request-1" |
| 78 | + question={question} |
| 79 | + questions={[question]} |
| 80 | + disabled={false} |
| 81 | + value={draft} |
| 82 | + onChangeText={onChangeText} |
| 83 | + />, |
| 84 | + ), |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +function input() { |
| 89 | + if (!field.props) throw new Error("answer field not rendered"); |
| 90 | + return field.props; |
| 91 | +} |
| 92 | + |
| 93 | +function type(text: string) { |
| 94 | + act(() => input().onChangeText(text)); |
| 95 | +} |
| 96 | + |
| 97 | +beforeEach(() => { |
| 98 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 99 | + root = createRoot(document.createElement("div")); |
| 100 | + field.props = null; |
| 101 | + onChangeText.mockClear(); |
| 102 | +}); |
| 103 | + |
| 104 | +afterEach(() => { |
| 105 | + act(() => root.unmount()); |
| 106 | +}); |
| 107 | + |
| 108 | +describe("QuestionAttachments answer field", () => { |
| 109 | + it("keeps newer typing while the thread screen still echoes the previous draft", () => { |
| 110 | + render("de"); |
| 111 | + type("de "); |
| 112 | + |
| 113 | + expect(onChangeText).toHaveBeenLastCalledWith("de "); |
| 114 | + expect(input().value).toBe("de "); |
| 115 | + |
| 116 | + render("de "); |
| 117 | + expect(input().value).toBe("de "); |
| 118 | + }); |
| 119 | + |
| 120 | + it("shows the draft an option tap cleared", () => { |
| 121 | + render(""); |
| 122 | + type("my own colour"); |
| 123 | + render("my own colour"); |
| 124 | + |
| 125 | + render(""); |
| 126 | + |
| 127 | + expect(input().value).toBe(""); |
| 128 | + }); |
| 129 | +}); |
0 commit comments