Skip to content

Commit

Permalink
1945: add unit test for card initialization with card params
Browse files Browse the repository at this point in the history
  • Loading branch information
f1sh1918 committed Feb 26, 2025
1 parent 4d9294d commit aea1fc0
Showing 1 changed file with 108 additions and 5 deletions.
113 changes: 108 additions & 5 deletions administration/src/bp-modules/cards/hooks/useCardGenerator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,44 @@ import createCards, { CreateCardsError, CreateCardsResult } from '../../../cards
import deleteCards from '../../../cards/deleteCards'
import { DynamicActivationCode, StaticVerificationCode } from '../../../generated/card_pb'
import { Region } from '../../../generated/graphql'
import { ProjectConfigProvider } from '../../../project-configs/ProjectConfigContext'
import bayernConfig from '../../../project-configs/bayern/config'
import { ProjectConfig } from '../../../project-configs/getProjectConfig'
import koblenzConfig from '../../../project-configs/koblenz/config'
import nuernbergConfig from '../../../project-configs/nuernberg/config'
import showcaseConfig from '../../../project-configs/showcase/config'
import downloadDataUri from '../../../util/downloadDataUri'
import { AppToasterProvider } from '../../AppToaster'
import useCardGenerator from './useCardGenerator'

const wrapper = ({ children }: { children: ReactNode }) => (
<MemoryRouter>
<AppToasterProvider>
<ApolloProvider>{children}</ApolloProvider>
</AppToasterProvider>
const wrapper = ({
children,
initialRoutes,
projectConfig,
}: {
children: ReactNode
projectConfig?: ProjectConfig
initialRoutes?: string[]
}) => (
<MemoryRouter initialEntries={initialRoutes}>
<ProjectConfigProvider projectConfig={projectConfig ?? showcaseConfig}>
<AppToasterProvider>
<ApolloProvider>{children}</ApolloProvider>
</AppToasterProvider>
</ProjectConfigProvider>
</MemoryRouter>
)

const withCustomWrapper =
(initialRoutes: string[], projectConfig: ProjectConfig) =>
// @ts-expect-error any is okay here
({ children }) =>
wrapper({
children,
initialRoutes,
projectConfig,
})

jest.mock('../../../cards/PdfFactory', () => ({
...jest.requireActual('../../../cards/PdfFactory'),
generatePdf: jest.fn(),
Expand Down Expand Up @@ -134,4 +159,82 @@ describe('useCardGenerator', () => {
expect(result.current.cardGenerationStep).toBe('input')
expect(result.current.cards).toEqual([])
})

it('should successfully initialize cards with searchParams for bavaria', async () => {
mocked(createCards).mockReturnValueOnce(Promise.resolve(codes))
const { result } = renderHook(() => useCardGenerator({ region }), {
wrapper: withCustomWrapper(
[
'?Name=Thea+Test&Ablaufdatum=26.02.2028&MailNotification=thea.test%40gmail.com&applicationIdToMarkAsProcessed=1',
],
bayernConfig
),
})

expect(result.current.cards).toEqual([
{
expirationDate: { day: 26, isoMonth: 2, isoYear: 2028 },
extensions: { bavariaCardType: 'Standard', regionId: 0, emailNotification: '[email protected]' },
fullName: 'Thea Test',
id: expect.any(Number),
},
])
})

it('should successfully initialize cards with searchParams for koblenz', async () => {
mocked(createCards).mockReturnValueOnce(Promise.resolve(codes))
const { result } = renderHook(() => useCardGenerator({ region }), {
wrapper: withCustomWrapper(['?Name=Karla Koblenz&Referenznummer=123K&Geburtsdatum=10.06.2003'], koblenzConfig),
})

expect(result.current.cards).toEqual([
{
expirationDate: { day: 26, isoMonth: 2, isoYear: 2026 },
extensions: {
birthday: {
day: 10,
isoMonth: 6,
isoYear: 2003,
},
koblenzReferenceNumber: '123K',
},
fullName: 'Karla Koblenz',
id: expect.any(Number),
},
])
})

it('should successfully initialize cards with searchParams for nuernberg', async () => {
mocked(createCards).mockReturnValueOnce(Promise.resolve(codes))
const { result } = renderHook(() => useCardGenerator({ region }), {
wrapper: withCustomWrapper(
[
'?Name=Thea+Test&Ablaufdatum=03.3.2026&Geburtsdatum=01.01.2000&Passnummer=12345678&Pass-ID=123&Adresszeile+1=Teststraße+3&Adresszeile+2=EG+Rechts&PLZ=86111&Ort=Musterstadt',
],
nuernbergConfig
),
})

expect(result.current.cards).toEqual([
{
expirationDate: { day: 3, isoMonth: 3, isoYear: 2026 },
extensions: {
birthday: {
day: 1,
isoMonth: 1,
isoYear: 2000,
},
addressLine1: 'Teststraße 3',
addressLine2: 'EG Rechts',
addressLocation: 'Musterstadt',
addressPlz: '86111',
nuernbergPassId: 123,
regionId: 0,
startDay: { day: 26, isoMonth: 2, isoYear: 2025 },
},
fullName: 'Thea Test',
id: expect.any(Number),
},
])
})
})

0 comments on commit aea1fc0

Please sign in to comment.