Set of helpers for react-pdf
visual testing with jest-image-snapshot
.
npm i -D react-pdf-testing-library
import { Resume } from '..'
import { renderDocument } from 'react-pdf-testing-library'
it('should work', async () => {
const document = await renderDocument(<Resume />)
expect(document.pagesNumber).toBe(2)
expect(await document.page(0).imageSnapshot()).toMatchImageSnapshot()
expect(await document.page(1).imageSnapshot()).toMatchImageSnapshot()
})
takes react-pdf component and size of page and returns helpers.
const wrapper = await renderComponent(<Component />, { size: 'A5' })
Size is optional. can be string (
A2
,A3
,A4
) or tuple ([number, number]
) with width and height size in pixels. default value isA4
returns raw png image of the component.
const wrapper = await renderComponent(<Component />)
expect(await wrapper.imageSnapshot()).toMatchImageSnapshot()
checks that link with href exists in the component.
const wrapper = await renderComponent(<Component />)
expect(await wrapper.containsLinkTo('https://example.com')).toBe(true)
checks that component contains goto construction with specified id.
const wrapper = await renderComponent(<Component />)
expect(await wrapper.containsAnchorTo('#myDest')).toBe(true)
takes react-pdf component or raw pdf and returns document helpers.
returns amount of pages in pdf.
const document = await renderDocument(<Component />)
expect(document.pagesNumber).toBe(10)
returns object with imageSnapshot
, containsLinkTo
and containsAnchorTo
helpers for page with specified index, index starts from 0.
helpers run over selected page
const document = await renderDocument(<Component />)
expect(await document.page(0).imageSnapshot()).toMatchImageSnapshot()
expect(await document.page(1).containsAnchorTo('#myDest')).toBe(true)
expect(await document.page(2).containsLinkTo('https://example.com')).toBe(true)
Clone this repo, cd
into it, make sure you’re using Node 12+, and then:
npm i
npm run build
Then run jest
npm test
MIT