binary-file-header.test.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React from 'react'
  2. import {
  3. screen,
  4. fireEvent,
  5. waitForElementToBeRemoved,
  6. } from '@testing-library/react'
  7. import { expect } from 'chai'
  8. import fetchMock from 'fetch-mock'
  9. import sinon from 'sinon'
  10. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  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. beforeEach(function () {
  48. fetchMock.reset()
  49. })
  50. describe('header text', function () {
  51. it('Renders the correct text for a file with the url provider', function () {
  52. renderWithEditorContext(
  53. <BinaryFileHeader file={urlFile} storeReferencesKeys={() => {}} />
  54. )
  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. renderWithEditorContext(
  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. renderWithEditorContext(
  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. it('Changes text when the file is refreshing', async function () {
  86. fetchMock.post(
  87. 'express:/project/:project_id/linked_file/:file_id/refresh',
  88. {
  89. new_file_id: '5ff7418157b4e144321df5c4',
  90. }
  91. )
  92. renderWithEditorContext(
  93. <BinaryFileHeader file={projectFile} storeReferencesKeys={() => {}} />
  94. )
  95. fireEvent.click(screen.getByRole('button', { name: 'Refresh' }))
  96. await waitForElementToBeRemoved(() =>
  97. screen.getByText('Refreshing', { exact: false })
  98. )
  99. await screen.findByText('Refresh')
  100. })
  101. it('Reindexes references after refreshing a file from a third-party provider', async function () {
  102. fetchMock.post(
  103. 'express:/project/:project_id/linked_file/:file_id/refresh',
  104. {
  105. new_file_id: '5ff7418157b4e144321df5c4',
  106. }
  107. )
  108. const reindexResponse = {
  109. projectId: '123abc',
  110. keys: ['reference1', 'reference2', 'reference3', 'reference4'],
  111. }
  112. fetchMock.post(
  113. 'express:/project/:project_id/references/indexAll',
  114. reindexResponse
  115. )
  116. const storeReferencesKeys = sinon.stub()
  117. renderWithEditorContext(
  118. <BinaryFileHeader
  119. file={thirdPartyReferenceFile}
  120. storeReferencesKeys={storeReferencesKeys}
  121. />
  122. )
  123. fireEvent.click(screen.getByRole('button', { name: 'Refresh' }))
  124. await waitForElementToBeRemoved(() =>
  125. screen.getByText('Refreshing', { exact: false })
  126. )
  127. expect(fetchMock.done()).to.be.true
  128. expect(storeReferencesKeys).to.be.calledWith(reindexResponse.keys)
  129. })
  130. })
  131. describe('The download button', function () {
  132. it('exists', function () {
  133. renderWithEditorContext(
  134. <BinaryFileHeader file={urlFile} storeReferencesKeys={() => {}} />
  135. )
  136. screen.getByText('Download', { exact: false })
  137. })
  138. })
  139. })