Browse Source

fix(photo-upload): stop wiping stored answer on preview/upload failure — matches pre-today behavior

master
parent
commit
e55b6a33e7
  1. 31
      src/components/Componentes/question-photo.test.tsx
  2. 18
      src/components/Componentes/question-photo.tsx

31
src/components/Componentes/question-photo.test.tsx

@ -169,27 +169,23 @@ describe("QuestionPhoto Component", () => {
// Error toast should appear // Error toast should appear
await waitFor(() => { await waitFor(() => {
// Toast message should appear
expect(screen.getByText("Photo upload failed. Please try again.")).toBeInTheDocument(); 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 // File input is reset allowing re-upload
expect(fileInput.value).toBe(""); expect(fileInput.value).toBe("");
});
} finally { } finally {
URL.createObjectURL = originalCreateObjectURL; URL.createObjectURL = originalCreateObjectURL;
URL.revokeObjectURL = originalRevokeObjectURL; 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"; answerMap[mockPhotoQuestion.id] = "https://example.com/broken-avatar.jpg";
const { container } = render(<QuestionPhoto question={mockPhotoQuestion} />); const { container } = render(<QuestionPhoto question={mockPhotoQuestion} />);
@ -199,15 +195,12 @@ describe("QuestionPhoto Component", () => {
// Trigger image error (e.g. 404 / ERR_CONNECTION_REFUSED) // Trigger image error (e.g. 404 / ERR_CONNECTION_REFUSED)
fireEvent.error(img); fireEvent.error(img);
await waitFor(() => {
expect(screen.getByText("Failed to load image. Please try uploading again.")).toBeInTheDocument();
});
// 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);
// Answer value cleared
expect(mockSetAnswerValue).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();
}); });
}); });

18
src/components/Componentes/question-photo.tsx

@ -81,8 +81,6 @@ export function QuestionPhoto({
activeBlobUrlRef.current = null; activeBlobUrlRef.current = null;
} }
setLocalPreviewUrl(null); setLocalPreviewUrl(null);
setAnswerValue(question, null);
setHasImageError(true);
if (fileInputRef.current) { if (fileInputRef.current) {
fileInputRef.current.value = ""; fileInputRef.current.value = "";
} }
@ -92,7 +90,7 @@ export function QuestionPhoto({
"Photo upload failed. Please try again.", "Photo upload failed. Please try again.",
); );
}, },
[question, setAnswerValue, t],
[t],
); );
const handleImageError = useCallback(() => { const handleImageError = useCallback(() => {
@ -102,16 +100,10 @@ export function QuestionPhoto({
activeBlobUrlRef.current = null; activeBlobUrlRef.current = null;
} }
setLocalPreviewUrl(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({ const uploadTmpMediaMutation = useUploadTmpMediaMutation({
onSuccess: (response) => { onSuccess: (response) => {

Loading…
Cancel
Save