file-view.test.jsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
  33. })
  34. describe('for a text file', function () {
  35. it('shows a loading indicator while the file is loading', async function () {
  36. fetchMock.head('express:/project/:project_id/blob/:hash', {
  37. status: 201,
  38. headers: { 'Content-Length': 10000 },
  39. })
  40. fetchMock.get(
  41. 'express:/project/:project_id/blob/:hash',
  42. 'Text file content'
  43. )
  44. renderWithEditorContext(<FileView file={textFile} />)
  45. await waitForElementToBeRemoved(() =>
  46. screen.getByTestId('loading-panel-file-view')
  47. )
  48. })
  49. it('shows messaging if the text view could not be loaded', async function () {
  50. const unpreviewableTextFile = {
  51. ...textFile,
  52. name: 'example.not-tex',
  53. }
  54. renderWithEditorContext(<FileView file={unpreviewableTextFile} />)
  55. await screen.findByText('Sorry, no preview is available', {
  56. exact: false,
  57. })
  58. })
  59. })
  60. describe('for an image file', function () {
  61. it('shows a loading indicator while the file is loading', async function () {
  62. renderWithEditorContext(<FileView file={imageFile} />)
  63. screen.getByTestId('loading-panel-file-view')
  64. })
  65. it('shows messaging if the image could not be loaded', async function () {
  66. renderWithEditorContext(<FileView file={imageFile} />)
  67. // Fake the image request failing as the request is handled by the browser
  68. fireEvent.error(screen.getByRole('img'))
  69. await screen.findByText('Sorry, no preview is available', {
  70. exact: false,
  71. })
  72. })
  73. })
  74. })