Skip to content

Commit

Permalink
Merge pull request #1115 from InseeFr/feat/improve-typescript-codeslist
Browse files Browse the repository at this point in the history
feat: improve some typescript for codeslist
  • Loading branch information
PierreVasseur authored Jan 10, 2025
2 parents 7a36132 + 89c9140 commit b3a7309
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/packages/model/CodesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface CodesList {
id?: string;
codes: Code[];
notation: string;
lastCodeUriSegment: string;
contributor: string;
}

export type CodesLists = {
Expand Down
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();
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CodesList } from '@model/CodesList';

import { ActionToolbar } from '@components/action-toolbar';
import {
ReturnButton,
Expand All @@ -8,12 +10,18 @@ import {
import { ADMIN, CODELIST_CONTRIBUTOR } from '../../../auth/roles';
import { usePermission } from '../../../redux/hooks/usePermission';

interface CodeSlidingPanelMenuTypes {
codelist: CodesList;
handleSubmit: () => void;
handleBack: () => void;
creation: boolean;
}
export const CodeSlidingPanelMenu = ({
codelist,
handleSubmit,
handleBack,
creation,
}) => {
}: Readonly<CodeSlidingPanelMenuTypes>) => {
const permission = usePermission();

const hasRightsBasedOnStamp =
Expand All @@ -23,11 +31,11 @@ export const CodeSlidingPanelMenu = ({

return (
<ActionToolbar>
<ReturnButton action={handleBack} col={6} />
<ReturnButton action={handleBack} />
{(isAdmin || hasRightsBasedOnStamp) && (
<>
{!creation && <UpdateButton action={handleSubmit} col={6} />}
{creation && <SaveButton action={handleSubmit} col={6} />}
{!creation && <UpdateButton action={handleSubmit} />}
{creation && <SaveButton action={handleSubmit} />}
</>
)}
</ActionToolbar>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CodesList } from '@model/CodesList';
import { render, screen, fireEvent } from '@testing-library/react';
import { Mock, vi } from 'vitest';

Expand All @@ -23,7 +24,10 @@ describe('CodesPanelAddButton', () => {
});

render(
<CodesPanelAddButton codelist={{}} onHandlePanel={mockOnHandlePanel} />,
<CodesPanelAddButton
codelist={{} as unknown as CodesList}
onHandlePanel={mockOnHandlePanel}
/>,
);

expect(screen.queryByRole('button', { name: /add/i })).toBeNull();
Expand All @@ -37,7 +41,7 @@ describe('CodesPanelAddButton', () => {

render(
<CodesPanelAddButton
codelist={{ lastCodeUriSegment: 'segment' }}
codelist={{ lastCodeUriSegment: 'segment' } as unknown as CodesList}
onHandlePanel={mockOnHandlePanel}
/>,
);
Expand All @@ -53,10 +57,12 @@ describe('CodesPanelAddButton', () => {

render(
<CodesPanelAddButton
codelist={{
lastCodeUriSegment: 'segment',
contributor: 'test-contributor',
}}
codelist={
{
lastCodeUriSegment: 'segment',
contributor: 'test-contributor',
} as unknown as CodesList
}
onHandlePanel={mockOnHandlePanel}
/>,
);
Expand All @@ -72,10 +78,12 @@ describe('CodesPanelAddButton', () => {

render(
<CodesPanelAddButton
codelist={{
lastCodeUriSegment: 'segment',
contributor: 'test-contributor',
}}
codelist={
{
lastCodeUriSegment: 'segment',
contributor: 'test-contributor',
} as unknown as CodesList
}
onHandlePanel={mockOnHandlePanel}
/>,
);
Expand All @@ -91,10 +99,12 @@ describe('CodesPanelAddButton', () => {

render(
<CodesPanelAddButton
codelist={{
lastCodeUriSegment: 'segment',
contributor: 'test-contributor',
}}
codelist={
{
lastCodeUriSegment: 'segment',
contributor: 'test-contributor',
} as unknown as CodesList
}
onHandlePanel={mockOnHandlePanel}
/>,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { CodesList } from '@model/CodesList';

import { AddButton } from '@components/buttons/add';

import { ADMIN, CODELIST_CONTRIBUTOR } from '../../../auth/roles';
import { usePermission } from '../../../redux/hooks/usePermission';

interface CodesPanelAddButtonTypes {
codelist: any;
codelist: CodesList;
onHandlePanel: () => void;
}
export const CodesPanelAddButton = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { validateCode } from '../../utils';
import { CollapsiblePanel } from '../collapsible-panel';
import { CodeSlidingPanelMenu } from './code-sliding-panel-menu';
import { CodesPanelAddButton } from './codes-panel-add-button';
import './codes-panel.scss';
import './codes-panel.css';
import { Table } from './codes/table';

const CodeSlidingPanel = ({
Expand Down

0 comments on commit b3a7309

Please sign in to comment.