binary-file.test.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React from 'react'
  2. import {
  3. render,
  4. screen,
  5. waitForElementToBeRemoved,
  6. fireEvent,
  7. } from '@testing-library/react'
  8. import fetchMock from 'fetch-mock'
  9. import BinaryFile from '../../../../../frontend/js/features/binary-file/components/binary-file.js'
  10. describe('<BinaryFile/>', function () {
  11. const textFile = {
  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('it shows a loading indicator while the file is loading', async function () {
  34. render(<BinaryFile file={textFile} storeReferencesKeys={() => {}} />)
  35. await waitForElementToBeRemoved(() =>
  36. screen.getByText('Loading', { exact: false })
  37. )
  38. })
  39. it('it shows messaging if the text view could not be loaded', async function () {
  40. render(<BinaryFile file={textFile} storeReferencesKeys={() => {}} />)
  41. await screen.findByText('Sorry, no preview is available', {
  42. exact: false,
  43. })
  44. })
  45. })
  46. describe('for an image file', function () {
  47. it('it shows a loading indicator while the file is loading', async function () {
  48. render(<BinaryFile file={imageFile} storeReferencesKeys={() => {}} />)
  49. screen.getByText('Loading', { exact: false })
  50. })
  51. it('it shows messaging if the image could not be loaded', function () {
  52. render(<BinaryFile file={imageFile} storeReferencesKeys={() => {}} />)
  53. // Fake the image request failing as the request is handled by the browser
  54. fireEvent.error(screen.getByRole('img'))
  55. screen.findByText('Sorry, no preview is available', { exact: false })
  56. })
  57. })
  58. })