-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1117 from InseeFr/1116-module-operation-gérer-pou…
…r-les-famille feat: display errors when back return 500 for Operations/Families
- Loading branch information
Showing
4 changed files
with
90 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { describe, it, expect } from 'vitest'; | ||
|
||
import { ClientSideError, GlobalClientSideErrorBloc, ErrorBloc } from './index'; | ||
|
||
const mockD = { | ||
errors: { | ||
GlobalClientSideErrorBloc: 'A global client-side error occurred.', | ||
SOME_ERROR_CODE: () => 'Error related to SOME_ERROR_CODE.', | ||
}, | ||
}; | ||
|
||
describe('ClientSideError', () => { | ||
it('renders error message when error is provided', () => { | ||
render( | ||
<ClientSideError error="<strong>Error occurred</strong>" id="error1" />, | ||
); | ||
screen.getByText('Error occurred'); | ||
}); | ||
|
||
it('does not render anything when error is not provided', () => { | ||
render(<ClientSideError id="error1" />); | ||
screen.queryByText('Error occurred'); | ||
}); | ||
}); | ||
|
||
describe('GlobalClientSideErrorBloc', () => { | ||
it('renders global error message when clientSideErrors are provided', () => { | ||
render( | ||
<GlobalClientSideErrorBloc clientSideErrors={['error1']} D={mockD} />, | ||
); | ||
const errorElement = screen.getByRole('alert'); | ||
expect(errorElement).toHaveTextContent( | ||
'A global client-side error occurred.', | ||
); | ||
}); | ||
|
||
it('does not render anything when clientSideErrors is undefined', () => { | ||
render(<GlobalClientSideErrorBloc D={mockD} />); | ||
const errorElement = screen.queryByRole('alert'); | ||
expect(errorElement).toBeNull(); | ||
}); | ||
|
||
it('does not render anything when clientSideErrors is an empty array', () => { | ||
render(<GlobalClientSideErrorBloc clientSideErrors={[]} D={mockD} />); | ||
const errorElement = screen.queryByRole('alert'); | ||
expect(errorElement).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('ErrorBloc', () => { | ||
it('renders formatted errors for an array of error messages', () => { | ||
const errors = [ | ||
JSON.stringify({ code: 'SOME_ERROR_CODE' }), | ||
JSON.stringify({ status: 500 }), | ||
'Plain error message', | ||
]; | ||
render(<ErrorBloc error={errors} D={mockD} />); | ||
|
||
screen.getByText('Error related to SOME_ERROR_CODE.'); | ||
screen.getByText( | ||
'An error has occurred. Please contact the RMéS administration team and provide them with the following message: {"status":500}', | ||
); | ||
screen.getByText('Plain error message'); | ||
}); | ||
|
||
it('renders a single error message when error is a string', () => { | ||
render(<ErrorBloc error="Plain error message" D={mockD} />); | ||
screen.getByText('Plain error message'); | ||
}); | ||
|
||
it('renders fallback message when JSON parsing fails', () => { | ||
const invalidError = 'Invalid JSON'; | ||
render(<ErrorBloc error={[invalidError]} D={mockD} />); | ||
screen.getByText('Invalid JSON'); | ||
}); | ||
}); |
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