2021-04-28 07:41:20 -04:00
|
|
|
import {
|
|
|
|
screen,
|
|
|
|
fireEvent,
|
|
|
|
waitForElementToBeRemoved,
|
|
|
|
} from '@testing-library/react'
|
|
|
|
import { expect } from 'chai'
|
|
|
|
import fetchMock from 'fetch-mock'
|
|
|
|
import sinon from 'sinon'
|
|
|
|
|
2021-05-21 07:32:51 -04:00
|
|
|
import { renderWithEditorContext } from '../../../helpers/render-with-context'
|
2021-06-10 07:26:04 -04:00
|
|
|
import FileViewHeader from '../../../../../frontend/js/features/file-view/components/file-view-header.js'
|
2021-04-28 07:41:20 -04:00
|
|
|
|
2021-06-10 07:26:04 -04:00
|
|
|
describe('<FileViewHeader/>', function () {
|
2021-04-28 07:41:20 -04:00
|
|
|
const urlFile = {
|
|
|
|
name: 'example.tex',
|
|
|
|
linkedFileData: {
|
|
|
|
url: 'https://overleaf.com',
|
|
|
|
provider: 'url',
|
|
|
|
},
|
|
|
|
created: new Date(2021, 1, 17, 3, 24).toISOString(),
|
|
|
|
}
|
|
|
|
|
|
|
|
const projectFile = {
|
|
|
|
name: 'example.tex',
|
|
|
|
linkedFileData: {
|
|
|
|
v1_source_doc_id: 'v1-source-id',
|
|
|
|
source_project_id: 'source-project-id',
|
|
|
|
source_entity_path: '/source-entity-path.ext',
|
|
|
|
provider: 'project_file',
|
2023-07-12 09:16:38 -04:00
|
|
|
importer_id: '123abd',
|
|
|
|
},
|
|
|
|
created: new Date(2021, 1, 17, 3, 24).toISOString(),
|
|
|
|
}
|
|
|
|
|
|
|
|
const notOrignalImporterFile = {
|
|
|
|
name: 'references.bib',
|
|
|
|
linkedFileData: {
|
|
|
|
v1_source_doc_id: 'v1-source-id',
|
|
|
|
source_project_id: 'source-project-id',
|
|
|
|
source_entity_path: '/source-entity-path.ext',
|
|
|
|
provider: 'project_file',
|
|
|
|
importer_id: '123abc',
|
2021-04-28 07:41:20 -04:00
|
|
|
},
|
|
|
|
created: new Date(2021, 1, 17, 3, 24).toISOString(),
|
|
|
|
}
|
|
|
|
|
|
|
|
const projectOutputFile = {
|
|
|
|
name: 'example.pdf',
|
|
|
|
linkedFileData: {
|
|
|
|
v1_source_doc_id: 'v1-source-id',
|
|
|
|
source_output_file_path: '/source-entity-path.ext',
|
|
|
|
provider: 'project_output_file',
|
|
|
|
},
|
|
|
|
created: new Date(2021, 1, 17, 3, 24).toISOString(),
|
|
|
|
}
|
|
|
|
|
|
|
|
const thirdPartyReferenceFile = {
|
|
|
|
name: 'example.tex',
|
|
|
|
linkedFileData: {
|
|
|
|
provider: 'zotero',
|
2023-07-12 09:16:38 -04:00
|
|
|
importer_id: '123abd',
|
2021-04-28 07:41:20 -04:00
|
|
|
},
|
|
|
|
created: new Date(2021, 1, 17, 3, 24).toISOString(),
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
fetchMock.reset()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('header text', function () {
|
|
|
|
it('Renders the correct text for a file with the url provider', function () {
|
2021-05-21 07:32:51 -04:00
|
|
|
renderWithEditorContext(
|
2021-06-10 07:26:04 -04:00
|
|
|
<FileViewHeader file={urlFile} storeReferencesKeys={() => {}} />
|
2021-05-21 07:32:51 -04:00
|
|
|
)
|
2021-04-28 07:41:20 -04:00
|
|
|
screen.getByText('Imported from', { exact: false })
|
|
|
|
screen.getByText('at 3:24 am Wed, 17th Feb 21', {
|
|
|
|
exact: false,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Renders the correct text for a file with the project_file provider', function () {
|
2021-05-21 07:32:51 -04:00
|
|
|
renderWithEditorContext(
|
2021-06-10 07:26:04 -04:00
|
|
|
<FileViewHeader file={projectFile} storeReferencesKeys={() => {}} />
|
2021-04-28 07:41:20 -04:00
|
|
|
)
|
|
|
|
screen.getByText('Imported from', { exact: false })
|
|
|
|
screen.getByText('Another project', { exact: false })
|
|
|
|
screen.getByText('/source-entity-path.ext, at 3:24 am Wed, 17th Feb 21', {
|
|
|
|
exact: false,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Renders the correct text for a file with the project_output_file provider', function () {
|
2021-05-21 07:32:51 -04:00
|
|
|
renderWithEditorContext(
|
2021-06-10 07:26:04 -04:00
|
|
|
<FileViewHeader
|
2021-04-28 07:41:20 -04:00
|
|
|
file={projectOutputFile}
|
|
|
|
storeReferencesKeys={() => {}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
screen.getByText('Imported from the output of', { exact: false })
|
|
|
|
screen.getByText('Another project', { exact: false })
|
|
|
|
screen.getByText('/source-entity-path.ext, at 3:24 am Wed, 17th Feb 21', {
|
|
|
|
exact: false,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('The refresh button', async function () {
|
|
|
|
it('Changes text when the file is refreshing', async function () {
|
|
|
|
fetchMock.post(
|
|
|
|
'express:/project/:project_id/linked_file/:file_id/refresh',
|
|
|
|
{
|
|
|
|
new_file_id: '5ff7418157b4e144321df5c4',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-05-21 07:32:51 -04:00
|
|
|
renderWithEditorContext(
|
2021-06-10 07:26:04 -04:00
|
|
|
<FileViewHeader file={projectFile} storeReferencesKeys={() => {}} />
|
2021-04-28 07:41:20 -04:00
|
|
|
)
|
|
|
|
|
2023-07-12 09:16:38 -04:00
|
|
|
const refreshButton = screen.getByRole('button', { name: 'Refresh' })
|
|
|
|
fireEvent.click(refreshButton)
|
2021-04-28 07:41:20 -04:00
|
|
|
|
|
|
|
await waitForElementToBeRemoved(() =>
|
|
|
|
screen.getByText('Refreshing', { exact: false })
|
|
|
|
)
|
2023-07-12 09:16:38 -04:00
|
|
|
|
2021-04-28 07:41:20 -04:00
|
|
|
await screen.findByText('Refresh')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Reindexes references after refreshing a file from a third-party provider', async function () {
|
|
|
|
fetchMock.post(
|
|
|
|
'express:/project/:project_id/linked_file/:file_id/refresh',
|
|
|
|
{
|
|
|
|
new_file_id: '5ff7418157b4e144321df5c4',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-05-21 07:32:51 -04:00
|
|
|
const reindexResponse = {
|
|
|
|
projectId: '123abc',
|
|
|
|
keys: ['reference1', 'reference2', 'reference3', 'reference4'],
|
|
|
|
}
|
2021-04-28 07:41:20 -04:00
|
|
|
fetchMock.post(
|
|
|
|
'express:/project/:project_id/references/indexAll',
|
|
|
|
reindexResponse
|
|
|
|
)
|
|
|
|
|
2021-05-21 07:32:51 -04:00
|
|
|
const storeReferencesKeys = sinon.stub()
|
|
|
|
|
|
|
|
renderWithEditorContext(
|
2021-06-10 07:26:04 -04:00
|
|
|
<FileViewHeader
|
2021-04-28 07:41:20 -04:00
|
|
|
file={thirdPartyReferenceFile}
|
|
|
|
storeReferencesKeys={storeReferencesKeys}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
2023-07-12 09:16:38 -04:00
|
|
|
const refreshButton = screen.getByRole('button', { name: 'Refresh' })
|
|
|
|
fireEvent.click(refreshButton)
|
2021-04-28 07:41:20 -04:00
|
|
|
|
|
|
|
await waitForElementToBeRemoved(() =>
|
|
|
|
screen.getByText('Refreshing', { exact: false })
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(fetchMock.done()).to.be.true
|
2023-07-12 09:16:38 -04:00
|
|
|
expect(storeReferencesKeys).to.have.been.calledWith(reindexResponse.keys)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Displays message when user is not original importer', function () {
|
|
|
|
renderWithEditorContext(
|
|
|
|
<FileViewHeader
|
|
|
|
file={notOrignalImporterFile}
|
|
|
|
storeReferencesKeys={() => {}}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
|
|
|
|
const refreshButton = screen.getByRole('button', { name: 'Refresh' })
|
|
|
|
if (refreshButton.disabled) {
|
|
|
|
const textBefore = screen.getByText(
|
|
|
|
'Only the person who originally imported this',
|
|
|
|
{ exact: false }
|
|
|
|
)
|
|
|
|
expect(textBefore).to.exist
|
|
|
|
|
|
|
|
const textAfter = screen.getByText('file can refresh it', {
|
|
|
|
exact: false,
|
|
|
|
})
|
|
|
|
expect(textAfter).to.exist
|
|
|
|
}
|
2021-04-28 07:41:20 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('The download button', function () {
|
|
|
|
it('exists', function () {
|
2021-05-21 07:32:51 -04:00
|
|
|
renderWithEditorContext(
|
2021-06-10 07:26:04 -04:00
|
|
|
<FileViewHeader file={urlFile} storeReferencesKeys={() => {}} />
|
2021-05-21 07:32:51 -04:00
|
|
|
)
|
2021-04-28 07:41:20 -04:00
|
|
|
|
|
|
|
screen.getByText('Download', { exact: false })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|