Skip to content

Commit

Permalink
Merge pull request #1083 from InseeFr/feat/add-unit-test-concepts
Browse files Browse the repository at this point in the history
feat: add unit test
  • Loading branch information
PierreVasseur authored Nov 25, 2024
2 parents 1849308 + 7fc6be1 commit ef8afd9
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { render, screen, fireEvent } from '@testing-library/react';
import { vi } from 'vitest';
import ExportButtons from './';
import D from '../../../deprecated-locales/build-dictionary';

describe('ExportButtons Component', () => {
it('should render all buttons with the correct labels', () => {
render(<ExportButtons exportHandler={vi.fn()} disabled={false} />);

screen.getByText(D.btnOdsExporter);
screen.getByText(D.btnOdtLg1Exporter);
screen.getByText(D.btnOdtLg2Exporter);
screen.getByText(D.btnCollectionConceptExporter);
});

it('should call exportHandler with correct arguments when buttons are clicked', () => {
const mockExportHandler = vi.fn();

render(
<ExportButtons exportHandler={mockExportHandler} disabled={false} />,
);

fireEvent.click(screen.getByText(D.btnOdsExporter));
expect(mockExportHandler).toHaveBeenCalledWith('ods', false);

fireEvent.click(screen.getByText(D.btnOdtLg1Exporter));
expect(mockExportHandler).toHaveBeenCalledWith('odt', false);

fireEvent.click(screen.getByText(D.btnOdtLg2Exporter));
expect(mockExportHandler).toHaveBeenCalledWith('odt', false, 'lg2');

fireEvent.click(screen.getByText(D.btnCollectionConceptExporter));
expect(mockExportHandler).toHaveBeenCalledWith('odt', true);
});

it('should disable buttons when disabled is true', () => {
render(<ExportButtons exportHandler={vi.fn()} disabled={true} />);

const button = screen.getByText('Export') as HTMLButtonElement;
expect(button.getAttribute('disabled')).toBeDefined();
});

it('should enable buttons when disabled is false', () => {
render(<ExportButtons exportHandler={vi.fn()} disabled={false} />);

const button = screen.getByText('Export') as HTMLButtonElement;
expect(button.getAttribute('disabled')).toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import D from '../../../deprecated-locales/build-dictionary';
import ExportButton from '../dropdown';
const ExportButtons = ({ exportHandler, disabled }) => {

type ExportButtonsTypes = {
disabled?: boolean;
exportHandler: (
type: string,
withConcepts: boolean,
lang?: 'lg1' | 'lg2',
) => void;
};

const ExportButtons = ({
exportHandler,
disabled,
}: Readonly<ExportButtonsTypes>) => {
return (
<ExportButton
disabled={disabled}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useTitle } from '../../utils/hooks/useTitle';

export const Component = () => {
useTitle(D.conceptsTitle, D.exportTitle);
const [ids, setIds] = useState([]);
const [ids, setIds] = useState<string[]>([]);

const { mutate: exportConcept, isPending: isExporting } =
useConceptExporter();
Expand All @@ -26,13 +26,12 @@ export const Component = () => {
title={D.exportTitle}
panelTitle={D.conceptsExportPanelTitle}
labelWarning={D.hasNotConceptToExport}
handleAction={(value) => setIds(value)}
handleAction={(value: string[]) => setIds(value)}
context="concepts"
disabled={ids.length < 1}
disabledWarningMessage={D.hasNotConceptToExport}
ValidationButton={() => (
<ExportButtons
ids={ids}
exportHandler={(type, withConcepts, lang = 'lg1') =>
exportConcept({ ids, type, withConcepts, lang })
}
Expand Down
16 changes: 5 additions & 11 deletions src/packages/modules-operations/msd/pages/sims-creation/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,21 @@ import {
shouldDisplayTitleForPrimaryItem,
} from '../../utils';

import Modal from 'react-modal';
import { useBlocker } from 'react-router-dom';
import { ActionToolbar } from '../../../../components/action-toolbar';
import {
CancelButton,
CloseIconButton,
SaveButton,
} from '../../../../components/buttons/buttons-with-icons';
import { Button } from '../../../../components/buttons/button';
import { CloseIconButton } from '../../../../components/buttons/buttons-with-icons';
import { OperationsApi } from '../../../../sdk/operations-api';
import { sortArrayByLabel } from '../../../../utils/array-utils';
import { useGoBack } from '../../../../utils/hooks/useGoBack';
import { rangeType } from '../../../utils/msd';
import { RubricEssentialMsg } from '../../rubric-essantial-msg';
import { DocumentFormPanel } from './document-form-panel';
import { useDocumentsStoreContext } from './documents-store-context';
import { Menu } from './menu';
import './sims-creation.scss';
import { getDefaultSims, getSiblingSims } from './utils/getSims';
import Modal from 'react-modal';
import { Button } from '../../../../components/buttons/button';

const { RICH_TEXT } = rangeType;

Expand Down Expand Up @@ -254,10 +251,7 @@ const SimsCreation = ({

return (
<>
<ActionToolbar>
<CancelButton action={goBackUrl} />
<SaveButton action={handleSubmit} col={3} />
</ActionToolbar>
<Menu goBackUrl={goBackUrl} handleSubmit={handleSubmit} />

<Modal
className="Modal__Bootstrap modal-dialog operations structures-specification-modal"
Expand Down
18 changes: 18 additions & 0 deletions src/packages/modules-operations/msd/pages/sims-creation/menu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ActionToolbar } from '../../../../components/action-toolbar';
import {
CancelButton,
SaveButton,
} from '../../../../components/buttons/buttons-with-icons';

type MenuTypes = {
goBackUrl: () => void;
handleSubmit: () => void;
};
export const Menu = ({ goBackUrl, handleSubmit }: Readonly<MenuTypes>) => {
return (
<ActionToolbar>
<CancelButton action={goBackUrl} />
<SaveButton action={handleSubmit} />
</ActionToolbar>
);
};

0 comments on commit ef8afd9

Please sign in to comment.