file-view-header.test.js 4.6 KB

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