diff --git a/packages/react-pdf/src/Page.spec.tsx b/packages/react-pdf/src/Page.spec.tsx index 038c8d29f..38f1b1bda 100644 --- a/packages/react-pdf/src/Page.spec.tsx +++ b/packages/react-pdf/src/Page.spec.tsx @@ -6,6 +6,7 @@ import { fireEvent, render } from '@testing-library/react'; import { pdfjs } from './index.test.js'; import Page from './Page.js'; +import LinkService from './LinkService.js'; import failingPdf from '../../../__mocks__/_failing_pdf.js'; import silentlyFailingPdf from '../../../__mocks__/_silently_failing_pdf.js'; @@ -42,6 +43,8 @@ function renderWithContext(children: React.ReactNode, context: Partial { + const linkService = new LinkService(); + // Loaded PDF file let pdf: PDFDocumentProxy; let pdf2: PDFDocumentProxy; @@ -83,7 +86,10 @@ describe('Page', () => { it('loads a page and calls onLoadSuccess callback properly', async () => { const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback(); - renderWithContext(, { pdf }); + renderWithContext(, { + linkService, + pdf, + }); expect.assertions(1); @@ -94,7 +100,10 @@ describe('Page', () => { const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback<[PageCallback]>(); - renderWithContext(, { pdf }); + renderWithContext(, { + linkService, + pdf, + }); expect.assertions(5); @@ -125,7 +134,10 @@ describe('Page', () => { it('loads page when given pageIndex', async () => { const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback(); - renderWithContext(, { pdf }); + renderWithContext(, { + linkService, + pdf, + }); expect.assertions(1); @@ -137,7 +149,10 @@ describe('Page', () => { it('loads page when given pageNumber', async () => { const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback(); - renderWithContext(, { pdf }); + renderWithContext(, { + linkService, + pdf, + }); expect.assertions(1); @@ -195,7 +210,10 @@ describe('Page', () => { const { func: onLoadSuccess2, promise: onLoadSuccessPromise2 } = makeAsyncCallback(); - rerender(, { pdf: pdf2 }); + rerender(, { + linkService, + pdf: pdf2, + }); await expect(onLoadSuccessPromise2).resolves.toMatchObject([desiredLoadedPage3]); }); @@ -213,7 +231,10 @@ describe('Page', () => { const { func: onLoadSuccess2, promise: onLoadSuccessPromise2 } = makeAsyncCallback(); - rerender(, { pdf }); + rerender(, { + linkService, + pdf, + }); await expect(onLoadSuccessPromise2).resolves.toMatchObject([desiredLoadedPage2]); }); @@ -257,7 +278,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -272,7 +296,10 @@ describe('Page', () => { it('renders "No page specified." when given neither pageIndex nor pageNumber', () => { muteConsole(); - const { container } = renderWithContext(, { pdf }); + const { container } = renderWithContext(, { + linkService, + pdf, + }); const noData = container.querySelector('.react-pdf__message'); @@ -285,7 +312,10 @@ describe('Page', () => { it('renders custom no data message when given nothing and noData is given', () => { muteConsole(); - const { container } = renderWithContext(, { pdf }); + const { container } = renderWithContext(, { + linkService, + pdf, + }); const noData = container.querySelector('.react-pdf__message'); @@ -298,7 +328,10 @@ describe('Page', () => { it('renders custom no data message when given nothing and noData is given as a function', () => { muteConsole(); - const { container } = renderWithContext( 'Nothing here'} />, { pdf }); + const { container } = renderWithContext( 'Nothing here'} />, { + linkService, + pdf, + }); const noData = container.querySelector('.react-pdf__message'); @@ -309,7 +342,10 @@ describe('Page', () => { }); it('renders "Loading pageā€¦" when loading a page', async () => { - const { container } = renderWithContext(, { pdf }); + const { container } = renderWithContext(, { + linkService, + pdf, + }); const loading = container.querySelector('.react-pdf__message'); @@ -318,7 +354,10 @@ describe('Page', () => { }); it('renders custom loading message when loading a page and loading prop is given', async () => { - const { container } = renderWithContext(, { pdf }); + const { container } = renderWithContext(, { + linkService, + pdf, + }); const loading = container.querySelector('.react-pdf__message'); @@ -360,7 +399,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); const [page] = await onRenderSuccessPromise; @@ -388,7 +430,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); const [page] = await onRenderSuccessPromise; @@ -411,7 +456,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -428,7 +476,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(2); @@ -447,7 +498,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -473,7 +527,10 @@ describe('Page', () => { pageIndex={0} renderMode="custom" />, - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -490,7 +547,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -507,7 +567,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -524,7 +587,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -541,7 +607,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -558,7 +627,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -585,7 +657,10 @@ describe('Page', () => { renderMode="custom" renderTextLayer />, - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -602,7 +677,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -619,7 +697,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -636,7 +717,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -653,7 +737,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -676,7 +763,10 @@ describe('Page', () => { pageIndex={0} renderMode="none" />, - { pdf: pdf4 }, + { + linkService, + pdf: pdf4, + }, ); expect.assertions(1); @@ -699,7 +789,10 @@ describe('Page', () => { renderForms renderMode="none" />, - { pdf: pdf4 }, + { + linkService, + pdf: pdf4, + }, ); expect.assertions(1); @@ -715,7 +808,10 @@ describe('Page', () => { const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback<[PageCallback]>(); - renderWithContext(, { pdf }); + renderWithContext(, { + linkService, + pdf, + }); expect.assertions(1); @@ -729,7 +825,10 @@ describe('Page', () => { makeAsyncCallback<[PageCallback]>(); const scale = 1.5; - renderWithContext(, { pdf }); + renderWithContext(, { + linkService, + pdf, + }); expect.assertions(1); @@ -743,7 +842,10 @@ describe('Page', () => { makeAsyncCallback<[PageCallback]>(); const width = 600; - renderWithContext(, { pdf }); + renderWithContext(, { + linkService, + pdf, + }); expect.assertions(1); @@ -845,7 +947,10 @@ describe('Page', () => { scale={scale} width={width} />, - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(2); @@ -860,7 +965,10 @@ describe('Page', () => { it('calls onClick callback when clicked a page (sample of mouse events family)', () => { const onClick = vi.fn(); - const { container } = renderWithContext(, { pdf }); + const { container } = renderWithContext(, { + linkService, + pdf, + }); const page = container.querySelector('.react-pdf__Page') as HTMLDivElement; fireEvent.click(page); @@ -871,7 +979,10 @@ describe('Page', () => { it('calls onTouchStart callback when touched a page (sample of touch events family)', () => { const onTouchStart = vi.fn(); - const { container } = renderWithContext(, { pdf }); + const { container } = renderWithContext(, { + linkService, + pdf, + }); const page = container.querySelector('.react-pdf__Page') as HTMLDivElement; fireEvent.touchStart(page); diff --git a/packages/react-pdf/src/Page/AnnotationLayer.tsx b/packages/react-pdf/src/Page/AnnotationLayer.tsx index f96a3a9ed..dca621561 100644 --- a/packages/react-pdf/src/Page/AnnotationLayer.tsx +++ b/packages/react-pdf/src/Page/AnnotationLayer.tsx @@ -172,6 +172,10 @@ export default function AnnotationLayer() { layer.innerHTML = ''; + if (!linkService) { + throw new Error('OMG'); + } + try { pdfjs.AnnotationLayer.render(parameters);