From 1455219cbfb5fdb0763235b7ace19a6ca6145790 Mon Sep 17 00:00:00 2001 From: Amit Galitzky Date: Wed, 27 Apr 2022 20:47:10 +0000 Subject: [PATCH 01/10] added unit tests for validation API related components Signed-off-by: Amit Galitzky --- .../__tests__/ConfigureModel.test.tsx | 2 +- .../__tests__/DefineDetector.test.tsx | 71 +- .../DefineDetector.test.tsx.snap | 1037 +++++++++++++++- .../pages/DefineDetector/utils/constants.tsx | 12 + .../__tests__/ListActions.test.tsx | 10 +- .../ConfirmDeleteDetectorsModal.test.tsx | 26 +- .../ConfirmStartDetectorsModal.test.tsx | 4 +- .../ConfirmStopDetectorsModal.test.tsx | 4 +- .../containers/List/__tests__/List.test.tsx | 54 +- .../SampleDataBox/SampleDataBox.tsx | 10 +- .../AnomalyDetectionOverview.test.tsx | 4 +- .../ModelConfigurationFields.test.tsx | 86 ++ .../ModelConfigurationFields.test.tsx.snap | 555 +++++++++ .../__tests__/ReviewAndCreate.test.tsx | 186 ++- .../ReviewAndCreate.test.tsx.snap | 1084 ++++++++++++++++- test/setup.jest.ts | 4 + 16 files changed, 3074 insertions(+), 75 deletions(-) create mode 100644 public/pages/ReviewAndCreate/components/__tests__/ModelConfigurationFields.test.tsx create mode 100644 public/pages/ReviewAndCreate/components/__tests__/__snapshots__/ModelConfigurationFields.test.tsx.snap diff --git a/public/pages/ConfigureModel/containers/__tests__/ConfigureModel.test.tsx b/public/pages/ConfigureModel/containers/__tests__/ConfigureModel.test.tsx index 9dbb0c03..77a9e370 100644 --- a/public/pages/ConfigureModel/containers/__tests__/ConfigureModel.test.tsx +++ b/public/pages/ConfigureModel/containers/__tests__/ConfigureModel.test.tsx @@ -64,7 +64,7 @@ describe(' spec', () => { test('validate all required fields', async () => { const { getByText } = renderWithRouter(); fireEvent.click(getByText('Next')); - await waitFor(()=>{}); + await waitFor(() => {}); getByText('You must enter a feature name'); getByText('You must select a field'); }); diff --git a/public/pages/DefineDetector/containers/__tests__/DefineDetector.test.tsx b/public/pages/DefineDetector/containers/__tests__/DefineDetector.test.tsx index cb6e5e59..84f9a212 100644 --- a/public/pages/DefineDetector/containers/__tests__/DefineDetector.test.tsx +++ b/public/pages/DefineDetector/containers/__tests__/DefineDetector.test.tsx @@ -22,9 +22,12 @@ import { DefineDetector } from '../DefineDetector'; import configureStore from '../../../../redux/configureStore'; import { httpClientMock, coreServicesMock } from '../../../../../test/mocks'; import { CoreServicesContext } from '../../../../components/CoreServices/CoreServices'; -import { INITIAL_DETECTOR_DEFINITION_VALUES } from '../../utils/constants'; +import { + INITIAL_DETECTOR_DEFINITION_VALUES, + testDetectorDefinitionValues, +} from '../../utils/constants'; -const renderWithRouter = (isEdit: boolean = false) => ({ +const renderWithRouterEmpty = (isEdit: boolean = false) => ({ ...render( @@ -46,7 +49,61 @@ const renderWithRouter = (isEdit: boolean = false) => ({ ), }); -describe(' spec', () => { +const renderWithRouterFull = (isEdit: boolean = false) => ({ + ...render( + + + + ( + + + + )} + /> + + + + ), +}); + +describe(' Full', () => { + beforeEach(() => { + jest.clearAllMocks(); + console.error = jest.fn(); + console.warn = jest.fn(); + }); + describe('creating detector definition', () => { + test('renders the component', () => { + const { container, getByText } = renderWithRouterFull(false); + getByText('Define detector'); + expect(container.firstChild).toMatchSnapshot(); + }); + + test('duplicate name', async () => { + httpClientMock.get = jest.fn().mockResolvedValue({ + ok: true, + response: { + count: 0, + match: true, + }, + }); + + const { getByText } = renderWithRouterFull(); + fireEvent.click(getByText('Next')); + + await waitFor(() => {}); + getByText('Duplicate detector name'); + getByText('Must specify an index'); + }); + }); +}); + +describe(' empty', () => { beforeEach(() => { jest.clearAllMocks(); console.error = jest.fn(); @@ -54,7 +111,7 @@ describe(' spec', () => { }); describe('creating detector definition', () => { test('renders the component', () => { - const { container, getByText } = renderWithRouter(false); + const { container, getByText } = renderWithRouterEmpty(false); expect(container.firstChild).toMatchSnapshot(); getByText('Define detector'); }); @@ -69,9 +126,9 @@ describe(' spec', () => { ], }, }); - const { getByText } = renderWithRouter(); + const { getByText } = renderWithRouterEmpty(); fireEvent.click(getByText('Next')); - await waitFor(()=>{}); + await waitFor(() => {}); getByText('Detector name cannot be empty'); getByText('Must specify an index'); getByText('Required'); @@ -79,7 +136,7 @@ describe(' spec', () => { }); describe('editing detector definition', () => { test('renders the component', () => { - const { container, getByText } = renderWithRouter(true); + const { container, getByText } = renderWithRouterEmpty(true); expect(container.firstChild).toMatchSnapshot(); getByText('Edit detector settings'); getByText('Save changes'); diff --git a/public/pages/DefineDetector/containers/__tests__/__snapshots__/DefineDetector.test.tsx.snap b/public/pages/DefineDetector/containers/__tests__/__snapshots__/DefineDetector.test.tsx.snap index 8e3dfe27..839c07ad 100644 --- a/public/pages/DefineDetector/containers/__tests__/__snapshots__/DefineDetector.test.tsx.snap +++ b/public/pages/DefineDetector/containers/__tests__/__snapshots__/DefineDetector.test.tsx.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[` spec creating detector definition renders the component 1`] = ` +exports[` Full creating detector definition renders the component 1`] = `
spec creating detector definition renders the compon name="name" placeholder="Enter detector name" type="text" - value="" + value="test" />
@@ -172,7 +172,9 @@ exports[` spec creating detector definition renders the compon name="description" placeholder="Describe the detector" rows="3" - /> + > + desc + @@ -518,11 +520,11 @@ exports[` spec creating detector definition renders the compon data-test-subj="comboBoxInput" tabindex="-1" > -

- Find timestamp -

+ timestamp +
spec creating detector definition renders the compon
`; -exports[` spec editing detector definition renders the component 1`] = ` +exports[` empty creating detector definition renders the component 1`] = ` +
+
+
+
+

+ Define detector + +

+
+
+
+
+
+

+ Detector details +

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+ +
+
+
+ Detector name must contain 1-64 characters. Valid characters are + a-z, A-Z, 0-9, -(hyphen), _(underscore) and .(period). +
+
+
+
+
+ +
+
+