From c8ab280ed185f6fd68884cd1e5b75ca84a114aae Mon Sep 17 00:00:00 2001 From: Sonatai Date: Fri, 26 Jan 2024 12:55:07 +0100 Subject: [PATCH] #203 add test for hool --- src/Hooks/index.ts | 2 +- .../useGenerateSearchEntries/mockSummery.json | 87 +++++++++++++++++++ .../useGenerateSearchEntries.test.ts | 66 ++++++++++++++ .../useGenerateSearchEntries.ts | 17 ++-- .../Search/SearchInput/SearchInput.tsx | 2 +- src/mocks/handlers.ts | 12 +-- src/mocks/mockedSummery.json | 87 +++++++++++++++++++ 7 files changed, 257 insertions(+), 16 deletions(-) create mode 100644 src/Hooks/useGenerateSearchEntries/mockSummery.json create mode 100644 src/Hooks/useGenerateSearchEntries/useGenerateSearchEntries.test.ts rename src/Hooks/{ => useGenerateSearchEntries}/useGenerateSearchEntries.ts (63%) create mode 100644 src/mocks/mockedSummery.json diff --git a/src/Hooks/index.ts b/src/Hooks/index.ts index 0f9d235..b82e62e 100644 --- a/src/Hooks/index.ts +++ b/src/Hooks/index.ts @@ -1,6 +1,6 @@ export * from './useActiveAnchor'; export * from './useActiveLink'; -export * from './useGenerateSearchEntries'; +export * from './useGenerateSearchEntries/useGenerateSearchEntries'; export * from './useGetSpec'; export * from './useGetSummary'; export * from './useMarkdown'; diff --git a/src/Hooks/useGenerateSearchEntries/mockSummery.json b/src/Hooks/useGenerateSearchEntries/mockSummery.json new file mode 100644 index 0000000..893391a --- /dev/null +++ b/src/Hooks/useGenerateSearchEntries/mockSummery.json @@ -0,0 +1,87 @@ +{ + "project": "Neureka", + "version": "0.20.1", + "created": "Thu Jun 29 18:08:38 CEST 2023", + "statistics": { + "runs": "99", + "passed": "99", + "failed": "0", + "featureFailures": "0", + "successRate": "1.0", + "duration": "?" + }, + "specifications": [ + { + "className": "Example_Spec.Example_Spec", + "title": "An Introduction to writing Spock Specifications", + "narrative": "Hello and welcome to the example / template specification of this project.\n This is a simple introduction as to how to get started writing Spock specifications.\n\n Spock works on top of Groovy which is in essence a syntactic super-set of Java.\n That means that one can write Java code in Groovy, and 99% of the time it will \n work the exact same way.", + "featureCount": "5", + "failures": "0", + "errors": "0", + "skipped": "0", + "successRate": "1.0", + "duration": "?", + "executedFeatures": [ + { "id": "Call me feature not unit test!", "extraInfo": [] }, + { + "id": "I am readable and also best practice!", + "extraInfo": [] + }, + { + "id": "Numbers to the power of two with a fancy data table!", + "extraInfo": [] + } + ], + "ignoredFeatures": [] + }, + { + "className": "it.Calculus_Stress_Test", + "title": "", + "narrative": "", + "featureCount": "5", + "failures": "0", + "errors": "0", + "skipped": "0", + "successRate": "1.0", + "duration": "?", + "executedFeatures": [ + { + "id": "Activation functions work across types, on large prime sized 1D slices and non sliced 1D tensors.", + "extraInfo": [] + }, + { + "id": "Activation functions work across types.", + "extraInfo": [] + }, + { + "id": "Dot operation stress test runs error free and produces expected result", + "extraInfo": [] + } + ], + "ignoredFeatures": [] + }, + { + "className": "it.Cross_Device_Sliced_Tensor_System_Test", + "title": "Cross Device Tensor Slicing", + "narrative": "", + "featureCount": "2", + "failures": "0", + "errors": "0", + "skipped": "0", + "successRate": "1.0", + "duration": "?", + "executedFeatures": [ + { + "id": "Cross device sliced tensor integration test runs without errors.", + "extraInfo": [] + }, + { + "id": "Slices can be created using the SliceBuilder.", + "extraInfo": [] + } + ], + "ignoredFeatures": [] + } + ], + "generator": "https://github.com/renatoathaydes/spock-reports" +} diff --git a/src/Hooks/useGenerateSearchEntries/useGenerateSearchEntries.test.ts b/src/Hooks/useGenerateSearchEntries/useGenerateSearchEntries.test.ts new file mode 100644 index 0000000..82f3249 --- /dev/null +++ b/src/Hooks/useGenerateSearchEntries/useGenerateSearchEntries.test.ts @@ -0,0 +1,66 @@ +import { expect, test } from 'vitest'; + +import { renderHook } from '@testing-library/react-hooks'; +import { HookProvider } from '../../test-utils'; +import mockedSummery from './mockSummery.json'; +import { useGenerateSearchEntries } from './useGenerateSearchEntries'; +import { IMinimizedSummaryEntry } from 'spock-react/components/search-types'; + +const minimizedSummaries: IMinimizedSummaryEntry[] = [ + { + className: 'Example_Spec.Example_Spec', + title: 'An Introduction to writing Spock Specifications', + narrative: + 'Hello and welcome to the example / template specification of this project.\n This is a simple introduction as to how to get started writing Spock specifications.\n\n Spock works on top of Groovy which is in essence a syntactic super-set of Java.\n That means that one can write Java code in Groovy, and 99% of the time it will \n work the exact same way.', + features: [ + { id: 'Call me feature not unit test!' }, + { + id: 'I am readable and also best practice!', + }, + { + id: 'Numbers to the power of two with a fancy data table!', + }, + ], + }, + { + className: 'it.Calculus_Stress_Test', + title: '', + narrative: '', + features: [ + { + id: 'Activation functions work across types, on large prime sized 1D slices and non sliced 1D tensors.', + }, + { + id: 'Activation functions work across types.', + }, + { + id: 'Dot operation stress test runs error free and produces expected result', + }, + ], + }, + { + className: 'it.Cross_Device_Sliced_Tensor_System_Test', + title: 'Cross Device Tensor Slicing', + narrative: '', + features: [ + { + id: 'Cross device sliced tensor integration test runs without errors.', + }, + { + id: 'Slices can be created using the SliceBuilder.', + }, + ], + }, +]; + +test('hook example', () => { + const { result } = renderHook( + () => useGenerateSearchEntries({ summary: mockedSummery }), + { + wrapper: HookProvider, + } + ); + + expect(result.current?.length).toBe(3); + expect(result.current).toStrictEqual(minimizedSummaries); +}); diff --git a/src/Hooks/useGenerateSearchEntries.ts b/src/Hooks/useGenerateSearchEntries/useGenerateSearchEntries.ts similarity index 63% rename from src/Hooks/useGenerateSearchEntries.ts rename to src/Hooks/useGenerateSearchEntries/useGenerateSearchEntries.ts index 95c38f5..577e8a5 100644 --- a/src/Hooks/useGenerateSearchEntries.ts +++ b/src/Hooks/useGenerateSearchEntries/useGenerateSearchEntries.ts @@ -14,12 +14,17 @@ export const useGenerateSearchEntries = ( useEffect(() => { const minimizedSummary = summary?.specifications.map( - (entry: ISpecification): IMinimizedSummaryEntry => ({ - className: entry.className, - features: entry.executedFeatures, - narrative: entry.narrative, - title: entry.title, - }) + (entry: ISpecification): IMinimizedSummaryEntry => { + const features = entry.executedFeatures.map((feature) => ({ + id: feature.id, + })); + return { + className: entry.className, + features, + narrative: entry.narrative, + title: entry.title, + }; + } ); minimizedSummary !== undefined && setSearchEntries(minimizedSummary); diff --git a/src/components/Search/SearchInput/SearchInput.tsx b/src/components/Search/SearchInput/SearchInput.tsx index deb452f..8357aaf 100644 --- a/src/components/Search/SearchInput/SearchInput.tsx +++ b/src/components/Search/SearchInput/SearchInput.tsx @@ -6,7 +6,7 @@ import { ISearchInput } from 'spock-react/components/search-input-types'; import { faMagnifyingGlass } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; -import { useGenerateSearchEntries } from '../../../Hooks/useGenerateSearchEntries'; +import { useGenerateSearchEntries } from '../../../Hooks/useGenerateSearchEntries/useGenerateSearchEntries'; import { getSearchScoreV2TS } from '../getSearchScore'; export const SearchInput = (props: ISearchInput): JSX.Element => { diff --git a/src/mocks/handlers.ts b/src/mocks/handlers.ts index 0bc7faa..427b60b 100644 --- a/src/mocks/handlers.ts +++ b/src/mocks/handlers.ts @@ -1,14 +1,10 @@ import { http, HttpResponse } from 'msw'; +import mockedSummery from './mockedSummery.json'; export const handlers = [ http.get('*summary.json', () => { - return new HttpResponse( - JSON.stringify({ - name: '', - }), - { - status: 200, - } - ); + return new HttpResponse(JSON.stringify(mockedSummery), { + status: 200, + }); }), ]; diff --git a/src/mocks/mockedSummery.json b/src/mocks/mockedSummery.json new file mode 100644 index 0000000..893391a --- /dev/null +++ b/src/mocks/mockedSummery.json @@ -0,0 +1,87 @@ +{ + "project": "Neureka", + "version": "0.20.1", + "created": "Thu Jun 29 18:08:38 CEST 2023", + "statistics": { + "runs": "99", + "passed": "99", + "failed": "0", + "featureFailures": "0", + "successRate": "1.0", + "duration": "?" + }, + "specifications": [ + { + "className": "Example_Spec.Example_Spec", + "title": "An Introduction to writing Spock Specifications", + "narrative": "Hello and welcome to the example / template specification of this project.\n This is a simple introduction as to how to get started writing Spock specifications.\n\n Spock works on top of Groovy which is in essence a syntactic super-set of Java.\n That means that one can write Java code in Groovy, and 99% of the time it will \n work the exact same way.", + "featureCount": "5", + "failures": "0", + "errors": "0", + "skipped": "0", + "successRate": "1.0", + "duration": "?", + "executedFeatures": [ + { "id": "Call me feature not unit test!", "extraInfo": [] }, + { + "id": "I am readable and also best practice!", + "extraInfo": [] + }, + { + "id": "Numbers to the power of two with a fancy data table!", + "extraInfo": [] + } + ], + "ignoredFeatures": [] + }, + { + "className": "it.Calculus_Stress_Test", + "title": "", + "narrative": "", + "featureCount": "5", + "failures": "0", + "errors": "0", + "skipped": "0", + "successRate": "1.0", + "duration": "?", + "executedFeatures": [ + { + "id": "Activation functions work across types, on large prime sized 1D slices and non sliced 1D tensors.", + "extraInfo": [] + }, + { + "id": "Activation functions work across types.", + "extraInfo": [] + }, + { + "id": "Dot operation stress test runs error free and produces expected result", + "extraInfo": [] + } + ], + "ignoredFeatures": [] + }, + { + "className": "it.Cross_Device_Sliced_Tensor_System_Test", + "title": "Cross Device Tensor Slicing", + "narrative": "", + "featureCount": "2", + "failures": "0", + "errors": "0", + "skipped": "0", + "successRate": "1.0", + "duration": "?", + "executedFeatures": [ + { + "id": "Cross device sliced tensor integration test runs without errors.", + "extraInfo": [] + }, + { + "id": "Slices can be created using the SliceBuilder.", + "extraInfo": [] + } + ], + "ignoredFeatures": [] + } + ], + "generator": "https://github.com/renatoathaydes/spock-reports" +}