file-view.test.jsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. created: new Date(2021, 1, 17, 3, 24).toISOString(),
  20. }
  21. const imageFile = {
  22. id: '60097ca20454610027c442a8',
  23. name: 'file.jpg',
  24. linkedFileData: {
  25. source_entity_path: '/source-entity-path',
  26. provider: 'project_file',
  27. },
  28. }
  29. beforeEach(function () {
  30. fetchMock.reset()
  31. })
  32. describe('for a text file', function () {
  33. it('shows a loading indicator while the file is loading', async function () {
  34. fetchMock.head('express:/project/:project_id/file/:file_id', {
  35. status: 201,
  36. headers: { 'Content-Length': 10000 },
  37. })
  38. fetchMock.get(
  39. 'express:/project/:project_id/file/:file_id',
  40. 'Text file content'
  41. )
  42. renderWithEditorContext(<FileView file={textFile} />)
  43. await waitForElementToBeRemoved(() =>
  44. screen.getByText('Loading', { exact: false })
  45. )
  46. })
  47. it('shows messaging if the text view could not be loaded', async function () {
  48. const unpreviewableTextFile = {
  49. ...textFile,
  50. name: 'example.not-tex',
  51. }
  52. renderWithEditorContext(<FileView file={unpreviewableTextFile} />)
  53. await screen.findByText('Sorry, no preview is available', {
  54. exact: false,
  55. })
  56. })
  57. })
  58. describe('for an image file', function () {
  59. it('shows a loading indicator while the file is loading', async function () {
  60. renderWithEditorContext(<FileView file={imageFile} />)
  61. screen.getByText('Loading', { exact: false })
  62. })
  63. it('shows messaging if the image could not be loaded', async function () {
  64. renderWithEditorContext(<FileView file={imageFile} />)
  65. // Fake the image request failing as the request is handled by the browser
  66. fireEvent.error(screen.getByRole('img'))
  67. await screen.findByText('Sorry, no preview is available', {
  68. exact: false,
  69. })
  70. })
  71. })
  72. })