Skip to content

Commit 77f4150

Browse files
committed
fix(react-providers): regression in isAbleToVote
1 parent b40b0e3 commit 77f4150

File tree

3 files changed

+76
-4
lines changed

3 files changed

+76
-4
lines changed

packages/react-providers/src/election/ElectionProvider.test.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,55 @@ describe('<ElectionProvider />', () => {
660660
localStorage.removeItem('csp_token')
661661
})
662662

663+
it('enables voting when CSP token is set after initial render and sign-info returns 401', async () => {
664+
jest.mocked(fetchSignInfo).mockRejectedValueOnce({ status: 401 })
665+
666+
const client = new VocdoniSDKClient({
667+
env: EnvOptions.STG,
668+
})
669+
670+
const census = new WeightedCensus()
671+
census.type = CensusType.CSP
672+
census.censusURI = 'https://csp.example/api'
673+
674+
// @ts-ignore
675+
const election = PublishedElection.build({
676+
id: 'csp-election-late-token',
677+
title: 'test',
678+
description: 'test',
679+
endDate: new Date(),
680+
census,
681+
electionType: { anonymous: false },
682+
voteType: { maxVoteOverwrites: 0, maxCount: 1, maxValue: 1 },
683+
})
684+
685+
const wrapper = (props: any) => {
686+
return (
687+
<TestProvider>
688+
<ClientProvider {...onlyProps(props)}>
689+
<ElectionProvider {...properProps(props)} />
690+
</ClientProvider>
691+
</TestProvider>
692+
)
693+
}
694+
695+
const { result } = renderHook(() => useElection(), {
696+
wrapper,
697+
initialProps: { election, fetchCensus: true, client },
698+
})
699+
700+
await act(async () => {
701+
result.current.actions.csp1('token')
702+
})
703+
704+
await waitFor(() => {
705+
expect(result.current.votesLeft).toBe(1)
706+
})
707+
708+
expect(result.current.isInCensus).toBeTruthy()
709+
expect(result.current.isAbleToVote).toBeTruthy()
710+
})
711+
663712
it('treats 401 on CSP sign info as no prior vote', async () => {
664713
localStorage.setItem('csp_token', 'token')
665714
jest.mocked(fetchSignInfo).mockRejectedValueOnce({ status: 401 })

packages/react-providers/src/election/use-election-provider.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,18 @@ export const useElectionProvider = ({
264264
await censusFetch()
265265
})()
266266
// eslint-disable-next-line react-hooks/exhaustive-deps
267-
}, [fetchCensus, client, state.voter, loaded.election, loading.census, actions, state.isAbleToVote, signature])
267+
}, [
268+
fetchCensus,
269+
client,
270+
client.wallet,
271+
state.voter,
272+
loaded.election,
273+
loading.census,
274+
actions,
275+
state.isAbleToVote,
276+
signature,
277+
state.csp.token,
278+
])
268279

269280
// csp check sign info (check if voter already voted)
270281
useEffect(() => {

packages/react-providers/src/election/use-election-reducer.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,17 @@ const electionReducer: Reducer<ElectionReducerState, ElectionAction> = (
220220
case ElectionCspStep1: {
221221
const token = action.payload as ElectionCspStep1Payload
222222
localStorage.setItem(LSKey.tokenR, token)
223-
return {
223+
const rstate = {
224224
...state,
225225
csp: {
226226
...state.csp,
227227
token,
228228
},
229229
}
230+
return {
231+
...rstate,
232+
isAbleToVote: isAbleToVote(rstate),
233+
}
230234
}
231235

232236
case ElectionLoad:
@@ -248,10 +252,14 @@ const electionReducer: Reducer<ElectionReducerState, ElectionAction> = (
248252

249253
case ElectionInCensus: {
250254
const isInCensus = action.payload as ElectionInCensusPayload
251-
return {
255+
const rstate = {
252256
...state,
253257
isInCensus,
254258
}
259+
return {
260+
...rstate,
261+
isAbleToVote: isAbleToVote(rstate),
262+
}
255263
}
256264

257265
case ElectionVoted: {
@@ -290,10 +298,14 @@ const electionReducer: Reducer<ElectionReducerState, ElectionAction> = (
290298

291299
case ElectionVotesLeft: {
292300
const votesLeft = action.payload as ElectionVotesLeftPayload
293-
return {
301+
const rstate = {
294302
...state,
295303
votesLeft,
296304
}
305+
return {
306+
...rstate,
307+
isAbleToVote: isAbleToVote(rstate),
308+
}
297309
}
298310

299311
case ElectionClientSet: {

0 commit comments

Comments
 (0)