Skip to content

Commit

Permalink
Merge pull request #1042 from InseeFr/feat/add-unit-test
Browse files Browse the repository at this point in the history
Feat/add unit test
  • Loading branch information
PierreVasseur authored Oct 16, 2024
2 parents 214f37e + 9116366 commit 7d86316
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
26 changes: 22 additions & 4 deletions src/packages/components/advanced-search/controls.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import { AdvancedSearchControls } from './controls';
import { render } from '@testing-library/react';
import { fireEvent, render, screen } from '@testing-library/react';

describe('concepts-advanced-search-controls', () => {
it('renders without crashing', () => {
const mockOnClickReturn = vi.fn();
const mockInitializeState = vi.fn();

beforeEach(() => {
render(
<AdvancedSearchControls
onClickReturn={vi.fn()}
initializeState={vi.fn()}
onClickReturn={mockOnClickReturn}
initializeState={mockInitializeState}
/>,
);
});

it('should render ReturnButton and ResetButton', () => {
screen.getByRole('button', { name: /back/i });
screen.getByRole('button', { name: /reinitialize/i });
});

it('should call onClickReturn when ReturnButton is clicked', () => {
fireEvent.click(screen.getByRole('button', { name: /back/i }));
expect(mockOnClickReturn).toHaveBeenCalled();
});

it('should call initializeState when ResetButton is clicked', () => {
fireEvent.click(screen.getByRole('button', { name: /reinitialize/i }));
expect(mockInitializeState).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import { renderWithRouter } from '../../tests-utils/render';

describe('menu-concepts', () => {
it('renders without crashing', () => {
renderWithRouter(
<MenuConcepts
location={{ pathname: '/location' }}
permission={{ authType: 'authType', roles: [] }}
/>,
);
renderWithRouter(<MenuConcepts />);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { processGeneral } from './shared';

describe('processGeneral', () => {
it('should process general object correctly', () => {
const input = {
valid: 'T12:00:00.000Z',
additionalMaterial: 'http://example.com/resource',
};
const keys = ['valid', 'additionalMaterial'];

const expectedOutput = {
valid: 'T00:00:00.000Z',
additionalMaterial: 'http://example.com/resource',
};

const result = processGeneral(input, keys);

expect(result).toEqual(expectedOutput);
});

it('should handle missing additionalMaterial', () => {
const input = {
valid: 'T12:00:00.000Z',
additionalMaterial: undefined,
};
const keys = ['valid', 'additionalMaterial'];

const expectedOutput = {
valid: 'T00:00:00.000Z',
additionalMaterial: '',
};

const result = processGeneral(input, keys);

expect(result).toEqual(expectedOutput);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ export function processGeneral(general: any, keys: any[]) {
const extract = takeKeys(keys);
general = extract(general);
general.additionalMaterial = prefixWithHttp(general.additionalMaterial);
general.valid = general.valid.replace(
/T[0-9]{2}:00:00.000Z/,
'T00:00:00.000Z',
);
general.valid = general.valid.replace(/T\d{2}:00:00.000Z/, 'T00:00:00.000Z');
return general;
}

0 comments on commit 7d86316

Please sign in to comment.