From 070b19f79dd3ad3d317fdaf6939d91e641e53c15 Mon Sep 17 00:00:00 2001 From: Maxim Palenov Date: Thu, 14 Nov 2024 11:59:45 +0100 Subject: [PATCH] add persistence query tests --- .../select_rule_type/test_helpers.ts | 29 ++ .../step_define_rule/index.test.tsx | 383 +++++++++++++----- .../components/step_define_rule/index.tsx | 75 ++-- .../step_define_rule/use_persistent_query.ts | 57 ++- .../pages/rule_creation/index.tsx | 1 - .../pages/rule_editing/index.tsx | 1 - 6 files changed, 405 insertions(+), 141 deletions(-) create mode 100644 x-pack/plugins/security_solution/public/detection_engine/rule_creation_ui/components/select_rule_type/test_helpers.ts diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_creation_ui/components/select_rule_type/test_helpers.ts b/x-pack/plugins/security_solution/public/detection_engine/rule_creation_ui/components/select_rule_type/test_helpers.ts new file mode 100644 index 000000000000..a4d5fca3a2ff --- /dev/null +++ b/x-pack/plugins/security_solution/public/detection_engine/rule_creation_ui/components/select_rule_type/test_helpers.ts @@ -0,0 +1,29 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +// eslint-disable-next-line import/no-extraneous-dependencies +import { act, fireEvent, within, screen } from '@testing-library/react'; +import type { Type as RuleType } from '@kbn/securitysolution-io-ts-alerting-types'; + +export async function selectRuleType(ruleType: RuleType): Promise { + const testId = RULE_TYPE_TEST_ID_MAP[ruleType]; + + await act(async () => fireEvent.click(screen.getByTestId(testId))); + + expect(within(screen.getByTestId(testId)).getByRole('switch')).toBeChecked(); +} + +const RULE_TYPE_TEST_ID_MAP = { + query: 'customRuleType', + saved_query: 'customRuleType', + eql: 'eqlRuleType', + machine_learning: 'machineLearningRuleType', + threshold: 'thresholdRuleType', + threat_match: 'threatMatchRuleType', + new_terms: 'newTermsRuleType', + esql: 'esqlRuleType', +}; diff --git a/x-pack/plugins/security_solution/public/detection_engine/rule_creation_ui/components/step_define_rule/index.test.tsx b/x-pack/plugins/security_solution/public/detection_engine/rule_creation_ui/components/step_define_rule/index.test.tsx index 50264fffabfb..b38c7b988419 100644 --- a/x-pack/plugins/security_solution/public/detection_engine/rule_creation_ui/components/step_define_rule/index.test.tsx +++ b/x-pack/plugins/security_solution/public/detection_engine/rule_creation_ui/components/step_define_rule/index.test.tsx @@ -5,6 +5,7 @@ * 2.0. */ +import type { ChangeEvent } from 'react'; import React, { useEffect, useState } from 'react'; import { screen, fireEvent, render, within, act, waitFor } from '@testing-library/react'; import type { Type as RuleType } from '@kbn/securitysolution-io-ts-alerting-types'; @@ -42,13 +43,43 @@ import { addRelatedIntegrationRow, setVersion, } from '../../../rule_creation/components/related_integrations/test_helpers'; +import { useEsqlAvailability } from '../../../../common/hooks/esql/use_esql_availability'; +import { useMLRuleConfig } from '../../../../common/components/ml/hooks/use_ml_rule_config'; +import { selectRuleType } from '../select_rule_type/test_helpers'; + +// Set the extended default timeout for all define rule step form test +jest.setTimeout(10 * 1000); // Mocks integrations jest.mock('../../../fleet_integrations/api'); + +const MOCKED_QUERY_BAR_TEST_ID = 'mockedQueryBar'; +const MOCKED_LANGUAGE_INPUT_TEST_ID = 'languageInput'; + +// Mocking QueryBar to avoid pulling and mocking a ton of dependencies jest.mock('../../../../common/components/query_bar', () => { return { - QueryBar: jest.fn(({ filterQuery }) => { - return
{`${filterQuery.query} ${filterQuery.language}`}
; + QueryBar: jest.fn().mockImplementation(({ filterQuery, onSubmitQuery }) => { + const handleQueryChange = (event: ChangeEvent) => { + onSubmitQuery({ query: event.target.value, language: filterQuery.language }); + }; + + const handleLanguageChange = (event: ChangeEvent) => { + onSubmitQuery({ query: filterQuery.query, language: event.target.value }); + }; + + return ( +
+