binary-file-header.test.js 4.6 KB

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