-
Notifications
You must be signed in to change notification settings - Fork 2
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
Changes from 5 commits
dfd190d
beb66e3
7b785a4
88d3a5f
b4ef7b0
56476db
da70c32
c5d5912
7605976
61280b4
de5d733
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,96 @@ | ||||||
import type { LoaderFunctionArgs } from "@remix-run/node"; | ||||||
import { PDFDocument } from "pdf-lib"; | ||||||
import { pdfDownloadLoader } from "../pdfDownloadLoader"; | ||||||
|
||||||
// Convert Buffers to Uint8Array for compatibility with Vitest | ||||||
// Vitest expects Uint8Array inputs for certain methods in pdf-lib. | ||||||
function wrapPdfDocumentLoad() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: would rename these functions something like
Suggested change
as it wasn't immediately clear what these were doing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Example: |
||||||
const originalLoad = PDFDocument.load; | ||||||
vi.spyOn(PDFDocument, "load").mockImplementation((input, ...args) => { | ||||||
const adjustedInput = | ||||||
input instanceof Buffer ? new Uint8Array(input) : input; | ||||||
return originalLoad.call(PDFDocument, adjustedInput, ...args); | ||||||
}); | ||||||
} | ||||||
|
||||||
function wrapEmbedFont() { | ||||||
const originalEmbedFont = PDFDocument.prototype.embedFont; | ||||||
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); | ||||||
}); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit confused why we used a There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
|
||||||
vi.mock("~/services/flow/pruner", () => ({ | ||||||
pruneIrrelevantData: vi | ||||||
.fn() | ||||||
.mockResolvedValue({ vorname: "Zoe", nachname: "Müller" }), | ||||||
})); | ||||||
|
||||||
describe("pdfDownloadLoader", () => { | ||||||
beforeAll(() => { | ||||||
wrapPdfDocumentLoad(); | ||||||
wrapEmbedFont(); | ||||||
}); | ||||||
|
||||||
afterAll(() => { | ||||||
vi.restoreAllMocks(); | ||||||
}); | ||||||
|
||||||
it("generates correct PDF for Beratungshilfe", async () => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 🤓 |
||||||
const mockLoaderArgs: LoaderFunctionArgs = { | ||||||
request: new Request( | ||||||
"https://mock-url.de/beratungshilfe/antrag/download/pdf", | ||||||
{ | ||||||
method: "GET", | ||||||
headers: new Headers({ Cookie: "mock-session-data" }), | ||||||
}, | ||||||
), | ||||||
params: {}, | ||||||
context: {}, | ||||||
}; | ||||||
|
||||||
const response = await pdfDownloadLoader(mockLoaderArgs); | ||||||
const pdfBuffer = Buffer.from(await response.arrayBuffer()); | ||||||
const pdfDoc = await PDFDocument.load(new Uint8Array(pdfBuffer)); | ||||||
const nameField = pdfDoc | ||||||
.getForm() | ||||||
.getTextField("Antragsteller (Name, Vorname ggf Geburtsname)"); | ||||||
|
||||||
expect(response.status).toBe(200); | ||||||
expect(response.headers.get("Content-Type")).toBe("application/pdf"); | ||||||
expect(pdfDoc.getPageCount()).toBe(4); | ||||||
expect(nameField.getText()).toBe("Müller, Zoe"); | ||||||
}); | ||||||
|
||||||
it("generates correct PDF for Prozesskostenhilfe", async () => { | ||||||
const mockLoaderArgs: LoaderFunctionArgs = { | ||||||
request: new Request( | ||||||
"https://mock-url.de/prozesskostenhilfe/formular/download/pdf", | ||||||
{ | ||||||
method: "GET", | ||||||
headers: new Headers({ Cookie: "mock-session-data" }), | ||||||
}, | ||||||
), | ||||||
params: {}, | ||||||
context: {}, | ||||||
}; | ||||||
|
||||||
const response = await pdfDownloadLoader(mockLoaderArgs); | ||||||
const pdfBuffer = Buffer.from(await response.arrayBuffer()); | ||||||
const pdfDoc = await PDFDocument.load(new Uint8Array(pdfBuffer)); | ||||||
const nameField = pdfDoc | ||||||
.getForm() | ||||||
.getTextField("Name Vorname ggf Geburtsname"); | ||||||
|
||||||
expect(response.status).toBe(200); | ||||||
expect(response.headers.get("Content-Type")).toBe("application/pdf"); | ||||||
expect(pdfDoc.getPageCount()).toBe(9); | ||||||
expect(nameField.getText()).toBe("Müller, Zoe"); | ||||||
}); | ||||||
}); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.