From e55b6a33e761aeabaaaa3ae9843ef45e2e1bcfee Mon Sep 17 00:00:00 2001 From: "Muhammad A. Ghorbani" Date: Sat, 12 Sep 2026 20:29:40 +0330 Subject: [PATCH] =?UTF-8?q?fix(photo-upload):=20stop=20wiping=20stored=20a?= =?UTF-8?q?nswer=20on=20preview/upload=20failure=20=E2=80=94=20matches=20p?= =?UTF-8?q?re-today=20behavior?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Componentes/question-photo.test.tsx | 35 ++++++++----------- src/components/Componentes/question-photo.tsx | 18 +++------- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/src/components/Componentes/question-photo.test.tsx b/src/components/Componentes/question-photo.test.tsx index 1be4f97..96dc452 100644 --- a/src/components/Componentes/question-photo.test.tsx +++ b/src/components/Componentes/question-photo.test.tsx @@ -169,27 +169,23 @@ describe("QuestionPhoto Component", () => { // Error toast should appear await waitFor(() => { + // Toast message should appear expect(screen.getByText("Photo upload failed. Please try again.")).toBeInTheDocument(); - }); - - // Answer value is cleared - expect(mockSetAnswerValue).toHaveBeenCalledWith(mockPhotoQuestion, null); - - // Default avatar SVG placeholder is restored - expect(container.querySelector("img[src*='Frame 2095586679.svg']")).toBeInTheDocument(); - // Camera badge is not visible - expect(container.querySelector(".bg-\\[\\#F0445B\\]")).toBeNull(); + // Answer value is NOT cleared — matches yesterday's behavior + // (error just shows toast, does not wipe the form state) + expect(mockSetAnswerValue).not.toHaveBeenCalledWith(mockPhotoQuestion, null); - // File input is reset allowing re-upload - expect(fileInput.value).toBe(""); + // File input is reset allowing re-upload + expect(fileInput.value).toBe(""); + }); } finally { URL.createObjectURL = originalCreateObjectURL; URL.revokeObjectURL = originalRevokeObjectURL; } }); - it("shows error toast and reverts to default avatar when rendered image fails to load", async () => { + it("keeps stored answer when rendered image fails to load (preview-only failure)", async () => { answerMap[mockPhotoQuestion.id] = "https://example.com/broken-avatar.jpg"; const { container } = render(); @@ -199,15 +195,12 @@ describe("QuestionPhoto Component", () => { // Trigger image error (e.g. 404 / ERR_CONNECTION_REFUSED) fireEvent.error(img); - await waitFor(() => { - expect(screen.getByText("Failed to load image. Please try uploading again.")).toBeInTheDocument(); - }); - - // Answer value cleared - expect(mockSetAnswerValue).toHaveBeenCalledWith(mockPhotoQuestion, null); + // The stored answer should NOT be cleared — the file is already on the server. + // This matches yesterday's behavior where preview errors never wiped the answer. + expect(mockSetAnswerValue).not.toHaveBeenCalledWith(mockPhotoQuestion, null); - // Default avatar placeholder rendered instead of broken image - expect(container.querySelector("img[src*='Frame 2095586679.svg']")).toBeInTheDocument(); - expect(container.querySelector(".bg-\\[\\#F0445B\\]")).toBeNull(); + // The component should remain in "answered" state via data-question-answered + // because storedValue still holds the uploaded path + expect(container.querySelector("[data-question-answered='true']")).toBeInTheDocument(); }); }); diff --git a/src/components/Componentes/question-photo.tsx b/src/components/Componentes/question-photo.tsx index ec5d301..8c6a778 100644 --- a/src/components/Componentes/question-photo.tsx +++ b/src/components/Componentes/question-photo.tsx @@ -81,8 +81,6 @@ export function QuestionPhoto({ activeBlobUrlRef.current = null; } setLocalPreviewUrl(null); - setAnswerValue(question, null); - setHasImageError(true); if (fileInputRef.current) { fileInputRef.current.value = ""; } @@ -92,7 +90,7 @@ export function QuestionPhoto({ "Photo upload failed. Please try again.", ); }, - [question, setAnswerValue, t], + [t], ); const handleImageError = useCallback(() => { @@ -102,16 +100,10 @@ export function QuestionPhoto({ activeBlobUrlRef.current = null; } setLocalPreviewUrl(null); - setAnswerValue(question, null); - setHasImageError(true); - if (fileInputRef.current) { - fileInputRef.current.value = ""; - } - setToastMessage( - t["Failed to load image. Please try uploading again."] ?? - "Failed to load image. Please try uploading again.", - ); - }, [question, setAnswerValue, t]); + // Do NOT clear the stored answer — the file was uploaded successfully, + // only the preview failed to load (proxy timing, network glitch, etc.). + // This matches yesterday's behavior where errors never wiped the answer. + }, []); const uploadTmpMediaMutation = useUploadTmpMediaMutation({ onSuccess: (response) => {