Files
overleaf-cep/services/web/test/frontend/features/file-view/components/file-view.test.tsx
T
David 9e6db89311 Merge pull request #27811 from overleaf/dp-file-view-typescript
Convert file-view components and test files to typescript

GitOrigin-RevId: 277aa8fd4f3d06a322dc9d0b372eebefb26fd285
2025-08-14 08:05:43 +00:00

69 lines
2.0 KiB
TypeScript

import {
screen,
waitForElementToBeRemoved,
fireEvent,
} from '@testing-library/react'
import fetchMock from 'fetch-mock'
import { renderWithEditorContext } from '../../../helpers/render-with-context'
import FileView from '../../../../../frontend/js/features/file-view/components/file-view'
import { imageFile, textFile } from '../util/files'
describe('<FileView/>', function () {
beforeEach(function () {
fetchMock.removeRoutes().clearHistory()
window.metaAttributesCache.set('ol-preventCompileOnLoad', true)
})
describe('for a text file', function () {
it('shows a loading indicator while the file is loading', async function () {
fetchMock.head('express:/project/:project_id/blob/:hash', {
status: 201,
headers: { 'Content-Length': 10000 },
})
fetchMock.get(
'express:/project/:project_id/blob/:hash',
'Text file content'
)
renderWithEditorContext(<FileView file={textFile} />)
await waitForElementToBeRemoved(() =>
screen.getByTestId('loading-panel-file-view')
)
})
it('shows messaging if the text view could not be loaded', async function () {
const unpreviewableTextFile = {
...textFile,
name: 'example.not-tex',
}
renderWithEditorContext(<FileView file={unpreviewableTextFile} />)
await screen.findByText('Sorry, no preview is available', {
exact: false,
})
})
})
describe('for an image file', function () {
it('shows a loading indicator while the file is loading', async function () {
renderWithEditorContext(<FileView file={imageFile} />)
screen.getByTestId('loading-panel-file-view')
})
it('shows messaging if the image could not be loaded', async function () {
renderWithEditorContext(<FileView file={imageFile} />)
// Fake the image request failing as the request is handled by the browser
fireEvent.error(screen.getByRole('img'))
await screen.findByText('Sorry, no preview is available', {
exact: false,
})
})
})
})