From 80c9223355b2ddab93e85913e7a4831a59dbde57 Mon Sep 17 00:00:00 2001 From: KingFRANKHOOD Date: Sun, 30 Aug 2026 08:55:59 +0100 Subject: [PATCH] test(keys): add unit tests for vote modal option index and submission states Add unit tests covering the VoteModal acceptance criteria: - Correct option index passed to the contract for each selectable option - Confirm button disabled until an option is selected - Confirm button disabled and 'Vote recorded' shown after a successful vote - Error message shown on contract failure and Confirm re-enabled Also fix VoteModal handleSubmit to catch mutation rejections and render an in-modal error message on contract failure. --- components/keys/VoteModal.tsx | 30 +++- components/keys/__tests__/VoteModal.test.tsx | 147 +++++++++++++++++++ 2 files changed, 170 insertions(+), 7 deletions(-) create mode 100644 components/keys/__tests__/VoteModal.test.tsx diff --git a/components/keys/VoteModal.tsx b/components/keys/VoteModal.tsx index 09ef691..60dccf5 100644 --- a/components/keys/VoteModal.tsx +++ b/components/keys/VoteModal.tsx @@ -44,13 +44,18 @@ export function VoteModal({ const handleSubmit = async () => { if (!address || selectedOption === null) return; - await voteMutation.mutateAsync({ - proposalId: proposal.id, - optionIndex: selectedOption, - walletAddress: address, - token: jwt, - }); - setRecorded(true); + try { + await voteMutation.mutateAsync({ + proposalId: proposal.id, + optionIndex: selectedOption, + walletAddress: address, + token: jwt, + }); + setRecorded(true); + } catch { + // react-query exposes the rejection as voteMutation.error so the error + // message below can be rendered; nothing else to do here. + } }; return ( @@ -109,6 +114,17 @@ export function VoteModal({

)} + {voteMutation.isError && ( +

+ {voteMutation.error instanceof Error + ? voteMutation.error.message + : "Vote submission failed. Please try again."} +

+ )} +