teqo-backup/tests/unit/wizardActionChain.unit.spec.ts
Cursor Agent 0bbb956a46
B135 — Wizard Voltar aponta ao passo anterior na cadeia
Adiciona wizardChainPreviousHref simétrico ao continue da B98 e
atualiza os quatro passos raiz (votos, tendência, sinal, liderança).
Sub-passos intra-fluxo permanecem inalterados.

Co-authored-by: Francisco Solla <fsolla@users.noreply.github.com>
2026-08-02 21:49:12 +00:00

128 lines
5.3 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { CAMPAIGN_ACTIONS_HOME } from '@/lib/campaignActionRoutes'
import { CAMPAIGN_HOME } from '@/lib/campaignPaths'
import {
isWizardChainActionId,
nextWizardChainStep,
previousWizardChainStep,
resolveWizardChainEntry,
wizardChainAfter,
wizardChainContinueHref,
wizardChainPreviousHref,
wizardChainSessionSteps,
} from '@/lib/wizardActionChain'
describe('wizardActionChain', () => {
it('excludes register-demand from the chainable set', () => {
expect(isWizardChainActionId('update-votes')).toBe(true)
expect(isWizardChainActionId('register-demand')).toBe(false)
expect(isWizardChainActionId(undefined)).toBe(false)
})
it('returns the full queue after the principal action', () => {
expect(wizardChainAfter('update-votes')).toEqual([
'change-trend',
'register-signal',
'update-leadership',
])
expect(wizardChainAfter('update-votes', 'update-votes')).toEqual([
'change-trend',
'register-signal',
'update-leadership',
])
expect(wizardChainAfter('register-signal', 'register-signal')).toEqual([
'change-trend',
'update-votes',
'update-leadership',
])
expect(wizardChainAfter('change-trend', 'change-trend')).toEqual([
'register-signal',
'update-votes',
'update-leadership',
])
expect(wizardChainAfter('update-leadership', 'update-leadership')).toEqual([
'register-signal',
'update-votes',
'change-trend',
])
})
it('slices the remaining queue after a chained step', () => {
expect(wizardChainAfter('update-votes', 'change-trend')).toEqual([
'register-signal',
'update-leadership',
])
expect(wizardChainAfter('update-votes', 'register-signal')).toEqual(['update-leadership'])
expect(wizardChainAfter('update-votes', 'update-leadership')).toEqual([])
expect(nextWizardChainStep('change-trend', 'register-signal')).toBe('update-votes')
expect(nextWizardChainStep('update-leadership', 'change-trend')).toBeUndefined()
})
it('builds continue hrefs with entry + municipio, or Início at the end', () => {
expect(wizardChainContinueHref('update-votes', 'update-votes', 'cairu')).toBe(
`${CAMPAIGN_ACTIONS_HOME}/mudar-tendencia?municipio=cairu&entry=update-votes`,
)
expect(wizardChainContinueHref('update-votes', 'change-trend', 'cairu')).toBe(
`${CAMPAIGN_ACTIONS_HOME}/registrar-sinal?municipio=cairu&entry=update-votes`,
)
expect(wizardChainContinueHref('register-signal', 'change-trend', 'cairu')).toBe(
`${CAMPAIGN_ACTIONS_HOME}/atualizar-votos?municipio=cairu&entry=register-signal`,
)
expect(wizardChainContinueHref('change-trend', 'update-leadership', 'cairu')).toBe(
CAMPAIGN_HOME,
)
expect(wizardChainContinueHref('register-demand', 'update-votes', 'cairu')).toBe(CAMPAIGN_HOME)
})
it('honors allowlisted return path when the chain ends', () => {
const origin = '/campanha/municipios/cairu'
expect(wizardChainContinueHref('change-trend', 'update-leadership', 'cairu', origin)).toBe(
origin,
)
expect(wizardChainContinueHref('update-votes', 'change-trend', 'cairu', origin)).toBe(
`${CAMPAIGN_ACTIONS_HOME}/registrar-sinal?municipio=cairu&entry=update-votes&from=${encodeURIComponent(origin)}`,
)
})
it('resolves session entry from query or current principal', () => {
expect(resolveWizardChainEntry(undefined, 'update-votes')).toBe('update-votes')
expect(resolveWizardChainEntry('register-signal', 'update-votes')).toBe('register-signal')
expect(resolveWizardChainEntry('register-demand', 'change-trend')).toBe('change-trend')
})
it('lists session steps as principal then queue', () => {
expect(wizardChainSessionSteps('update-votes')).toEqual([
'update-votes',
'change-trend',
'register-signal',
'update-leadership',
])
})
it('resolves the previous step in a session', () => {
expect(previousWizardChainStep('update-votes', 'update-votes')).toBe('municipality-search')
expect(previousWizardChainStep('update-votes', 'change-trend')).toBe('update-votes')
expect(previousWizardChainStep('update-votes', 'register-signal')).toBe('change-trend')
expect(previousWizardChainStep('register-signal', 'update-votes')).toBe('change-trend')
})
it('builds previous hrefs for principal and chained steps', () => {
expect(wizardChainPreviousHref(undefined, 'update-votes', 'cairu')).toBe(
`${CAMPAIGN_ACTIONS_HOME}/atualizar-votos?entry=update-votes`,
)
expect(wizardChainPreviousHref('update-votes', 'change-trend', 'cairu')).toBe(
`${CAMPAIGN_ACTIONS_HOME}/atualizar-votos?municipio=cairu&entry=update-votes`,
)
expect(wizardChainPreviousHref('update-votes', 'register-signal', 'cairu')).toBe(
`${CAMPAIGN_ACTIONS_HOME}/mudar-tendencia?municipio=cairu&entry=update-votes`,
)
expect(wizardChainPreviousHref('update-votes', 'update-leadership', 'cairu')).toBe(
`${CAMPAIGN_ACTIONS_HOME}/registrar-sinal?municipio=cairu&entry=update-votes`,
)
const origin = '/campanha/municipios/cairu'
expect(wizardChainPreviousHref('update-votes', 'change-trend', 'cairu', origin)).toBe(
`${CAMPAIGN_ACTIONS_HOME}/atualizar-votos?municipio=cairu&entry=update-votes&from=${encodeURIComponent(origin)}`,
)
})
})