file-view-refresh-button.test.jsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 FileViewRefreshButton from '@/features/file-view/components/file-view-refresh-button'
  9. import { renderWithEditorContext } from '../../../helpers/render-with-context'
  10. import { USER_ID } from '../../../helpers/editor-providers'
  11. describe('<FileViewRefreshButton />', function () {
  12. const projectFile = {
  13. name: 'example.tex',
  14. linkedFileData: {
  15. v1_source_doc_id: 'v1-source-id',
  16. source_project_id: 'source-project-id',
  17. source_entity_path: '/source-entity-path.ext',
  18. provider: 'project_file',
  19. importer_id: USER_ID,
  20. },
  21. created: new Date(2021, 1, 17, 3, 24).toISOString(),
  22. }
  23. const thirdPartyReferenceFile = {
  24. name: 'example.tex',
  25. linkedFileData: {
  26. provider: 'zotero',
  27. importer_id: USER_ID,
  28. },
  29. created: new Date(2021, 1, 17, 3, 24).toISOString(),
  30. }
  31. const thirdPartyNotOriginalImporterReferenceFile = {
  32. name: 'references.bib',
  33. linkedFileData: {
  34. v1_source_doc_id: 'v1-source-id',
  35. source_project_id: 'source-project-id',
  36. source_entity_path: '/source-entity-path.ext',
  37. provider: 'mendeley',
  38. importer_id: '123abc',
  39. },
  40. created: new Date(2021, 1, 17, 3, 24).toISOString(),
  41. }
  42. beforeEach(function () {
  43. fetchMock.reset()
  44. })
  45. it('Changes text when the file is refreshing', async function () {
  46. fetchMock.post(
  47. 'express:/project/:project_id/linked_file/:file_id/refresh',
  48. {
  49. new_file_id: '5ff7418157b4e144321df5c4',
  50. }
  51. )
  52. renderWithEditorContext(<FileViewRefreshButton file={projectFile} />)
  53. fireEvent.click(screen.getByRole('button', { name: 'Refresh' }))
  54. await waitForElementToBeRemoved(() =>
  55. screen.getByText('Refreshing', { exact: false })
  56. )
  57. await screen.findByText('Refresh')
  58. })
  59. it('Reindexes references after refreshing a file from a third-party provider', async function () {
  60. fetchMock.post(
  61. 'express:/project/:project_id/linked_file/:file_id/refresh',
  62. {
  63. new_file_id: '5ff7418157b4e144321df5c4',
  64. }
  65. )
  66. renderWithEditorContext(
  67. <FileViewRefreshButton file={thirdPartyReferenceFile} />
  68. )
  69. fireEvent.click(screen.getByRole('button', { name: 'Refresh' }))
  70. await waitForElementToBeRemoved(() =>
  71. screen.getByText('Refreshing', { exact: false })
  72. )
  73. expect(fetchMock.done()).to.be.true
  74. const lastCallBody = JSON.parse(fetchMock.lastCall()[1].body)
  75. expect(lastCallBody.shouldReindexReferences).to.be.true
  76. })
  77. it('is disabled when user is not original importer', function () {
  78. renderWithEditorContext(
  79. <FileViewRefreshButton
  80. file={thirdPartyNotOriginalImporterReferenceFile}
  81. />
  82. )
  83. const button = screen.getByRole('button', { name: 'Refresh' })
  84. expect(button.disabled).to.equal(true)
  85. })
  86. })