diff --git a/src/files/domain/repositories/FileRepository.ts b/src/files/domain/repositories/FileRepository.ts index 8b46323e4..666f7ae2d 100644 --- a/src/files/domain/repositories/FileRepository.ts +++ b/src/files/domain/repositories/FileRepository.ts @@ -24,5 +24,5 @@ export interface FileRepository { criteria?: FileCriteria ) => Promise getUserPermissionsById: (id: number) => Promise - getById: (id: number) => Promise + getById: (id: number) => Promise } diff --git a/src/files/domain/useCases/getFileById.ts b/src/files/domain/useCases/getFileById.ts index 9cefdae31..226c9e711 100644 --- a/src/files/domain/useCases/getFileById.ts +++ b/src/files/domain/useCases/getFileById.ts @@ -1,7 +1,10 @@ import { FileRepository } from '../repositories/FileRepository' import { File } from '../models/File' -export async function getFileById(repository: FileRepository, id: number): Promise { +export async function getFileById( + repository: FileRepository, + id: number +): Promise { return repository.getById(id).catch((error: Error) => { throw new Error(error.message) }) diff --git a/src/sections/file/useFile.tsx b/src/sections/file/useFile.tsx index 49ff6a7d3..91ac72510 100644 --- a/src/sections/file/useFile.tsx +++ b/src/sections/file/useFile.tsx @@ -11,7 +11,7 @@ export function useFile(repository: FileRepository, id: number) { setIsLoading(true) getFileById(repository, id) - .then((file: File) => { + .then((file: File | undefined) => { setFile(file) setIsLoading(false) }) diff --git a/src/stories/files/File.stories.tsx b/src/stories/files/File.stories.tsx index 28ea47dd4..060fe8dfc 100644 --- a/src/stories/files/File.stories.tsx +++ b/src/stories/files/File.stories.tsx @@ -4,6 +4,7 @@ import { FileMockRepository } from './FileMockRepository' import { File } from '../../sections/file/File' import { WithLayout } from '../WithLayout' import { FileMockLoadingRepository } from './FileMockLoadingRepository' +import { FileMockNoDataRepository } from './FileMockNoDataRepository' const meta: Meta = { title: 'Pages/File', @@ -21,3 +22,7 @@ export const Default: Story = { export const Loading: Story = { render: () => } + +export const FileNotFound: Story = { + render: () => +} diff --git a/src/stories/files/FileMockNoDataRepository.ts b/src/stories/files/FileMockNoDataRepository.ts index 22a1ad3a5..0db3ae4e3 100644 --- a/src/stories/files/FileMockNoDataRepository.ts +++ b/src/stories/files/FileMockNoDataRepository.ts @@ -5,6 +5,7 @@ import { FilesCountInfoMother } from '../../../tests/component/files/domain/mode import { DatasetVersion } from '../../dataset/domain/models/Dataset' import { FileCriteria } from '../../files/domain/models/FileCriteria' import { FileMockRepository } from './FileMockRepository' +import { File } from '../../files/domain/models/File' export class FileMockNoDataRepository extends FileMockRepository implements FileRepository { getAllByDatasetPersistentId( @@ -47,4 +48,13 @@ export class FileMockNoDataRepository extends FileMockRepository implements File }, 1000) }) } + + // eslint-disable-next-line unused-imports/no-unused-vars + getById(id: number): Promise { + return new Promise((resolve) => { + setTimeout(() => { + resolve(undefined) + }, 1000) + }) + } } diff --git a/src/stories/files/FileMockRepository.ts b/src/stories/files/FileMockRepository.ts index ecd9036ab..c8b9eebbf 100644 --- a/src/stories/files/FileMockRepository.ts +++ b/src/stories/files/FileMockRepository.ts @@ -63,7 +63,7 @@ export class FileMockRepository implements FileRepository { } // eslint-disable-next-line unused-imports/no-unused-vars - getById(id: number): Promise { + getById(id: number): Promise { return new Promise((resolve) => { setTimeout(() => { resolve(FileMother.createRealistic()) diff --git a/tests/component/sections/file/File.spec.tsx b/tests/component/sections/file/File.spec.tsx index 260bb676f..cfc07956e 100644 --- a/tests/component/sections/file/File.spec.tsx +++ b/tests/component/sections/file/File.spec.tsx @@ -27,4 +27,12 @@ describe('File', () => { cy.wrap(fileRepository.getById).should('be.calledWith', 19) cy.findByText(testFile.name).should('not.exist') }) + + it('renders page not found when file is not found', () => { + fileRepository.getById = cy.stub().resolves(undefined) + + cy.customMount() + + cy.findByText('Page Not Found').should('exist') + }) })