binary-file.test.js 2.2 KB

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