pdf-js-viewer.test.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { expect } from 'chai'
  2. import { screen } from '@testing-library/react'
  3. import path from 'path'
  4. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  5. import { pathToFileURL } from 'url'
  6. import PdfJsViewer from '../../../../../frontend/js/features/pdf-preview/components/pdf-js-viewer'
  7. const example = pathToFileURL(
  8. path.join(__dirname, '../fixtures/test-example.pdf')
  9. ).toString()
  10. describe('<PdfJSViewer/>', function () {
  11. beforeEach(function () {
  12. window.showNewPdfPreview = true
  13. })
  14. afterEach(function () {
  15. window.showNewPdfPreview = undefined
  16. })
  17. it('loads all PDF pages', async function () {
  18. renderWithEditorContext(<PdfJsViewer url={example} />)
  19. await screen.findByLabelText('Page 1')
  20. await screen.findByLabelText('Page 2')
  21. await screen.findByLabelText('Page 3')
  22. expect(screen.queryByLabelText('Page 4')).to.not.exist
  23. })
  24. it('renders pages in a "loading" state', async function () {
  25. renderWithEditorContext(<PdfJsViewer url={example} />)
  26. await screen.findByLabelText('Loading…')
  27. })
  28. it('can be unmounted while loading a document', async function () {
  29. const { unmount } = renderWithEditorContext(<PdfJsViewer url={example} />)
  30. unmount()
  31. })
  32. it('can be unmounted after loading a document', async function () {
  33. const { unmount } = renderWithEditorContext(<PdfJsViewer url={example} />)
  34. await screen.findByLabelText('Page 1')
  35. unmount()
  36. })
  37. })