Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List-duties-error-handling #74

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions frontend/src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ describe('App', () => {
screen.getByText('Sample Duty 2')
})

it('should handle error when remote service rejected to list duties', async () => {
dutyRemoteService.listDuties = () => {
return Promise.reject(new Error('Failed to list duties'))
}

render(<App dutyRemoteService={dutyRemoteService} />)

expect(await screen.findByText('Failed to list duties')).toBeVisible()
})

it('should able to add duty', async () => {
await dutyRemoteService.createDuty('Initial Duty')

Expand All @@ -64,14 +74,14 @@ describe('App', () => {

it('should handle error when remote service rejected to create duty', async () => {
dutyRemoteService.createDuty = () => {
return Promise.reject(new Error('Create duty failed'))
return Promise.reject(new Error('Failed to create duty'))
}

render(<App dutyRemoteService={dutyRemoteService} />)

addDutyViaUI(screen)

expect(await screen.findByText('Create duty failed')).toBeVisible()
expect(await screen.findByText('Failed to create duty')).toBeVisible()

const savedDuties = await dutyRemoteService.listDuties()
expect(savedDuties).toHaveLength(0)
Expand Down Expand Up @@ -123,7 +133,7 @@ describe('App', () => {

it('should handle error when remote service rejected to update duty', async () => {
dutyRemoteService.updateDuty = () => {
return Promise.reject(new Error('Update duty failed'))
return Promise.reject(new Error('Failed to update duty'))
}

render(<App dutyRemoteService={dutyRemoteService} />)
Expand All @@ -134,7 +144,7 @@ describe('App', () => {
originalName: 'Duty',
})

expect(await screen.findByText('Update duty failed')).toBeVisible()
expect(await screen.findByText('Failed to update duty')).toBeVisible()
})

it('should prevent editing existing duty to empty', async () => {
Expand Down Expand Up @@ -193,7 +203,7 @@ describe('App', () => {

it('should handle error when remote service rejected to complete duty', async () => {
dutyRemoteService.completeDuty = () => {
return Promise.reject(new Error('Complete duty failed'))
return Promise.reject(new Error('Failed to complete duty'))
}

render(<App dutyRemoteService={dutyRemoteService} />)
Expand All @@ -202,7 +212,7 @@ describe('App', () => {
const completeButton = await screen.findByTestId('complete-button-0')
fireEvent.click(completeButton)

expect(await screen.findByText('Complete duty failed')).toBeVisible()
expect(await screen.findByText('Failed to complete duty')).toBeVisible()
})

function addDutyViaUI(
Expand Down
13 changes: 9 additions & 4 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ const App = ({
const [messageApi, contextHolder] = message.useMessage()

const loadDutiesFromRemoteToState = useCallback(async () => {
return dutyRemoteService.listDuties().then((duties) => {
setDuties([...duties])
})
}, [dutyRemoteService])
return dutyRemoteService
.listDuties()
.then((duties) => {
setDuties([...duties])
})
.catch((error) => {
messageApi.error(error.message)
})
}, [dutyRemoteService, messageApi])

useEffect(() => {
loadDutiesFromRemoteToState()
Expand Down
Loading