From 10bb746627bb85c5194f75a13311ed4e07f6afbb Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Fri, 22 Sep 2023 11:47:57 +0200 Subject: [PATCH] Add LinkService to mocked documentContext in Page unit tests --- packages/react-pdf/src/Page.spec.tsx | 189 +++++++++++++++++++++------ 1 file changed, 152 insertions(+), 37 deletions(-) diff --git a/packages/react-pdf/src/Page.spec.tsx b/packages/react-pdf/src/Page.spec.tsx index 038c8d29f..a94414b05 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); @@ -113,7 +122,10 @@ describe('Page', () => { muteConsole(); - renderWithContext(, { pdf: failingPdf }); + renderWithContext(, { + linkService, + pdf: failingPdf, + }); expect.assertions(1); @@ -125,7 +137,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 +152,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 +213,10 @@ describe('Page', () => { const { func: onLoadSuccess2, promise: onLoadSuccessPromise2 } = makeAsyncCallback(); - rerender(, { pdf: pdf2 }); + rerender(, { + linkService, + pdf: pdf2, + }); await expect(onLoadSuccessPromise2).resolves.toMatchObject([desiredLoadedPage3]); }); @@ -213,7 +234,10 @@ describe('Page', () => { const { func: onLoadSuccess2, promise: onLoadSuccessPromise2 } = makeAsyncCallback(); - rerender(, { pdf }); + rerender(, { + linkService, + pdf, + }); await expect(onLoadSuccessPromise2).resolves.toMatchObject([desiredLoadedPage2]); }); @@ -244,6 +268,7 @@ describe('Page', () => { const inputRef = createRef(); renderWithContext(, { + linkService, pdf: silentlyFailingPdf, }); @@ -257,7 +282,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -272,7 +300,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 +316,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 +332,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 +346,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 +358,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 +403,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); const [page] = await onRenderSuccessPromise; @@ -388,7 +434,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); const [page] = await onRenderSuccessPromise; @@ -411,7 +460,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -428,7 +480,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(2); @@ -447,7 +502,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -473,7 +531,10 @@ describe('Page', () => { pageIndex={0} renderMode="custom" />, - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -490,7 +551,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -507,7 +571,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -524,7 +591,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -541,7 +611,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -558,7 +631,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -585,7 +661,10 @@ describe('Page', () => { renderMode="custom" renderTextLayer />, - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -602,7 +681,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -619,7 +701,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -636,7 +721,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -653,7 +741,10 @@ describe('Page', () => { const { container } = renderWithContext( , - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(1); @@ -676,7 +767,10 @@ describe('Page', () => { pageIndex={0} renderMode="none" />, - { pdf: pdf4 }, + { + linkService, + pdf: pdf4, + }, ); expect.assertions(1); @@ -699,7 +793,10 @@ describe('Page', () => { renderForms renderMode="none" />, - { pdf: pdf4 }, + { + linkService, + pdf: pdf4, + }, ); expect.assertions(1); @@ -715,7 +812,10 @@ describe('Page', () => { const { func: onLoadSuccess, promise: onLoadSuccessPromise } = makeAsyncCallback<[PageCallback]>(); - renderWithContext(, { pdf }); + renderWithContext(, { + linkService, + pdf, + }); expect.assertions(1); @@ -729,7 +829,10 @@ describe('Page', () => { makeAsyncCallback<[PageCallback]>(); const scale = 1.5; - renderWithContext(, { pdf }); + renderWithContext(, { + linkService, + pdf, + }); expect.assertions(1); @@ -743,7 +846,10 @@ describe('Page', () => { makeAsyncCallback<[PageCallback]>(); const width = 600; - renderWithContext(, { pdf }); + renderWithContext(, { + linkService, + pdf, + }); expect.assertions(1); @@ -845,7 +951,10 @@ describe('Page', () => { scale={scale} width={width} />, - { pdf }, + { + linkService, + pdf, + }, ); expect.assertions(2); @@ -860,7 +969,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 +983,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);