mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-16 11:33:07 +00:00
Staff can find state deputies by name or party with municipality counts in the global Início search, mirroring the B49/B50 loader and UI pattern.
84 lines
3.3 KiB
TypeScript
84 lines
3.3 KiB
TypeScript
// @vitest-environment node
|
|
|
|
import type { Payload } from 'payload'
|
|
import { getPayload } from 'payload'
|
|
import { beforeAll, describe, expect, it } from 'vitest'
|
|
|
|
import { setStateDeputyMunicipalitiesBatchRecord } from '@/app/(campaign)/campanha/actions/stateDeputy'
|
|
import config from '@/payload.config'
|
|
import { searchHomeStateDeputies } from '@/utilities/homeSearch/searchHomeStateDeputies'
|
|
|
|
import { installCampaignFixtures } from '../helpers/campaignFixtures'
|
|
|
|
let payload: Payload
|
|
const campaignFixtures = installCampaignFixtures({
|
|
getPayload: () => payload,
|
|
setPayload: (nextPayload) => {
|
|
payload = nextPayload
|
|
},
|
|
})
|
|
|
|
describe('searchHomeStateDeputies (B52)', () => {
|
|
beforeAll(async () => {
|
|
payload = await getPayload({ config: await config })
|
|
})
|
|
|
|
it('returns empty hits when query is shorter than the minimum', async () => {
|
|
const fixtures = campaignFixtures()
|
|
const coordinator = await fixtures.createCampaignUser('coordinator')
|
|
|
|
const result = await searchHomeStateDeputies(payload, coordinator, 'a')
|
|
expect(result).toEqual([])
|
|
})
|
|
|
|
it('matches state deputies by word-start on the name for staff', async () => {
|
|
const fixtures = campaignFixtures()
|
|
const coordinator = await fixtures.createCampaignUser('coordinator')
|
|
const stateDeputy = await fixtures.createStateDeputy({ name: 'Zeca Oliveira Dobradinha' })
|
|
|
|
const result = await searchHomeStateDeputies(payload, coordinator, 'Zeca')
|
|
expect(result.some((hit) => hit.slug === stateDeputy.slug)).toBe(true)
|
|
expect(result.find((hit) => hit.slug === stateDeputy.slug)?.name).toBe(
|
|
'Zeca Oliveira Dobradinha',
|
|
)
|
|
})
|
|
|
|
it('matches state deputies by word-start on the party when the name does not match', async () => {
|
|
const fixtures = campaignFixtures()
|
|
const coordinator = await fixtures.createCampaignUser('coordinator')
|
|
const stateDeputy = await fixtures.createStateDeputy({
|
|
name: 'Maria Silva',
|
|
party: 'PSOL',
|
|
})
|
|
|
|
const result = await searchHomeStateDeputies(payload, coordinator, 'PSOL')
|
|
expect(result.some((hit) => hit.slug === stateDeputy.slug)).toBe(true)
|
|
})
|
|
|
|
it('returns state deputy hits for advisor role when name matches', async () => {
|
|
const fixtures = campaignFixtures()
|
|
const advisor = await fixtures.createCampaignUser('advisor')
|
|
const stateDeputy = await fixtures.createStateDeputy({ name: 'Assessoria Dobradinha' })
|
|
|
|
const result = await searchHomeStateDeputies(payload, advisor, 'Assessoria')
|
|
expect(result.some((hit) => hit.slug === stateDeputy.slug)).toBe(true)
|
|
})
|
|
|
|
it('reports municipality count on matched state deputies', async () => {
|
|
const fixtures = campaignFixtures()
|
|
const coordinator = await fixtures.createCampaignUser('coordinator')
|
|
const stateDeputy = await fixtures.createStateDeputy({ name: 'Carteira Dobradinha' })
|
|
const cairu = await fixtures.getMunicipality('cairu')
|
|
const feira = await fixtures.getMunicipality('feira-de-santana')
|
|
await setStateDeputyMunicipalitiesBatchRecord(payload, coordinator, {
|
|
stateDeputyId: stateDeputy.id,
|
|
municipalityIds: [cairu.id, feira.id],
|
|
assigned: true,
|
|
})
|
|
|
|
const result = await searchHomeStateDeputies(payload, coordinator, 'Carteira')
|
|
const hit = result.find((row) => row.slug === stateDeputy.slug)
|
|
expect(hit?.municipalityCount).toBe(2)
|
|
expect(hit?.party).toBe(stateDeputy.party ?? null)
|
|
})
|
|
})
|