Skip to content

Commit

Permalink
1945: move koblenz test to the correct hook. add minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
f1sh1918 committed Feb 28, 2025
1 parent aea1fc0 commit 12ef720
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 59 deletions.
69 changes: 20 additions & 49 deletions administration/src/bp-modules/cards/hooks/useCardGenerator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@ 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'

jest.useFakeTimers({ now: new Date('2025-01-01T00:00:00.000Z') })
jest.mock('../../../cards/PdfFactory', () => ({
...jest.requireActual('../../../cards/PdfFactory'),
generatePdf: jest.fn(),
}))
jest.mock('../../../cards/createCards', () => ({
...jest.requireActual('../../../cards/createCards'),
__esModule: true,
default: jest.fn(),
}))
jest.mock('../../../cards/deleteCards')
jest.mock('../../../util/downloadDataUri')
const wrapper = ({
children,
initialRoutes,
Expand All @@ -40,27 +51,14 @@ const wrapper = ({
)

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

jest.mock('../../../cards/PdfFactory', () => ({
...jest.requireActual('../../../cards/PdfFactory'),
generatePdf: jest.fn(),
}))
jest.mock('../../../cards/createCards', () => ({
...jest.requireActual('../../../cards/createCards'),
__esModule: true,
default: jest.fn(),
}))
jest.mock('../../../cards/deleteCards')
jest.mock('../../../util/downloadDataUri')

describe('useCardGenerator', () => {
const region: Region = {
id: 0,
Expand Down Expand Up @@ -164,10 +162,8 @@ describe('useCardGenerator', () => {
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
bayernConfig,
'?Name=Thea+Test&Ablaufdatum=26.02.2028&MailNotification=thea.test%40gmail.com&applicationIdToMarkAsProcessed=1'
),
})

Expand All @@ -181,37 +177,12 @@ describe('useCardGenerator', () => {
])
})

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
nuernbergConfig,
'?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&Startdatum=01.05.2025'
),
})

Expand All @@ -230,7 +201,7 @@ describe('useCardGenerator', () => {
addressPlz: '86111',
nuernbergPassId: 123,
regionId: 0,
startDay: { day: 26, isoMonth: 2, isoYear: 2025 },
startDay: { day: 1, isoMonth: 5, isoYear: 2025 },
},
fullName: 'Thea Test',
id: expect.any(Number),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,7 @@ import useCardGeneratorSelfService from '../useCardGeneratorSelfService'

const mocks: MockedResponse[] = []

const wrapper = ({ children }: { children: ReactNode }) => (
<MemoryRouter>
<AppToasterProvider>
<MockedProvider mocks={mocks} addTypename={false}>
<ProjectConfigProvider projectConfig={koblenzConfig}>{children}</ProjectConfigProvider>
</MockedProvider>
</AppToasterProvider>
</MemoryRouter>
)

jest.useFakeTimers({ now: new Date('2025-01-01T00:00:00.000Z') })
jest.mock('../../../../generated/graphql', () => ({
...jest.requireActual('../../../../generated/graphql'),
generatePdf: jest.fn(),
Expand All @@ -40,6 +31,24 @@ jest.mock('../../../../cards/PdfFactory', () => ({

jest.mock('../../../../util/downloadDataUri')

const wrapper = ({ children, initialRoutes }: { children: ReactNode; initialRoutes?: string[] }) => (
<MemoryRouter initialEntries={initialRoutes}>
<AppToasterProvider>
<MockedProvider mocks={mocks} addTypename={false}>
<ProjectConfigProvider projectConfig={koblenzConfig}>{children}</ProjectConfigProvider>
</MockedProvider>
</AppToasterProvider>
</MemoryRouter>
)

const withCustomWrapper =
(initialRoute: string) =>
({ children }: { children: ReactNode }) =>
wrapper({
children,
initialRoutes: [initialRoute],
})

describe('useCardGeneratorSelfService', () => {
it('should successfully create a card', async () => {
const toasterSpy = jest.spyOn(OverlayToaster.prototype, 'show')
Expand All @@ -54,4 +63,24 @@ describe('useCardGeneratorSelfService', () => {
await act(async () => result.current.downloadPdf(result.current.code!, 'koblenzpass.pdf'))
expect(downloadDataUri).toHaveBeenCalled()
})

it('should successfully initialize cards with searchParams for koblenz', async () => {
const { result } = renderHook(() => useCardGeneratorSelfService(), {
wrapper: withCustomWrapper('?Name=Karla Koblenz&Referenznummer=123K&Geburtsdatum=10.06.2003'),
})

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

0 comments on commit 12ef720

Please sign in to comment.