file-view.test.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. import { imageFile, textFile } from '../util/files'
  10. describe('<FileView/>', function () {
  11. beforeEach(function () {
  12. fetchMock.removeRoutes().clearHistory()
  13. window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
  14. })
  15. describe('for a text file', function () {
  16. it('shows a loading indicator while the file is loading', async function () {
  17. fetchMock.head('express:/project/:project_id/blob/:hash', {
  18. status: 201,
  19. headers: { 'Content-Length': 10000 },
  20. })
  21. fetchMock.get(
  22. 'express:/project/:project_id/blob/:hash',
  23. 'Text file content'
  24. )
  25. renderWithEditorContext(<FileView file={textFile} />)
  26. await waitForElementToBeRemoved(() =>
  27. screen.getByTestId('loading-panel-file-view')
  28. )
  29. })
  30. it('shows messaging if the text view could not be loaded', async function () {
  31. const unpreviewableTextFile = {
  32. ...textFile,
  33. name: 'example.not-tex',
  34. }
  35. renderWithEditorContext(<FileView file={unpreviewableTextFile} />)
  36. await screen.findByText('Sorry, no preview is available', {
  37. exact: false,
  38. })
  39. })
  40. })
  41. describe('for an image file', function () {
  42. it('shows a loading indicator while the file is loading', async function () {
  43. renderWithEditorContext(<FileView file={imageFile} />)
  44. screen.getByTestId('loading-panel-file-view')
  45. })
  46. it('shows messaging if the image could not be loaded', async function () {
  47. renderWithEditorContext(<FileView file={imageFile} />)
  48. // Fake the image request failing as the request is handled by the browser
  49. fireEvent.error(screen.getByRole('img'))
  50. await screen.findByText('Sorry, no preview is available', {
  51. exact: false,
  52. })
  53. })
  54. })
  55. })