-
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 #1115 from InseeFr/feat/improve-typescript-codeslist
feat: improve some typescript for codeslist
- Loading branch information
Showing
7 changed files
with
185 additions
and
20 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
143 changes: 143 additions & 0 deletions
143
src/packages/modules-codelists/components/codelist-detail/code-sliding-panel-menu.spec.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,143 @@ | ||
import { CodesList } from '@model/CodesList'; | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { Mock, vi } from 'vitest'; | ||
|
||
import { ADMIN, CODELIST_CONTRIBUTOR } from '../../../auth/roles'; | ||
import { usePermission } from '../../../redux/hooks/usePermission'; | ||
import { CodeSlidingPanelMenu } from './code-sliding-panel-menu'; | ||
|
||
vi.mock('../../../redux/hooks/usePermission'); | ||
|
||
describe('CodeSlidingPanelMenu', () => { | ||
const mockHandleSubmit = vi.fn(); | ||
const mockHandleBack = vi.fn(); | ||
const codelist = { contributor: 'test-contributor' }; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('renders the ReturnButton', () => { | ||
(usePermission as Mock).mockReturnValue({ roles: [], stamp: '' }); | ||
render( | ||
<CodeSlidingPanelMenu | ||
codelist={codelist as unknown as CodesList} | ||
handleSubmit={mockHandleSubmit} | ||
handleBack={mockHandleBack} | ||
creation={false} | ||
/>, | ||
); | ||
|
||
screen.getByRole('button', { name: /back/i }); | ||
}); | ||
|
||
it('renders the UpdateButton when not in creation mode and has permission', () => { | ||
(usePermission as Mock).mockReturnValue({ | ||
roles: [CODELIST_CONTRIBUTOR], | ||
stamp: 'test-contributor', | ||
}); | ||
|
||
render( | ||
<CodeSlidingPanelMenu | ||
codelist={codelist as unknown as CodesList} | ||
handleSubmit={mockHandleSubmit} | ||
handleBack={mockHandleBack} | ||
creation={false} | ||
/>, | ||
); | ||
|
||
screen.getByRole('button', { name: /update/i }); | ||
}); | ||
|
||
it('renders the SaveButton when in creation mode and has permission', () => { | ||
(usePermission as Mock).mockReturnValue({ | ||
roles: [CODELIST_CONTRIBUTOR], | ||
stamp: 'test-contributor', | ||
}); | ||
|
||
render( | ||
<CodeSlidingPanelMenu | ||
codelist={codelist as unknown as CodesList} | ||
handleSubmit={mockHandleSubmit} | ||
handleBack={mockHandleBack} | ||
creation={true} | ||
/>, | ||
); | ||
|
||
screen.getByRole('button', { name: /save/i }); | ||
}); | ||
|
||
it('does not render UpdateButton or SaveButton when user lacks permissions', () => { | ||
(usePermission as Mock).mockReturnValue({ | ||
roles: [], | ||
stamp: 'other-contributor', | ||
}); | ||
|
||
render( | ||
<CodeSlidingPanelMenu | ||
codelist={codelist as unknown as CodesList} | ||
handleSubmit={mockHandleSubmit} | ||
handleBack={mockHandleBack} | ||
creation={false} | ||
/>, | ||
); | ||
|
||
expect(screen.queryByRole('button', { name: /update/i })).toBeNull(); | ||
|
||
render( | ||
<CodeSlidingPanelMenu | ||
codelist={codelist as unknown as CodesList} | ||
handleSubmit={mockHandleSubmit} | ||
handleBack={mockHandleBack} | ||
creation={false} | ||
/>, | ||
); | ||
|
||
expect(screen.queryByRole('button', { name: /save/i })).toBeNull(); | ||
}); | ||
|
||
it('renders the UpdateButton and SaveButton for admin users', () => { | ||
(usePermission as Mock).mockReturnValue({ roles: [ADMIN], stamp: '' }); | ||
|
||
render( | ||
<CodeSlidingPanelMenu | ||
codelist={codelist as unknown as CodesList} | ||
handleSubmit={mockHandleSubmit} | ||
handleBack={mockHandleBack} | ||
creation={false} | ||
/>, | ||
); | ||
|
||
screen.getByRole('button', { name: /update/i }); | ||
|
||
render( | ||
<CodeSlidingPanelMenu | ||
codelist={codelist as unknown as CodesList} | ||
handleSubmit={mockHandleSubmit} | ||
handleBack={mockHandleBack} | ||
creation={true} | ||
/>, | ||
); | ||
|
||
screen.getByRole('button', { name: /save/i }); | ||
}); | ||
|
||
it('triggers the appropriate actions on button clicks', () => { | ||
(usePermission as Mock).mockReturnValue({ roles: [ADMIN], stamp: '' }); | ||
|
||
render( | ||
<CodeSlidingPanelMenu | ||
codelist={codelist as unknown as CodesList} | ||
handleSubmit={mockHandleSubmit} | ||
handleBack={mockHandleBack} | ||
creation={false} | ||
/>, | ||
); | ||
|
||
fireEvent.click(screen.getByRole('button', { name: /back/i })); | ||
expect(mockHandleBack).toHaveBeenCalled(); | ||
|
||
fireEvent.click(screen.getByRole('button', { name: /update/i })); | ||
expect(mockHandleSubmit).toHaveBeenCalled(); | ||
}); | ||
}); |
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
4 changes: 3 additions & 1 deletion
4
src/packages/modules-codelists/components/codelist-detail/codes-panel-add-button.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
File renamed without changes.
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