file-view.test.js 2.2 KB

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