-
Notifications
You must be signed in to change notification settings - Fork 1
MPDX-9702 Add Export Goals as PDF (Print All) button to Active Goals tab #1990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wjames111
wants to merge
12
commits into
main
Choose a base branch
from
MPDX-9702
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
532f35d
MPDX-9702 - Add Print All export of cohort goals as PDF
wjames111 2e24992
fix: address review comment on PrintCohortGoalsButton.tsx:11
wjames111 55d5e24
fix: address review comment on PrintCohortGoalsButton.tsx:29
wjames111 9743569
fix: address review comment on PrintCohortGoalsButton.tsx:32
wjames111 fc89387
fix: address review comment on PrintCohortGoalsButton.tsx:56
wjames111 c60a0d6
fix: address review comment on PrintCohortGoalsButton.test.tsx:81
wjames111 a8b841f
fix: address review comment on PrintCohortGoalsButton.test.tsx:83
wjames111 f1e83a2
fix: address review comment on printCohortGoalsPdf.test.ts:27
wjames111 d5c6c41
fix: address review comment on printCohortGoalsPdf.ts:20
wjames111 b6ca9e0
fix: address review comment on printCohortGoalsPdf.ts:51
wjames111 a5fe840
fix: address review comment on printCohortGoalsPdf.ts:60
wjames111 7b7ae96
fix: address review comment on mockData.ts:181
wjames111 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/components/HrTools/MpdGoalAdmin/PrintCohortGoalsButton/PrintCohortGoalsButton.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { ThemeProvider } from '@mui/material/styles'; | ||
| import { act, render, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { SnackbarProvider } from 'notistack'; | ||
| import theme from 'src/theme'; | ||
| import { MpdGoalAdminProvider, useMpdGoalAdmin } from '../MpdGoalAdminContext'; | ||
| import { TrainingCosts } from '../mpdGoalAdminHelpers'; | ||
| import { PrintCohortGoalsButton } from './PrintCohortGoalsButton'; | ||
| import { downloadPdf, generateCohortGoalsPdf } from './printCohortGoalsPdf'; | ||
|
|
||
| jest.mock('./printCohortGoalsPdf'); | ||
|
|
||
| const generateMock = generateCohortGoalsPdf as jest.MockedFunction< | ||
| typeof generateCohortGoalsPdf | ||
| >; | ||
| const downloadMock = downloadPdf as jest.MockedFunction<typeof downloadPdf>; | ||
|
|
||
| // Test harness exposing context so we can switch cohorts. | ||
| let ctx: ReturnType<typeof useMpdGoalAdmin>; | ||
| const Capture: React.FC = () => { | ||
| ctx = useMpdGoalAdmin(); | ||
| return <PrintCohortGoalsButton />; | ||
| }; | ||
|
|
||
| const renderButton = () => | ||
| render( | ||
| <ThemeProvider theme={theme}> | ||
| <SnackbarProvider> | ||
| <MpdGoalAdminProvider> | ||
| <Capture /> | ||
| </MpdGoalAdminProvider> | ||
| </SnackbarProvider> | ||
| </ThemeProvider>, | ||
| ); | ||
|
|
||
| describe('PrintCohortGoalsButton', () => { | ||
| beforeEach(() => { | ||
| generateMock.mockResolvedValue('blob:mock-pdf'); | ||
| }); | ||
|
|
||
| it('is disabled with an explanation until the cohort has training costs', async () => { | ||
| const { getByRole, findByText } = renderButton(); | ||
| // The spring cohort's training costs have not been entered yet. | ||
| act(() => ctx.setSelectedCohortId('spring-nso-2027')); | ||
|
|
||
| const button = getByRole('button', { name: 'Print All' }); | ||
| expect(button).toBeDisabled(); | ||
|
|
||
| userEvent.hover(button.parentElement as HTMLElement); | ||
| expect( | ||
| await findByText( | ||
| 'Enter training costs for this cohort to print its goals.', | ||
| ), | ||
| ).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('becomes enabled once training costs are entered', () => { | ||
| const { getByRole } = renderButton(); | ||
| act(() => ctx.setSelectedCohortId('spring-nso-2027')); | ||
| expect(getByRole('button', { name: 'Print All' })).toBeDisabled(); | ||
|
|
||
| const trainingCosts: TrainingCosts = { | ||
| nsoIndividual1InRoom: 100, | ||
| nsoIndividual2InRoom: 200, | ||
| nsoCouple: 300, | ||
| nsoFamily: 400, | ||
| ibsSingle: 500, | ||
| ibsCouple: 600, | ||
| refreshRetreatSingle: 700, | ||
| refreshRetreatCouple: 800, | ||
| faithAndFinanceSingle: 900, | ||
| faithAndFinanceCouple: 1000, | ||
| cruConferenceSingle: 1100, | ||
| cruConferenceCouple: 1200, | ||
| cruConferenceFamily: 1300, | ||
| }; | ||
| act(() => ctx.saveTrainingCosts('spring-nso-2027', trainingCosts)); | ||
| expect(getByRole('button', { name: 'Print All' })).toBeEnabled(); | ||
| }); | ||
|
|
||
| it('generates the cohort PDF and downloads it', async () => { | ||
| const { getByRole } = renderButton(); | ||
| userEvent.click(getByRole('button', { name: 'Print All' })); | ||
|
|
||
| await waitFor(() => | ||
| expect(downloadMock).toHaveBeenCalledWith( | ||
| 'blob:mock-pdf', | ||
| 'MPD Goals - Fall NSO 2026.pdf', | ||
| ), | ||
| ); | ||
| expect(generateMock).toHaveBeenCalledWith( | ||
| expect.objectContaining({ id: 'fall-nso-2026' }), | ||
| ); | ||
| expect(getByRole('button', { name: 'Print All' })).toBeEnabled(); | ||
| }); | ||
|
|
||
| it('disables the button and shows a spinner while generating', async () => { | ||
| let resolvePdf!: (url: string) => void; | ||
| generateMock.mockReturnValue( | ||
| new Promise((resolve) => (resolvePdf = resolve)), | ||
| ); | ||
| const { getByRole, findByRole } = renderButton(); | ||
| userEvent.click(getByRole('button', { name: 'Print All' })); | ||
|
|
||
| expect(await findByRole('progressbar')).toBeInTheDocument(); | ||
| expect(getByRole('button', { name: 'Print All' })).toBeDisabled(); | ||
|
|
||
| resolvePdf('blob:mock-pdf'); | ||
| await waitFor(() => expect(downloadMock).toHaveBeenCalled()); | ||
| expect(getByRole('button', { name: 'Print All' })).toBeEnabled(); | ||
| }); | ||
|
|
||
| it('shows an error and re-enables the button when generation fails', async () => { | ||
| generateMock.mockRejectedValue(new Error('boom')); | ||
| const { getByRole, findByText } = renderButton(); | ||
| userEvent.click(getByRole('button', { name: 'Print All' })); | ||
|
|
||
| expect( | ||
| await findByText('Unable to export the MPD Goals PDF.'), | ||
| ).toBeInTheDocument(); | ||
| expect(downloadMock).not.toHaveBeenCalled(); | ||
| expect(getByRole('button', { name: 'Print All' })).toBeEnabled(); | ||
| }); | ||
| }); | ||
69 changes: 69 additions & 0 deletions
69
src/components/HrTools/MpdGoalAdmin/PrintCohortGoalsButton/PrintCohortGoalsButton.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import React, { useState } from 'react'; | ||
| import { Button, CircularProgress, Tooltip } from '@mui/material'; | ||
| import { useSnackbar } from 'notistack'; | ||
| import { useTranslation } from 'react-i18next'; | ||
| import { useMpdGoalAdmin } from '../MpdGoalAdminContext'; | ||
| import { downloadPdf, generateCohortGoalsPdf } from './printCohortGoalsPdf'; | ||
|
|
||
| /** | ||
| * "Print All" — exports every goal in the selected cohort as a single | ||
| * printable PDF (used for NSO hard copies). Disabled until the cohort's | ||
| * training costs have been entered: without training costs the goal amounts | ||
| * aren't final, so printing may not act on them. (Run & Send is expected to | ||
| * adopt the same gate when it is wired up — it does not enforce it yet.) | ||
| */ | ||
| export const PrintCohortGoalsButton: React.FC = () => { | ||
| const { t } = useTranslation(); | ||
| const { enqueueSnackbar } = useSnackbar(); | ||
| const { selectedCohort } = useMpdGoalAdmin(); | ||
| const [printing, setPrinting] = useState(false); | ||
|
|
||
| const hasTrainingCosts = !!selectedCohort?.trainingCostEntered; | ||
|
|
||
| const handlePrint = async () => { | ||
| if (!selectedCohort) { | ||
| return; | ||
| } | ||
| setPrinting(true); | ||
| try { | ||
| const url = await generateCohortGoalsPdf(selectedCohort); | ||
|
wjames111 marked this conversation as resolved.
|
||
| downloadPdf(url, `MPD Goals - ${selectedCohort.name}.pdf`); | ||
| } catch { | ||
| // TODO(MPDX-9691): once generation is a GraphQL mutation, the global | ||
| // Apollo error link will already toast failures — remove this snackbar | ||
| // (or suppress the generic one) so the user isn't double-toasted. | ||
| enqueueSnackbar(t('Unable to export the MPD Goals PDF.'), { | ||
|
wjames111 marked this conversation as resolved.
|
||
| variant: 'error', | ||
| }); | ||
| } finally { | ||
| setPrinting(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Tooltip | ||
| title={ | ||
| hasTrainingCosts | ||
| ? t('Prints every goal in {{cohort}}.', { | ||
| cohort: selectedCohort?.name ?? '', | ||
| }) | ||
| : t('Enter training costs for this cohort to print its goals.') | ||
| } | ||
| > | ||
| {/* span so the tooltip still fires while the button is disabled */} | ||
| <span> | ||
| <Button | ||
| variant="outlined" | ||
| disabled={!hasTrainingCosts || printing} | ||
| onClick={handlePrint} | ||
| aria-busy={printing} | ||
| > | ||
| {t('Print All')} | ||
| {printing && ( | ||
| <CircularProgress size={16} color="inherit" sx={{ ml: 1 }} /> | ||
| )} | ||
| </Button> | ||
| </span> | ||
| </Tooltip> | ||
| ); | ||
| }; | ||
85 changes: 85 additions & 0 deletions
85
src/components/HrTools/MpdGoalAdmin/PrintCohortGoalsButton/printCohortGoalsPdf.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { mockCohorts } from '../mockData'; | ||
| import { | ||
| buildPlaceholderPdf, | ||
| downloadPdf, | ||
| generateCohortGoalsPdf, | ||
| } from './printCohortGoalsPdf'; | ||
|
|
||
| describe('buildPlaceholderPdf', () => { | ||
| it('produces a well-formed PDF document', () => { | ||
| const pdf = buildPlaceholderPdf(['Hello', 'World']); | ||
| expect(pdf).toMatch(/^%PDF-1\.4\n/); | ||
| expect(pdf).toMatch(/%%EOF$/); | ||
| expect(pdf).toContain('(Hello) Tj'); | ||
| expect(pdf).toContain('(World) Tj'); | ||
| // The xref offset in startxref must point at the xref keyword. | ||
| const xrefOffset = Number(pdf.match(/startxref\n(\d+)/)?.[1]); | ||
| expect(pdf.slice(xrefOffset, xrefOffset + 4)).toBe('xref'); | ||
| }); | ||
|
|
||
| it('escapes characters that would terminate a PDF string', () => { | ||
| const pdf = buildPlaceholderPdf(['John (Jack) Doe \\ Co']); | ||
| expect(pdf).toContain('(John \\(Jack\\) Doe \\\\ Co) Tj'); | ||
| }); | ||
|
|
||
| it('throws when the lines exceed the single-page capacity', () => { | ||
| expect(() => buildPlaceholderPdf(Array(45).fill('x'))).toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('generateCohortGoalsPdf', () => { | ||
| // jsdom does not implement Blob.text(), so read the blob with a FileReader. | ||
| const readBlobText = (blob: Blob) => | ||
| new Promise<string>((resolve, reject) => { | ||
| const reader = new FileReader(); | ||
| reader.onload = () => resolve(reader.result as string); | ||
| reader.onerror = () => reject(reader.error); | ||
| reader.readAsText(blob); | ||
| }); | ||
|
|
||
| it('builds a blob URL for a PDF listing every goal in the cohort', async () => { | ||
|
wjames111 marked this conversation as resolved.
|
||
| const createObjectURL = jest.fn().mockReturnValue('blob:cohort-pdf'); | ||
| window.URL.createObjectURL = createObjectURL; | ||
|
|
||
| await expect(generateCohortGoalsPdf(mockCohorts[0])).resolves.toBe( | ||
| 'blob:cohort-pdf', | ||
| ); | ||
| const blob = createObjectURL.mock.calls[0][0] as Blob; | ||
| expect(blob.type).toBe('application/pdf'); | ||
|
|
||
| const pdf = await readBlobText(blob); | ||
| expect(pdf).toContain('(MPD Goals - Fall NSO 2026) Tj'); | ||
| expect(pdf).toContain('(John & Jane Doe: $6,430.25) Tj'); | ||
| expect(pdf).toContain('(Carlos & Michaela Everts: $5,280.77) Tj'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('downloadPdf', () => { | ||
| it('downloads via a temporary anchor and releases the URL', () => { | ||
| const revokeObjectURL = jest.fn(); | ||
| window.URL.revokeObjectURL = revokeObjectURL; | ||
| const click = jest | ||
| .spyOn(HTMLAnchorElement.prototype, 'click') | ||
| .mockImplementation(() => {}); | ||
| let anchor: HTMLAnchorElement | undefined; | ||
| const realCreateElement = document.createElement.bind(document); | ||
| const createElement = jest | ||
| .spyOn(document, 'createElement') | ||
| .mockImplementation((tagName) => { | ||
| const element = realCreateElement(tagName); | ||
| if (element instanceof HTMLAnchorElement) { | ||
| anchor = element; | ||
| } | ||
| return element; | ||
| }); | ||
|
|
||
| downloadPdf('blob:cohort-pdf', 'MPD Goals - Fall NSO 2026.pdf'); | ||
|
|
||
| expect(click).toHaveBeenCalled(); | ||
| expect(anchor?.download).toBe('MPD Goals - Fall NSO 2026.pdf'); | ||
| expect(anchor?.href).toContain('blob:cohort-pdf'); | ||
| expect(revokeObjectURL).toHaveBeenCalledWith('blob:cohort-pdf'); | ||
| click.mockRestore(); | ||
| createElement.mockRestore(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.