-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
đ„
(frontend) improve add member form error handling
- translate known errors, including already existing group error, and directly display the other ones - add component tests - update translations Closes issue #293
- Loading branch information
1 parent
45181b7
commit 840b553
Showing
6 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
...apps/desk/src/features/teams/team-management/components/__tests__/CardCreateTeam.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import fetchMock from 'fetch-mock'; | ||
import React from 'react'; | ||
|
||
import { AppWrapper } from '@/tests/utils'; | ||
|
||
import { CardCreateTeam } from '../CardCreateTeam'; | ||
|
||
const mockPush = jest.fn(); | ||
jest.mock('next/navigation', () => ({ | ||
useRouter: () => ({ | ||
push: mockPush, | ||
}), | ||
})); | ||
|
||
describe('CardCreateTeam', () => { | ||
const renderCardCreateTeam = () => | ||
render(<CardCreateTeam />, { wrapper: AppWrapper }); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterEach(() => { | ||
fetchMock.restore(); | ||
}); | ||
|
||
it('renders all the elements', () => { | ||
renderCardCreateTeam(); | ||
|
||
expect(screen.getByLabelText('Create new team card')).toBeInTheDocument(); | ||
expect(screen.getByText('Create a new group')).toBeInTheDocument(); | ||
expect(screen.getByLabelText('Team name')).toBeInTheDocument(); | ||
expect(screen.getByText('Cancel')).toBeInTheDocument(); | ||
expect(screen.getByText('Create the team')).toBeInTheDocument(); | ||
}); | ||
|
||
it('handles input for team name and enables submit button', async () => { | ||
const user = userEvent.setup(); | ||
renderCardCreateTeam(); | ||
|
||
const teamNameInput = screen.getByLabelText('Team name'); | ||
const createButton = screen.getByText('Create the team'); | ||
|
||
expect(createButton).toBeDisabled(); | ||
|
||
await user.type(teamNameInput, 'New Team'); | ||
expect(createButton).toBeEnabled(); | ||
}); | ||
|
||
it('creates a team successfully and redirects', async () => { | ||
fetchMock.post('end:teams/', { | ||
id: '270328ea-c2c0-4f74-a449-5cdc976dcdb6', | ||
name: 'New Team', | ||
}); | ||
|
||
const user = userEvent.setup(); | ||
renderCardCreateTeam(); | ||
|
||
const teamNameInput = screen.getByLabelText('Team name'); | ||
const createButton = screen.getByText('Create the team'); | ||
|
||
await user.type(teamNameInput, 'New Team'); | ||
await user.click(createButton); | ||
|
||
await waitFor(() => { | ||
expect(mockPush).toHaveBeenCalledWith( | ||
'/teams/270328ea-c2c0-4f74-a449-5cdc976dcdb6', | ||
); | ||
}); | ||
|
||
expect(fetchMock.calls()).toHaveLength(1); | ||
expect(fetchMock.lastCall()?.[0]).toContain('/teams/'); | ||
expect(fetchMock.lastCall()?.[1]?.body).toEqual( | ||
JSON.stringify({ name: 'New Team' }), | ||
); | ||
}); | ||
|
||
it('displays an error message when team name already exists', async () => { | ||
fetchMock.post('end:teams/', { | ||
body: { | ||
cause: ['Team with this Slug already exists.'], | ||
}, | ||
status: 400, | ||
}); | ||
|
||
const user = userEvent.setup(); | ||
renderCardCreateTeam(); | ||
|
||
const teamNameInput = screen.getByLabelText('Team name'); | ||
const createButton = screen.getByText('Create the team'); | ||
|
||
await user.type(teamNameInput, 'Existing Team'); | ||
await user.click(createButton); | ||
|
||
await waitFor(() => { | ||
expect( | ||
screen.getByText(/This name is already used for another group/i), | ||
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it('handles server error gracefully', async () => { | ||
fetchMock.post('end:/teams/', { | ||
body: {}, | ||
status: 500, | ||
}); | ||
|
||
const user = userEvent.setup(); | ||
renderCardCreateTeam(); | ||
|
||
const teamNameInput = screen.getByLabelText('Team name'); | ||
const createButton = screen.getByText('Create the team'); | ||
|
||
await user.type(teamNameInput, 'Server Error Team'); | ||
await user.click(createButton); | ||
|
||
await waitFor(() => { | ||
expect( | ||
screen.getByText( | ||
/Your request cannot be processed because the server is experiencing an error/i, | ||
), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
expect(fetchMock.calls()).toHaveLength(1); | ||
expect(fetchMock.lastCall()?.[0]).toContain('/teams/'); | ||
expect(fetchMock.lastCall()?.[1]?.body).toEqual( | ||
JSON.stringify({ name: 'Server Error Team' }), | ||
); | ||
}); | ||
|
||
it('disables create button when API request is pending', async () => { | ||
// Never resolves | ||
fetchMock.post('end:teams/', new Promise(() => {})); | ||
|
||
const user = userEvent.setup(); | ||
renderCardCreateTeam(); | ||
|
||
const teamNameInput = screen.getByLabelText('Team name'); | ||
const createButton = screen.getByText('Create the team'); | ||
|
||
await user.type(teamNameInput, 'Pending Team'); | ||
await user.click(createButton); | ||
|
||
expect(createButton).toBeDisabled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters