Replies: 4 comments 11 replies
-
Current tests I would like to get working with v7: describe('ProcessingFile', () => {
test('unprotected pdf', async () => {
const workingPDFFile = loadPDF('./__mocks__/test-pdf.pdf');
const {func: onLoadSuccess, promise: onLoadSuccessPromise} = makeAsyncCallback(
Object.assign(workingPDFFile.file, {
dataUrl: URL.createObjectURL(workingPDFFile.file),
}),
);
render(<ProcessingFile file={workingPDFFile.file} onAccept={onLoadSuccess} process={true} />);
expect.hasAssertions();
expect.assertions(2);
expect(await screen.findByTestId('selectedFileName')).toHaveTextContent('test-pdf.pdf');
await expect(onLoadSuccessPromise).resolves.toMatchObject({dataUrl: 'mock-url'});
});
test('protected pdf', async () => {
const protectedPDFFile = loadPDF('./__mocks__/password-protected-pdf.pdf');
const {func: onLoadSuccess} = makeAsyncCallback();
const {func: onReject, promise: onRejectPromise} = makeAsyncCallback();
render(<ProcessingFile file={protectedPDFFile.file} onAccept={onLoadSuccess} onReject={onReject} process={true} />);
expect.hasAssertions();
expect.assertions(1);
await expect(onRejectPromise).resolves.toMatchObject({
errors: [{code: 'protected-pdf', message: 'Please upload a file without password protection.'}],
file: protectedPDFFile.file,
});
});
test('corrupt pdf', async () => {
const corruptedFile = new File([new ArrayBuffer(42)], 'derp', {type: 'application/pdf'});
const originalConsoleLog = console.log;
const {func: onLoadSuccess} = makeAsyncCallback();
const {func: onReject, promise: onRejectPromise} = makeAsyncCallback();
// Temporarily override console.log as PDF.js warns about the corrupt PDF as it tries to fix it.
console.log = jest.fn();
render(<ProcessingFile file={corruptedFile} onAccept={onLoadSuccess} onReject={onReject} process={true} />);
expect.hasAssertions();
expect.assertions(4);
expect(await screen.findByText(/Failed to load PDF file./i)).toBeVisible();
await expect(onRejectPromise).resolves.toMatchObject({
errors: [{code: 'source-error', message: 'Sorry, this upload failed. Please try again.'}],
file: corruptedFile,
});
// Ensure we don't get other logs apart from the one we are hiding...
expect(console.log).toHaveBeenCalledTimes(1);
expect(console.log).toHaveBeenCalledWith('Warning: Indexing all PDF objects');
// Restore logging
console.log = originalConsoleLog;
});
}); |
Beta Was this translation helpful? Give feedback.
-
I guess one of the fundamental questions is why do the tests work in Vitest/this repository and not in Jest/my repo? What am I missing in my setup that's been done for this repo? :) |
Beta Was this translation helpful? Give feedback.
-
I've been through similar problems and got stuck at the same point (arraybuffer cannot be polyfilled) and I think the problem lies in pdfjs-dist, not react-pdf itself. However, I'm using react-pdf 7.1.3 with pdfjs-dist v3.6.172 and still getting the same error. One of the suggested solutions ( mozilla/pdf.js#16367 (comment) ) is to downgrade to pdfjs v3.4.120 which is not very helpful as react-pdf currently depends on v3.6.172. @wojtekmaj Are there plans to upgrade to use the latest pdfjs-dist, or is there a straightforward way of making react-pdf use a newer version? |
Beta Was this translation helpful? Give feedback.
-
I'm just throwing my 2 cents in here... after reading this thread and looking at the source, I was eventually able to get some tests working but they weren't very stable. I eventually decided that it just smelled bad and wasn't appropriate for a unit test with react testing library. I switched all of the pdf related tests to e2e tests with playwright and everything just worked without any complicated mocking. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
G'day,
I have spent almost all day today trying to figure out how I can add tests to my project that just asserts if the right callbacks are fired and the right error messages are shown on a page.
Set up:
It's working fine in the browser after I deleted the
options
prop from the implementation (I am upgrading my implementation from [email protected] to 7.x) because it caused errors (similar to #1529).Anyway, here's my basic component:
In other words, it's not meant to render anything itself, we are purely using react-pdf to check if a given PDF (selected via react-dropzone) can be read and whether or not it's password protected.
Now we would like to have tests that give the PdfLoader a file and check if the right error messages and data has been rendered to the page. That's all. My current implementation with react-pdf@v5 is working and the tests pass. After the upgrade they fail for various reasons. I looked at the tests in this repo (written using vitest, not jest) and can't see what I am doing differently in my set up.
If someone has a working Jest version of the tests that would be super appreciated :)
Happy to answer questions or add more code samples or something to repro.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions