pdf-js-viewer.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { expect } from 'chai'
  2. import { screen } from '@testing-library/react'
  3. import path from 'path'
  4. import nock from 'nock'
  5. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  6. import { pathToFileURL } from 'url'
  7. import PdfJsViewer from '../../../../../frontend/js/features/pdf-preview/components/pdf-js-viewer'
  8. const example = pathToFileURL(
  9. path.join(__dirname, '../fixtures/test-example.pdf')
  10. ).toString()
  11. const exampleCorrupt = pathToFileURL(
  12. path.join(__dirname, '../fixtures/test-example-corrupt.pdf')
  13. ).toString()
  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. nock('https://www.test-overleaf.com')
  38. .get('/invalid/doc.pdf')
  39. .replyWithError({
  40. message: 'something awful happened',
  41. code: 'AWFUL_ERROR',
  42. })
  43. renderWithEditorContext(<PdfJsViewer url="/invalid/doc.pdf" />)
  44. await screen.findByRole('alert')
  45. screen.getByText('something awful happened')
  46. expect(screen.queryByLabelText('Page 1')).to.not.exist
  47. })
  48. it('can load another document after the error', async function () {
  49. nock('https://www.test-overleaf.com')
  50. .get('/invalid/doc.pdf')
  51. .replyWithError({
  52. message: 'something awful happened',
  53. code: 'AWFUL_ERROR',
  54. })
  55. const { rerender } = renderWithEditorContext(
  56. <PdfJsViewer url="/invalid/doc.pdf" />
  57. )
  58. await screen.findByRole('alert')
  59. screen.getByText('something awful happened')
  60. rerender(<PdfJsViewer url={example} />)
  61. await screen.findByLabelText('Page 1')
  62. expect(screen.queryByRole('alert')).to.not.exist
  63. })
  64. })
  65. describe('with an corrupted document', function () {
  66. it('renders an error alert', async function () {
  67. renderWithEditorContext(<PdfJsViewer url={exampleCorrupt} />)
  68. await screen.findByRole('alert')
  69. expect(screen.queryByLabelText('Page 1')).to.not.exist
  70. })
  71. it('can load another document after the error', async function () {
  72. const { rerender } = renderWithEditorContext(
  73. <PdfJsViewer url={exampleCorrupt} />
  74. )
  75. await screen.findByRole('alert')
  76. rerender(<PdfJsViewer url={example} />)
  77. await screen.findByLabelText('Page 1')
  78. expect(screen.queryByRole('alert')).to.not.exist
  79. })
  80. })
  81. })