pdf-js-viewer.test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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').toString()
  9. )
  10. const exampleCorrupt = pathToFileURL(
  11. path.join(__dirname, '../fixtures/test-example-corrupt.pdf')
  12. ).toString()
  13. const invalidURL = 'http://nonexisting.com/doc'
  14. describe('<PdfJSViewer/>', function () {
  15. it('loads all PDF pages', async function () {
  16. renderWithEditorContext(<PdfJsViewer url={example} />)
  17. await screen.findByLabelText('Page 1')
  18. await screen.findByLabelText('Page 2')
  19. await screen.findByLabelText('Page 3')
  20. expect(screen.queryByLabelText('Page 4')).to.not.exist
  21. })
  22. it('renders pages in a "loading" state', async function () {
  23. renderWithEditorContext(<PdfJsViewer url={example} />)
  24. await screen.findByLabelText('Loading…')
  25. })
  26. it('can be unmounted while loading a document', async function () {
  27. const { unmount } = renderWithEditorContext(<PdfJsViewer url={example} />)
  28. unmount()
  29. })
  30. it('can be unmounted after loading a document', async function () {
  31. const { unmount } = renderWithEditorContext(<PdfJsViewer url={example} />)
  32. await screen.findByLabelText('Page 1')
  33. unmount()
  34. })
  35. describe('with an invalid URL', function () {
  36. it('renders an error alert', async function () {
  37. renderWithEditorContext(<PdfJsViewer url={invalidURL} />)
  38. await screen.findByRole('alert')
  39. expect(screen.queryByLabelText('Page 1')).to.not.exist
  40. })
  41. it('can load another document after the error', async function () {
  42. const { rerender } = renderWithEditorContext(
  43. <PdfJsViewer url={invalidURL} />
  44. )
  45. await screen.findByRole('alert')
  46. rerender(<PdfJsViewer url={example} />)
  47. await screen.findByLabelText('Page 1')
  48. expect(screen.queryByRole('alert')).to.not.exist
  49. })
  50. })
  51. describe('with an corrupted document', function () {
  52. it('renders an error alert', async function () {
  53. renderWithEditorContext(<PdfJsViewer url={exampleCorrupt} />)
  54. await screen.findByRole('alert')
  55. expect(screen.queryByLabelText('Page 1')).to.not.exist
  56. })
  57. it('can load another document after the error', async function () {
  58. const { rerender } = renderWithEditorContext(
  59. <PdfJsViewer url={exampleCorrupt} />
  60. )
  61. await screen.findByRole('alert')
  62. rerender(<PdfJsViewer url={example} />)
  63. await screen.findByLabelText('Page 1')
  64. expect(screen.queryByRole('alert')).to.not.exist
  65. })
  66. })
  67. })