Skip to content

Commit eb009ff

Browse files
committed
Fix Page tests relying on implementation details
1 parent cc566b4 commit eb009ff

File tree

2 files changed

+86
-44
lines changed

2 files changed

+86
-44
lines changed

__mocks__/_pdf4.pdf

74.3 KB
Binary file not shown.

src/Page.spec.jsx

Lines changed: 86 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,13 @@ import { loadPDF, makeAsyncCallback, muteConsole, restoreConsole } from '../test
1212

1313
const pdfFile = loadPDF('./__mocks__/_pdf.pdf');
1414
const pdfFile2 = loadPDF('./__mocks__/_pdf2.pdf');
15-
16-
vi.mock('./Page/AnnotationLayer', () => ({
17-
default: function AnnotationLayer() {
18-
return <div className="react-pdf__Page__annotations" />;
19-
},
20-
}));
15+
const pdfFile4 = loadPDF('./__mocks__/_pdf4.pdf');
2116

2217
describe('Page', () => {
2318
// Loaded PDF file
2419
let pdf;
2520
let pdf2;
21+
let pdf4;
2622

2723
// Object with basic loaded page information that shall match after successful loading
2824
const desiredLoadedPage = {};
@@ -52,6 +48,8 @@ describe('Page', () => {
5248

5349
registerPageArguments.push(page._pageIndex, expect.any(HTMLDivElement));
5450
unregisterPageArguments = page._pageIndex;
51+
52+
pdf4 = await pdfjs.getDocument({ data: pdfFile4.arrayBuffer }).promise;
5553
});
5654

5755
describe('loading', () => {
@@ -300,27 +298,50 @@ describe('Page', () => {
300298
});
301299

302300
it('requests page to be rendered with default rotation when given nothing', async () => {
303-
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
304-
const instance = createRef();
301+
const { func: onRenderSuccess, promise: onRenderSuccessPromise } = makeAsyncCallback();
305302

306-
render(<Page onLoadSuccess={onLoadSuccess} pageIndex={0} pdf={pdf} ref={instance} />);
303+
const { container } = render(
304+
<Page onRenderSuccess={onRenderSuccess} pageIndex={0} pdf={pdf} renderMode="svg" />,
305+
);
307306

308-
await onLoadSuccessPromise;
307+
const page = await onRenderSuccessPromise;
309308

310-
expect(instance.current.rotate).toBe(0);
309+
const pageSvg = container.querySelector('.react-pdf__Page__svg');
310+
311+
const { width, height } = window.getComputedStyle(pageSvg);
312+
313+
const viewport = page.getViewport({ scale: 1 });
314+
315+
// Expect the annotation layer not to be rotated
316+
expect(parseInt(width, 10)).toBe(Math.floor(viewport.width));
317+
expect(parseInt(height, 10)).toBe(Math.floor(viewport.height));
311318
});
312319

313320
it('requests page to be rendered with given rotation when given rotate prop', async () => {
314-
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
315-
const instance = createRef();
321+
const { func: onRenderSuccess, promise: onRenderSuccessPromise } = makeAsyncCallback();
322+
const rotate = 90;
316323

317-
render(
318-
<Page onLoadSuccess={onLoadSuccess} pageIndex={0} pdf={pdf} rotate={90} ref={instance} />,
324+
const { container } = render(
325+
<Page
326+
onRenderSuccess={onRenderSuccess}
327+
pageIndex={0}
328+
pdf={pdf}
329+
renderMode="svg"
330+
rotate={rotate}
331+
/>,
319332
);
320333

321-
await onLoadSuccessPromise;
334+
const page = await onRenderSuccessPromise;
335+
336+
const pageSvg = container.querySelector('.react-pdf__Page__svg');
337+
338+
const { width, height } = window.getComputedStyle(pageSvg);
322339

323-
expect(instance.current.rotate).toBe(90);
340+
const viewport = page.getViewport({ scale: 1, rotation: rotate });
341+
342+
// Expect the annotation layer to be rotated
343+
expect(parseInt(width, 10)).toBe(Math.floor(viewport.width));
344+
expect(parseInt(height, 10)).toBe(Math.floor(viewport.height));
324345
});
325346

326347
it('requests page to be rendered in canvas mode by default', async () => {
@@ -530,74 +551,95 @@ describe('Page', () => {
530551
});
531552

532553
it('requests page to be rendered without forms by default', async () => {
533-
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
534-
const instance = createRef();
554+
const { func: onRenderAnnotationLayerSuccess, promise: onRenderAnnotationLayerSuccessPromise } =
555+
makeAsyncCallback();
535556

536-
render(<Page onLoadSuccess={onLoadSuccess} pageIndex={0} pdf={pdf} ref={instance} />);
557+
const { container } = render(
558+
<Page
559+
onRenderAnnotationLayerSuccess={onRenderAnnotationLayerSuccess}
560+
pageIndex={0}
561+
pdf={pdf4}
562+
renderMode="none"
563+
/>,
564+
);
537565

538566
expect.assertions(1);
539567

540-
await onLoadSuccessPromise;
568+
await onRenderAnnotationLayerSuccessPromise;
569+
570+
const textWidgetAnnotation = container.querySelector('.textWidgetAnnotation');
541571

542-
expect(instance.current.childContext.renderForms).toBeFalsy();
572+
expect(textWidgetAnnotation).toBeFalsy();
543573
});
544574

545575
it('requests page to be rendered with forms given renderForms = true', async () => {
546-
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
547-
const instance = createRef();
576+
const { func: onRenderAnnotationLayerSuccess, promise: onRenderAnnotationLayerSuccessPromise } =
577+
makeAsyncCallback();
548578

549-
render(
550-
<Page onLoadSuccess={onLoadSuccess} pageIndex={0} pdf={pdf} ref={instance} renderForms />,
579+
const { container } = render(
580+
<Page
581+
onRenderAnnotationLayerSuccess={onRenderAnnotationLayerSuccess}
582+
pageIndex={0}
583+
pdf={pdf4}
584+
renderForms
585+
renderMode="none"
586+
/>,
551587
);
552588

553589
expect.assertions(1);
554590

555-
await onLoadSuccessPromise;
591+
await onRenderAnnotationLayerSuccessPromise;
592+
593+
const textWidgetAnnotation = container.querySelector('.textWidgetAnnotation');
556594

557-
expect(instance.current.childContext.renderForms).toBe(true);
595+
expect(textWidgetAnnotation).toBeTruthy();
558596
});
559597

560598
it('requests page to be rendered with forms given legacy renderInteractiveForms = true', async () => {
561-
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
562-
const instance = createRef();
599+
const { func: onRenderAnnotationLayerSuccess, promise: onRenderAnnotationLayerSuccessPromise } =
600+
makeAsyncCallback();
563601

564-
render(
602+
const { container } = render(
565603
<Page
566-
onLoadSuccess={onLoadSuccess}
604+
onRenderAnnotationLayerSuccess={onRenderAnnotationLayerSuccess}
567605
pageIndex={0}
568-
pdf={pdf}
569-
ref={instance}
606+
pdf={pdf4}
570607
renderInteractiveForms
608+
renderMode="none"
571609
/>,
572610
);
573611

574612
expect.assertions(1);
575613

576-
await onLoadSuccessPromise;
614+
await onRenderAnnotationLayerSuccessPromise;
615+
616+
const textWidgetAnnotation = container.querySelector('.textWidgetAnnotation');
577617

578-
expect(instance.current.childContext.renderForms).toBe(true);
618+
expect(textWidgetAnnotation).toBeTruthy();
579619
});
580620

581621
it('requests page to be rendered without forms given renderForms = false and legacy renderInteractiveForms = true', async () => {
582-
const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback();
583-
const instance = createRef();
622+
const { func: onRenderAnnotationLayerSuccess, promise: onRenderAnnotationLayerSuccessPromise } =
623+
makeAsyncCallback();
584624

585-
render(
625+
const { container } = render(
586626
<Page
587-
onLoadSuccess={onLoadSuccess}
627+
onRenderAnnotationLayerSuccess={onRenderAnnotationLayerSuccess}
588628
pageIndex={0}
589-
pdf={pdf}
590-
ref={instance}
629+
pdf={pdf4}
591630
renderForms={false}
592631
renderInteractiveForms
632+
renderMode="none"
593633
/>,
594634
);
595635

596636
expect.assertions(1);
597637

598-
await onLoadSuccessPromise;
638+
await onRenderAnnotationLayerSuccessPromise;
639+
640+
const textWidgetAnnotation = container.querySelector('.textWidgetAnnotation');
599641

600-
expect(instance.current.childContext.renderForms).toBeFalsy();
642+
expect(textWidgetAnnotation).toBeFalsy();
601643
});
602644

603645
it('requests page to be rendered at its original size given nothing', async () => {

0 commit comments

Comments
 (0)