file-view-header.test.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import {
  2. screen,
  3. fireEvent,
  4. waitForElementToBeRemoved,
  5. } from '@testing-library/react'
  6. import { expect } from 'chai'
  7. import fetchMock from 'fetch-mock'
  8. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  9. import FileViewHeader from '../../../../../frontend/js/features/file-view/components/file-view-header'
  10. describe('<FileViewHeader/>', function () {
  11. const urlFile = {
  12. name: 'example.tex',
  13. linkedFileData: {
  14. url: 'https://overleaf.com',
  15. provider: 'url',
  16. },
  17. created: new Date(2021, 1, 17, 3, 24).toISOString(),
  18. }
  19. const projectFile = {
  20. name: 'example.tex',
  21. linkedFileData: {
  22. v1_source_doc_id: 'v1-source-id',
  23. source_project_id: 'source-project-id',
  24. source_entity_path: '/source-entity-path.ext',
  25. provider: 'project_file',
  26. },
  27. created: new Date(2021, 1, 17, 3, 24).toISOString(),
  28. }
  29. const projectOutputFile = {
  30. name: 'example.pdf',
  31. linkedFileData: {
  32. v1_source_doc_id: 'v1-source-id',
  33. source_output_file_path: '/source-entity-path.ext',
  34. provider: 'project_output_file',
  35. },
  36. created: new Date(2021, 1, 17, 3, 24).toISOString(),
  37. }
  38. const thirdPartyReferenceFile = {
  39. name: 'example.tex',
  40. linkedFileData: {
  41. provider: 'zotero',
  42. },
  43. created: new Date(2021, 1, 17, 3, 24).toISOString(),
  44. }
  45. beforeEach(function () {
  46. fetchMock.reset()
  47. })
  48. describe('header text', function () {
  49. it('Renders the correct text for a file with the url provider', function () {
  50. renderWithEditorContext(<FileViewHeader file={urlFile} />)
  51. screen.getByText('Imported from', { exact: false })
  52. screen.getByText('at 3:24 am Wed, 17th Feb 21', {
  53. exact: false,
  54. })
  55. })
  56. it('Renders the correct text for a file with the project_file provider', function () {
  57. renderWithEditorContext(<FileViewHeader file={projectFile} />)
  58. screen.getByText('Imported from', { exact: false })
  59. screen.getByText('Another project', { exact: false })
  60. screen.getByText('/source-entity-path.ext, at 3:24 am Wed, 17th Feb 21', {
  61. exact: false,
  62. })
  63. })
  64. it('Renders the correct text for a file with the project_output_file provider', function () {
  65. renderWithEditorContext(
  66. <FileViewHeader
  67. file={projectOutputFile}
  68. storeReferencesKeys={() => {}}
  69. />
  70. )
  71. screen.getByText('Imported from the output of', { exact: false })
  72. screen.getByText('Another project', { exact: false })
  73. screen.getByText('/source-entity-path.ext, at 3:24 am Wed, 17th Feb 21', {
  74. exact: false,
  75. })
  76. })
  77. })
  78. describe('The refresh button', async function () {
  79. it('Changes text when the file is refreshing', async function () {
  80. fetchMock.post(
  81. 'express:/project/:project_id/linked_file/:file_id/refresh',
  82. {
  83. new_file_id: '5ff7418157b4e144321df5c4',
  84. }
  85. )
  86. renderWithEditorContext(<FileViewHeader file={projectFile} />)
  87. fireEvent.click(screen.getByRole('button', { name: 'Refresh' }))
  88. await waitForElementToBeRemoved(() =>
  89. screen.getByText('Refreshing', { exact: false })
  90. )
  91. await screen.findByText('Refresh')
  92. })
  93. it('Reindexes references after refreshing a file from a third-party provider', async function () {
  94. fetchMock.post(
  95. 'express:/project/:project_id/linked_file/:file_id/refresh',
  96. {
  97. new_file_id: '5ff7418157b4e144321df5c4',
  98. }
  99. )
  100. renderWithEditorContext(<FileViewHeader file={thirdPartyReferenceFile} />)
  101. fireEvent.click(screen.getByRole('button', { name: 'Refresh' }))
  102. await waitForElementToBeRemoved(() =>
  103. screen.getByText('Refreshing', { exact: false })
  104. )
  105. expect(fetchMock.done()).to.be.true
  106. const lastCallBody = JSON.parse(fetchMock.lastCall()[1].body)
  107. expect(lastCallBody.shouldReindexReferences).to.be.true
  108. })
  109. })
  110. describe('The download button', function () {
  111. it('exists', function () {
  112. renderWithEditorContext(<FileViewHeader file={urlFile} />)
  113. screen.getByText('Download', { exact: false })
  114. })
  115. })
  116. })