From 08cd29ffa765d66becc525360cb6620e0ec2d782 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Wed, 16 Oct 2024 10:09:17 +0100 Subject: [PATCH 1/3] fix: solve sonar issue and add unit test --- .../shared.spec.ts | 37 +++++++++++++++++++ .../build-payload-creation-update/shared.ts | 5 +-- 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/packages/modules-concepts/utils/build-payload-creation-update/shared.spec.ts diff --git a/src/packages/modules-concepts/utils/build-payload-creation-update/shared.spec.ts b/src/packages/modules-concepts/utils/build-payload-creation-update/shared.spec.ts new file mode 100644 index 000000000..6803e8508 --- /dev/null +++ b/src/packages/modules-concepts/utils/build-payload-creation-update/shared.spec.ts @@ -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); + }); +}); diff --git a/src/packages/modules-concepts/utils/build-payload-creation-update/shared.ts b/src/packages/modules-concepts/utils/build-payload-creation-update/shared.ts index df227db84..22120290c 100644 --- a/src/packages/modules-concepts/utils/build-payload-creation-update/shared.ts +++ b/src/packages/modules-concepts/utils/build-payload-creation-update/shared.ts @@ -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; } From b77f4c16d9935a1a04bd2f57ab02bafec58df1e0 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Wed, 16 Oct 2024 10:09:34 +0100 Subject: [PATCH 2/3] feat: add unit test for the advanced search control component --- .../advanced-search/controls.spec.tsx | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/packages/components/advanced-search/controls.spec.tsx b/src/packages/components/advanced-search/controls.spec.tsx index d73898413..79c31fcdd 100644 --- a/src/packages/components/advanced-search/controls.spec.tsx +++ b/src/packages/components/advanced-search/controls.spec.tsx @@ -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( , ); }); + + 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(); + }); }); From 9116366de696dc6be1c59089449c5cde7f49b7e0 Mon Sep 17 00:00:00 2001 From: Emmanuel Date: Wed, 16 Oct 2024 10:14:42 +0100 Subject: [PATCH 3/3] feat: migrate a file to TS --- .../menu/{concepts.spec.jsx => concepts.spec.tsx} | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) rename src/packages/modules-concepts/menu/{concepts.spec.jsx => concepts.spec.tsx} (55%) diff --git a/src/packages/modules-concepts/menu/concepts.spec.jsx b/src/packages/modules-concepts/menu/concepts.spec.tsx similarity index 55% rename from src/packages/modules-concepts/menu/concepts.spec.jsx rename to src/packages/modules-concepts/menu/concepts.spec.tsx index 6f6bfa606..40669662f 100644 --- a/src/packages/modules-concepts/menu/concepts.spec.jsx +++ b/src/packages/modules-concepts/menu/concepts.spec.tsx @@ -3,11 +3,6 @@ import { renderWithRouter } from '../../tests-utils/render'; describe('menu-concepts', () => { it('renders without crashing', () => { - renderWithRouter( - , - ); + renderWithRouter(); }); });