file-view.test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.js'
  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(
  43. <FileView file={textFile} storeReferencesKeys={() => {}} />
  44. )
  45. await waitForElementToBeRemoved(() =>
  46. screen.getByText('Loading', { exact: false })
  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(
  55. <FileView file={unpreviewableTextFile} storeReferencesKeys={() => {}} />
  56. )
  57. await screen.findByText('Sorry, no preview is available', {
  58. exact: false,
  59. })
  60. })
  61. })
  62. describe('for an image file', function () {
  63. it('shows a loading indicator while the file is loading', async function () {
  64. renderWithEditorContext(
  65. <FileView file={imageFile} storeReferencesKeys={() => {}} />
  66. )
  67. screen.getByText('Loading', { exact: false })
  68. })
  69. it('shows messaging if the image could not be loaded', async function () {
  70. renderWithEditorContext(
  71. <FileView file={imageFile} storeReferencesKeys={() => {}} />
  72. )
  73. // Fake the image request failing as the request is handled by the browser
  74. fireEvent.error(screen.getByRole('img'))
  75. await screen.findByText('Sorry, no preview is available', {
  76. exact: false,
  77. })
  78. })
  79. })
  80. })