2021-04-28 07:41:20 -04:00
|
|
|
import {
|
|
|
|
screen,
|
|
|
|
waitForElementToBeRemoved,
|
|
|
|
fireEvent,
|
|
|
|
} from '@testing-library/react'
|
|
|
|
import fetchMock from 'fetch-mock'
|
|
|
|
|
2021-05-21 07:32:51 -04:00
|
|
|
import { renderWithEditorContext } from '../../../helpers/render-with-context'
|
2023-09-28 11:32:24 -04:00
|
|
|
import FileView from '../../../../../frontend/js/features/file-view/components/file-view'
|
2021-04-28 07:41:20 -04:00
|
|
|
|
2021-06-10 07:26:04 -04:00
|
|
|
describe('<FileView/>', function () {
|
2021-04-28 07:41:20 -04:00
|
|
|
const textFile = {
|
2021-10-11 09:45:36 -04:00
|
|
|
id: 'text-file',
|
2021-04-28 07:41:20 -04:00
|
|
|
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',
|
|
|
|
},
|
|
|
|
created: new Date(2021, 1, 17, 3, 24).toISOString(),
|
|
|
|
}
|
|
|
|
|
|
|
|
const imageFile = {
|
|
|
|
id: '60097ca20454610027c442a8',
|
|
|
|
name: 'file.jpg',
|
|
|
|
linkedFileData: {
|
|
|
|
source_entity_path: '/source-entity-path',
|
|
|
|
provider: 'project_file',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
fetchMock.reset()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('for a text file', function () {
|
2021-05-21 07:32:51 -04:00
|
|
|
it('shows a loading indicator while the file is loading', async function () {
|
2021-10-11 09:45:36 -04:00
|
|
|
fetchMock.head('express:/project/:project_id/file/:file_id', {
|
|
|
|
status: 201,
|
|
|
|
headers: { 'Content-Length': 10000 },
|
|
|
|
})
|
|
|
|
fetchMock.get(
|
|
|
|
'express:/project/:project_id/file/:file_id',
|
|
|
|
'Text file content'
|
|
|
|
)
|
|
|
|
|
2023-09-28 09:42:31 -04:00
|
|
|
renderWithEditorContext(<FileView file={textFile} />)
|
2021-04-28 07:41:20 -04:00
|
|
|
|
|
|
|
await waitForElementToBeRemoved(() =>
|
2024-10-10 03:38:28 -04:00
|
|
|
screen.getByTestId('loading-panel-file-view')
|
2021-04-28 07:41:20 -04:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2021-05-21 07:32:51 -04:00
|
|
|
it('shows messaging if the text view could not be loaded', async function () {
|
2021-10-11 09:45:36 -04:00
|
|
|
const unpreviewableTextFile = {
|
|
|
|
...textFile,
|
|
|
|
name: 'example.not-tex',
|
|
|
|
}
|
|
|
|
|
2023-09-28 09:42:31 -04:00
|
|
|
renderWithEditorContext(<FileView file={unpreviewableTextFile} />)
|
2021-04-28 07:41:20 -04:00
|
|
|
|
|
|
|
await screen.findByText('Sorry, no preview is available', {
|
|
|
|
exact: false,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('for an image file', function () {
|
2021-05-21 07:32:51 -04:00
|
|
|
it('shows a loading indicator while the file is loading', async function () {
|
2023-09-28 09:42:31 -04:00
|
|
|
renderWithEditorContext(<FileView file={imageFile} />)
|
2021-04-28 07:41:20 -04:00
|
|
|
|
2024-10-10 03:38:28 -04:00
|
|
|
screen.getByTestId('loading-panel-file-view')
|
2021-04-28 07:41:20 -04:00
|
|
|
})
|
|
|
|
|
2021-06-23 04:09:23 -04:00
|
|
|
it('shows messaging if the image could not be loaded', async function () {
|
2023-09-28 09:42:31 -04:00
|
|
|
renderWithEditorContext(<FileView file={imageFile} />)
|
2021-04-28 07:41:20 -04:00
|
|
|
|
|
|
|
// Fake the image request failing as the request is handled by the browser
|
|
|
|
fireEvent.error(screen.getByRole('img'))
|
|
|
|
|
2021-06-23 04:09:23 -04:00
|
|
|
await screen.findByText('Sorry, no preview is available', {
|
|
|
|
exact: false,
|
|
|
|
})
|
2021-04-28 07:41:20 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|