-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: implement updates for usePDF hook (#2247)
* implement updates for usePDF hooks * setup simple test * setup first test for usePDF hook * deduplicate deps * implement tests for update * use custom environment * remove mocks * add changeset * add no args version * update `BlobProvider` component * add tests for components * fix typos
- Loading branch information
Showing
12 changed files
with
850 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
'@react-pdf/renderer': patch | ||
--- | ||
|
||
### updates for `usePDF` hook | ||
|
||
`update` function takes the new document and renders it: | ||
|
||
```jsx | ||
const PdfView = () => { | ||
const [pdf, update] = usePdf(); | ||
|
||
useEffect(() => { | ||
update(<PDFDocument />); | ||
}, []); | ||
|
||
if (pdf.loading) return null; | ||
|
||
// use your PDF here | ||
return <>{pdf.url}</>; | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
module.exports = { | ||
testRegex: 'tests/.*?(test)\\.js$', | ||
setupFilesAfterEnv: ['<rootDir>/setup.jest.js'], | ||
transformIgnorePatterns: [ | ||
'/node_modules/', | ||
'<rootDir>/tests/environment/jsdom.js', | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* @jest-environment ./tests/environment/jsdom.js | ||
*/ | ||
|
||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { render, waitFor, screen } from '@testing-library/react'; | ||
import { | ||
BlobProvider, | ||
PDFViewer, | ||
PDFDownloadLink, | ||
Document, | ||
Page, | ||
Text, | ||
} from '../src/dom'; | ||
|
||
const TestDocument = ({ title = 'Default' }) => ( | ||
<Document title={title}> | ||
<Page> | ||
<Text>Hello tests</Text> | ||
</Page> | ||
</Document> | ||
); | ||
|
||
describe('BlobProvider', () => { | ||
it('works', async () => { | ||
const renderFunction = jest.fn(); | ||
|
||
render( | ||
<BlobProvider document={<TestDocument />}>{renderFunction}</BlobProvider>, | ||
); | ||
|
||
await waitFor(() => expect(renderFunction).toBeCalledTimes(3)); | ||
|
||
expect(renderFunction).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
blob: expect.anything(), | ||
url: expect.anything(), | ||
error: null, | ||
loading: false, | ||
}), | ||
); | ||
}); | ||
}); | ||
|
||
describe('BlobProvider', () => { | ||
it('works', async () => { | ||
const renderFunction = jest.fn(); | ||
|
||
render( | ||
<BlobProvider document={<TestDocument />}>{renderFunction}</BlobProvider>, | ||
); | ||
|
||
await waitFor(() => expect(renderFunction).toBeCalledTimes(3)); | ||
|
||
expect(renderFunction).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
blob: expect.anything(), | ||
url: expect.anything(), | ||
error: null, | ||
loading: false, | ||
}), | ||
); | ||
}); | ||
}); | ||
|
||
describe('BlobProvider', () => { | ||
it('works', async () => { | ||
const { container } = render( | ||
<PDFViewer> | ||
<TestDocument /> | ||
</PDFViewer>, | ||
); | ||
|
||
await waitFor(() => expect(container.querySelector('iframe'))); | ||
}); | ||
}); | ||
|
||
describe('PDFDownloadLink', () => { | ||
it('works', async () => { | ||
render(<PDFDownloadLink document={<TestDocument />}>test</PDFDownloadLink>); | ||
|
||
const link = await screen.findByText('test'); | ||
expect(link.getAttribute('download')).toBe('document.pdf'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
const Environment = require('jest-environment-jsdom').default; | ||
const { TextEncoder, TextDecoder } = require('util'); | ||
|
||
class CustomEnvironment extends Environment { | ||
async setup() { | ||
await super.setup(); | ||
|
||
if (typeof this.global.TextEncoder === 'undefined') { | ||
this.global.TextEncoder = TextEncoder; | ||
this.global.TextDecoder = TextDecoder; | ||
} | ||
|
||
if (typeof this.global.TextDecoder === 'undefined') { | ||
this.global.TextDecoder = TextDecoder; | ||
} | ||
|
||
if (typeof this.global.URL.createObjectURL === 'undefined') { | ||
this.global.URL.createObjectURL = blob => `[Blob - ${blob.size}]`; | ||
} | ||
|
||
if (typeof this.global.URL.revokeObjectURL === 'undefined') { | ||
this.global.URL.revokeObjectURL = () => undefined; | ||
} | ||
} | ||
} | ||
|
||
module.exports = CustomEnvironment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @jest-environment ./tests/environment/jsdom.js | ||
*/ | ||
|
||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { renderHook, waitFor, act } from '@testing-library/react'; | ||
import { usePDF, Document, Page, Text } from '../src/dom'; | ||
|
||
const TestDocument = ({ title = 'Default' }) => ( | ||
<Document title={title}> | ||
<Page> | ||
<Text>Hello tests</Text> | ||
</Page> | ||
</Document> | ||
); | ||
|
||
it('returns value, updater tuple', () => { | ||
const { result } = renderHook(() => usePDF({ document: undefined })); | ||
|
||
expect(Array.isArray(result.current)).toBeTruthy(); | ||
expect(result.current[0]).toMatchObject( | ||
expect.objectContaining({ | ||
url: null, | ||
blob: null, | ||
error: null, | ||
loading: false, | ||
}), | ||
); | ||
|
||
expect(typeof result.current[1]).toBe('function'); | ||
}); | ||
|
||
it('works with no args', () => { | ||
const { result } = renderHook(() => usePDF()); | ||
|
||
expect(Array.isArray(result.current)).toBeTruthy(); | ||
expect(typeof result.current[0]).toBe('object'); | ||
expect(typeof result.current[1]).toBe('function'); | ||
}); | ||
|
||
it('renders document', async () => { | ||
const { result } = renderHook(() => usePDF({ document: <TestDocument /> })); | ||
|
||
await waitFor(() => expect(result.current[0].loading).toBeFalsy()); | ||
}); | ||
|
||
it('updates document', async () => { | ||
const { result } = renderHook(() => usePDF({ document: <TestDocument /> })); | ||
|
||
await waitFor(() => expect(result.current[0].loading).toBeFalsy()); | ||
|
||
const pdfSize = result.current[0].blob.size; | ||
|
||
act(() => result.current[1](<TestDocument title="Long long long title" />)); | ||
|
||
await waitFor(() => expect(result.current[0].loading).toBeFalsy()); | ||
|
||
expect(result.current[0].blob.size).not.toEqual(pdfSize); | ||
}); |
Oops, something went wrong.