Skip to content

Commit

Permalink
feat(FilePage): add file not found state
Browse files Browse the repository at this point in the history
  • Loading branch information
MellyGray committed Dec 13, 2023
1 parent df0a00b commit 4090af5
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/files/domain/repositories/FileRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ export interface FileRepository {
criteria?: FileCriteria
) => Promise<number>
getUserPermissionsById: (id: number) => Promise<FileUserPermissions>
getById: (id: number) => Promise<File>
getById: (id: number) => Promise<File | undefined>
}
5 changes: 4 additions & 1 deletion src/files/domain/useCases/getFileById.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { FileRepository } from '../repositories/FileRepository'
import { File } from '../models/File'

export async function getFileById(repository: FileRepository, id: number): Promise<File> {
export async function getFileById(
repository: FileRepository,
id: number
): Promise<File | undefined> {
return repository.getById(id).catch((error: Error) => {
throw new Error(error.message)
})
Expand Down
2 changes: 1 addition & 1 deletion src/sections/file/useFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
5 changes: 5 additions & 0 deletions src/stories/files/File.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof File> = {
title: 'Pages/File',
Expand All @@ -21,3 +22,7 @@ export const Default: Story = {
export const Loading: Story = {
render: () => <File repository={new FileMockLoadingRepository()} id={56} />
}

export const FileNotFound: Story = {
render: () => <File repository={new FileMockNoDataRepository()} id={56} />
}
10 changes: 10 additions & 0 deletions src/stories/files/FileMockNoDataRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<File | undefined> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(undefined)
}, 1000)
})
}
}
2 changes: 1 addition & 1 deletion src/stories/files/FileMockRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class FileMockRepository implements FileRepository {
}

// eslint-disable-next-line unused-imports/no-unused-vars
getById(id: number): Promise<File> {
getById(id: number): Promise<File | undefined> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(FileMother.createRealistic())
Expand Down
8 changes: 8 additions & 0 deletions tests/component/sections/file/File.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<File repository={fileRepository} id={19} />)

cy.findByText('Page Not Found').should('exist')
})
})

0 comments on commit 4090af5

Please sign in to comment.