Skip to content
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

test(pdf-service): add pdfDownloadLoader tests #1547

Merged
merged 11 commits into from
Dec 12, 2024
Merged

test(pdf-service): add pdfDownloadLoader tests #1547

merged 11 commits into from
Dec 12, 2024

Conversation

rbrtrfl
Copy link
Contributor

@rbrtrfl rbrtrfl commented Dec 10, 2024

  • add pdfDownloadLoader tests for BerH and PKH
  • remove the expect statement for the pdf headers in both BerH and PKH e2e tests

Copy link
Contributor

@Spencer6497 Spencer6497 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stellar 🌟 work! few comments/suggestions, but don't let that stop you 🏃‍♂️

Comment on lines 18 to 27
vi.spyOn(PDFDocument.prototype, "embedFont").mockImplementation(function (
this: PDFDocument,
fontData,
...args
) {
const adjustedFontData =
fontData instanceof Buffer ? new Uint8Array(fontData) : fontData;
return originalEmbedFont.call(this, adjustedFontData, ...args);
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused why we used a function definition here as opposed to an arrow function like above? I have a feeling it has to do with the this binding... but what exactly is that doing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, correct. It's not possible to use an arrow function in this case, because we are looking to wrap an instance of the PDFDocument class (which is pdfDoc in fillPdf.server). So we need the this keyword.
In contrast, the other wrapper is wrapping load method of the PDFDocument class down the stack.


// Convert Buffers to Uint8Array for compatibility with Vitest
// Vitest expects Uint8Array inputs for certain methods in pdf-lib.
function wrapPdfDocumentLoad() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: would rename these functions something like

Suggested change
function wrapPdfDocumentLoad() {
function spyOnPdfDocumentLoad() {

as it wasn't immediately clear what these were doing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example: pdfKitBuffer in app/domains/beratungshilfe/services/pdf/index.ts is of type Buffer. Vitest expects is to be Uint8Array (please don't ask me why) which is why it needs to be wrapped like this new Uint8Array(pdfBuffer) before passing it into PDFDocument.load. Instead of touching the code in several places just for this one test, I've decided to wrap on demand inside of the test. So I think the name is quite appropriate. wdyt?

vi.restoreAllMocks();
});

it("generates correct PDF for Beratungshilfe", async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you really wanted to get crazy with it, you could consolidate these tests into a single it.each(), with an array of configs for PKH/BerH to test against, but entirely up to you 🤭

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Despite duplication of code, I've decided against this approach - for better readability 🤓

Comment on lines 5 to 6
// Convert Buffers to Uint8Array for compatibility with Vitest
// Vitest expects Uint8Array inputs for certain methods in pdf-lib.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does vitest have expectations here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no answer to this question. Maybe you can shed some light?

For better understanding, see the above answer to @Spencer6497's question.

@rbrtrfl rbrtrfl merged commit 8222791 into main Dec 12, 2024
21 checks passed
@rbrtrfl rbrtrfl deleted the pdf-test branch December 12, 2024 14:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants