file-view.test.jsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {
  2. screen,
  3. waitForElementToBeRemoved,
  4. fireEvent,
  5. } from '@testing-library/react'
  6. import fetchMock from 'fetch-mock'
  7. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  8. import FileView from '../../../../../frontend/js/features/file-view/components/file-view'
  9. describe('<FileView/>', function () {
  10. const textFile = {
  11. id: 'text-file',
  12. name: 'example.tex',
  13. linkedFileData: {
  14. v1_source_doc_id: 'v1-source-id',
  15. source_project_id: 'source-project-id',
  16. source_entity_path: '/source-entity-path.ext',
  17. provider: 'project_file',
  18. },
  19. hash: '012345678901234567890123',
  20. created: new Date(2021, 1, 17, 3, 24).toISOString(),
  21. }
  22. const imageFile = {
  23. id: '60097ca20454610027c442a8',
  24. name: 'file.jpg',
  25. linkedFileData: {
  26. source_entity_path: '/source-entity-path',
  27. provider: 'project_file',
  28. },
  29. }
  30. beforeEach(function () {
  31. fetchMock.removeRoutes().clearHistory()
  32. })
  33. describe('for a text file', function () {
  34. it('shows a loading indicator while the file is loading', async function () {
  35. fetchMock.head('express:/project/:project_id/blob/:hash', {
  36. status: 201,
  37. headers: { 'Content-Length': 10000 },
  38. })
  39. fetchMock.get(
  40. 'express:/project/:project_id/blob/:hash',
  41. 'Text file content'
  42. )
  43. renderWithEditorContext(<FileView file={textFile} />)
  44. await waitForElementToBeRemoved(() =>
  45. screen.getByTestId('loading-panel-file-view')
  46. )
  47. })
  48. it('shows messaging if the text view could not be loaded', async function () {
  49. const unpreviewableTextFile = {
  50. ...textFile,
  51. name: 'example.not-tex',
  52. }
  53. renderWithEditorContext(<FileView file={unpreviewableTextFile} />)
  54. await screen.findByText('Sorry, no preview is available', {
  55. exact: false,
  56. })
  57. })
  58. })
  59. describe('for an image file', function () {
  60. it('shows a loading indicator while the file is loading', async function () {
  61. renderWithEditorContext(<FileView file={imageFile} />)
  62. screen.getByTestId('loading-panel-file-view')
  63. })
  64. it('shows messaging if the image could not be loaded', async function () {
  65. renderWithEditorContext(<FileView file={imageFile} />)
  66. // Fake the image request failing as the request is handled by the browser
  67. fireEvent.error(screen.getByRole('img'))
  68. await screen.findByText('Sorry, no preview is available', {
  69. exact: false,
  70. })
  71. })
  72. })
  73. })