Skip to content

Commit

Permalink
adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dplumlee committed Feb 21, 2024
1 parent 4e10d1c commit 2cb907a
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -978,14 +978,13 @@ Then the preview should display the changes for the newly selected rule

#### **Scenario: User can see changes when updated rule is a different rule type**

**Automation**: 1 UI integration test
**Automation**: 1 e2e test

```Gherkin
Given a prebuilt rule is installed in Kibana
And this rule has an update available that changes the rule type
When user opens the upgrade preview
Then the rule type changes should be displayed in grouped field diffs with corresponding query fields
And a tooltip is displayed with information about changing rule types
```

#### **Scenario: Field groupings should be rendered together in the same accordion panel**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const SubFieldComponent = ({
<EuiFlexGroup justifyContent="spaceBetween">
<EuiFlexGroup direction="column">
{shouldShowSubtitles ? (
<EuiTitle size="xxxs">
<EuiTitle data-test-subj="ruleUpgradePerFieldDiffSubtitle" size="xxxs">
<h4>{fieldToDisplayNameMap[fieldName] ?? startCase(camelCase(fieldName))}</h4>
</EuiTitle>
) : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const RuleDiffPanelWrapper = ({ fieldName, children }: RuleDiffPanelWrapp
const { euiTheme } = useEuiTheme();

return (
<EuiSplitPanel.Outer hasBorder>
<EuiSplitPanel.Outer data-test-subj="ruleUpgradePerFieldDiffWrapper" hasBorder>
<EuiAccordion
initialIsOpen={true}
css={css`
Expand All @@ -31,12 +31,14 @@ export const RuleDiffPanelWrapper = ({ fieldName, children }: RuleDiffPanelWrapp
`}
id={fieldName}
buttonContent={
<EuiTitle size="xs">
<EuiTitle data-test-subj="ruleUpgradePerFieldDiffLabel" size="xs">
<h5>{fieldToDisplayNameMap[fieldName] ?? startCase(camelCase(fieldName))}</h5>
</EuiTitle>
}
>
<EuiSplitPanel.Inner color="transparent">{children}</EuiSplitPanel.Inner>
<EuiSplitPanel.Inner data-test-subj="ruleUpgradePerFieldDiffContent" color="transparent">
{children}
</EuiSplitPanel.Inner>
</EuiAccordion>
</EuiSplitPanel.Outer>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ import { FieldGroupDiffComponent } from './field_diff';
interface RuleDiffSectionProps {
title: string;
fieldGroups: FieldsGroupDiff[];
dataTestSubj?: string;
}

export const RuleDiffSection = ({ title, fieldGroups }: RuleDiffSectionProps) => (
export const RuleDiffSection = ({ title, fieldGroups, dataTestSubj }: RuleDiffSectionProps) => (
<>
<EuiSpacer size="m" />
<EuiAccordion
data-test-subj={dataTestSubj}
initialIsOpen={true}
id={title}
css={css`
padding-top: 1px; // Fixes border disappearing bug
`}
buttonContent={
<EuiTitle size="s">
<EuiTitle data-test-subj="ruleUpgradePerFieldDiffSectionHeader" size="s">
<h3>{title}</h3>
</EuiTitle>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* 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.
*/

import {
KqlQueryType,
ThreeWayDiffOutcome,
ThreeWayMergeOutcome,
} from '../../../../../common/api/detection_engine';
import type { PartialRuleDiff } from '../../../../../common/api/detection_engine';
import { TestProviders } from '../../../../common/mock';
import { render } from '@testing-library/react';
import React from 'react';
import { PerFieldRuleDiffTab } from './per_field_rule_diff_tab';

const ruleFieldsDiffBaseFieldsMock = {
diff_outcome: ThreeWayDiffOutcome.StockValueCanUpdate,
has_conflict: false,
has_update: true,
merge_outcome: ThreeWayMergeOutcome.Target,
};

const ruleFieldsDiffMock: PartialRuleDiff = {
fields: {
version: {
...ruleFieldsDiffBaseFieldsMock,
base_version: 1,
current_version: 1,
merged_version: 2,
target_version: 2,
},
},
has_conflict: false,
};

const renderPerFieldRuleDiffTab = (ruleDiff: PartialRuleDiff) => {
return render(
<TestProviders>
<PerFieldRuleDiffTab ruleDiff={ruleDiff} />
</TestProviders>
);
};

describe('PerFieldRuleDiffTab', () => {
test('Field groupings should be rendered together in the same accordion panel', () => {
const mockData: PartialRuleDiff = {
...ruleFieldsDiffMock,
fields: {
kql_query: {
...ruleFieldsDiffBaseFieldsMock,
base_version: {
filters: [],
language: 'lucene',
query: 'old query',
type: KqlQueryType.inline_query,
},
current_version: {
filters: [],
language: 'lucene',
query: 'old query',
type: KqlQueryType.inline_query,
},
merged_version: {
filters: [],
language: 'kuery',
query: 'new query',
type: KqlQueryType.inline_query,
},
target_version: {
filters: [],
language: 'kuery',
query: 'new query',
type: KqlQueryType.inline_query,
},
},
},
};
const wrapper = renderPerFieldRuleDiffTab(mockData);

const matchedSubtitleElements = wrapper.queryAllByTestId('ruleUpgradePerFieldDiffSubtitle');
const subtitles = matchedSubtitleElements.map((element) => {
return element.textContent;
});

// `filters` and `type` have not changed between versions so shouldn't be displayed
expect(subtitles).toEqual(['Query', 'Language']);
});

describe('Undefined values are displayed with empty diffs', () => {
test('when a new field is added', () => {
const mockData: PartialRuleDiff = {
...ruleFieldsDiffMock,
fields: {
timestamp_field: {
...ruleFieldsDiffBaseFieldsMock,
base_version: undefined,
current_version: undefined,
merged_version: 'new timestamp field',
target_version: 'new timestamp field',
},
},
};
const wrapper = renderPerFieldRuleDiffTab(mockData);
const diffContent = wrapper.getByTestId('ruleUpgradePerFieldDiffContent').textContent;

// Only the new timestamp field should be displayed
expect(diffContent).toEqual('+new timestamp field');
});

test('when an old field is removed', () => {
const mockData: PartialRuleDiff = {
...ruleFieldsDiffMock,
fields: {
timestamp_field: {
...ruleFieldsDiffBaseFieldsMock,
base_version: 'old timestamp field',
current_version: 'old timestamp field',
merged_version: undefined,
target_version: undefined,
},
},
};
const wrapper = renderPerFieldRuleDiffTab(mockData);
const diffContent = wrapper.getByTestId('ruleUpgradePerFieldDiffContent').textContent;

// Only the old timestamp_field should be displayed
expect(diffContent).toEqual('-old timestamp field');
});
});

test('Field diff components have the same grouping and order as in rule details overview', () => {
const mockData: PartialRuleDiff = {
...ruleFieldsDiffMock,
fields: {
setup: {
...ruleFieldsDiffBaseFieldsMock,
base_version: 'old setup',
current_version: 'old setup',
merged_version: 'new setup',
target_version: 'new setup',
},
timestamp_field: {
...ruleFieldsDiffBaseFieldsMock,
base_version: undefined,
current_version: undefined,
merged_version: 'new timestamp',
target_version: 'new timestamp',
},
name: {
...ruleFieldsDiffBaseFieldsMock,
base_version: 'old name',
current_version: 'old name',
merged_version: 'new name',
target_version: 'new name',
},
},
};
const wrapper = renderPerFieldRuleDiffTab(mockData);

const matchedSectionElements = wrapper.queryAllByTestId('ruleUpgradePerFieldDiffSectionHeader');
const sectionLabels = matchedSectionElements.map((element) => {
return element.textContent;
});

// Schedule doesn't have any fields in the diff and shouldn't be displayed
expect(sectionLabels).toEqual(['About', 'Definition', 'Setup guide']);

const matchedFieldElements = wrapper.queryAllByTestId('ruleUpgradePerFieldDiffLabel');
const fieldLabels = matchedFieldElements.map((element) => {
return element.textContent;
});

expect(fieldLabels).toEqual(['Name', 'Timestamp Field', 'Setup']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,32 @@ export const PerFieldRuleDiffTab = ({ ruleDiff }: PerFieldRuleDiffTabProps) => {
<>
<RuleDiffHeaderBar />
{aboutFields.length !== 0 && (
<RuleDiffSection title={i18n.ABOUT_SECTION_LABEL} fieldGroups={aboutFields} />
<RuleDiffSection
title={i18n.ABOUT_SECTION_LABEL}
fieldGroups={aboutFields}
dataTestSubj="perFieldDiffAboutSection"
/>
)}
{definitionFields.length !== 0 && (
<RuleDiffSection title={i18n.DEFINITION_SECTION_LABEL} fieldGroups={definitionFields} />
<RuleDiffSection
title={i18n.DEFINITION_SECTION_LABEL}
fieldGroups={definitionFields}
dataTestSubj="perFieldDiffDefinitiontSection"
/>
)}
{scheduleFields.length !== 0 && (
<RuleDiffSection title={i18n.SCHEDULE_SECTION_LABEL} fieldGroups={scheduleFields} />
<RuleDiffSection
title={i18n.SCHEDULE_SECTION_LABEL}
fieldGroups={scheduleFields}
dataTestSubj="perFieldDiffScheduleSection"
/>
)}
{setupFields.length !== 0 && (
<RuleDiffSection title={i18n.SETUP_GUIDE_SECTION_LABEL} fieldGroups={setupFields} />
<RuleDiffSection
title={i18n.SETUP_GUIDE_SECTION_LABEL}
fieldGroups={setupFields}
dataTestSubj="perFieldDiffSetupSection"
/>
)}
</>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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.
*/

import type {
EuiStatelessTourStep,
EuiTourActions,
EuiTourState,
EuiTourStepProps,
} from '@elastic/eui';
import { EuiText, EuiTourStep, useEuiTour } from '@elastic/eui';
import { noop } from 'lodash';
import type { FC } from 'react';
import React, { useEffect, useMemo } from 'react';
import { NEW_FEATURES_TOUR_STORAGE_KEYS } from '../../../../../../common/constants';
import { useKibana } from '../../../../../common/lib/kibana';
import { useIsElementMounted } from '../rules_table/guided_onboarding/use_is_element_mounted';
import * as i18n from './translations';

export interface RulesFeatureTourContextType {
steps: EuiTourStepProps[];
actions: EuiTourActions;
}

export const PER_FIELD_UPGRADE_TOUR_ANCHOR = 'perFieldUpgradeTour';
export const PREBUILT_RULE_UPDATE_FLYOUT_ANCHOR = 'updatePrebuiltRulePreview';

const TOUR_STORAGE_KEY = NEW_FEATURES_TOUR_STORAGE_KEYS.RULE_MANAGEMENT_PAGE;
const TOUR_POPOVER_WIDTH = 400;

const tourConfig: EuiTourState = {
currentTourStep: 1,
isTourActive: true,
tourPopoverWidth: TOUR_POPOVER_WIDTH,
tourSubtitle: '',
};

const stepsConfig: EuiStatelessTourStep[] = [
{
step: 1,
title: i18n.UPDATE_TOUR_TITLE,
content: <EuiText>{i18n.UPDATE_TOUR_DESCRIPTION}</EuiText>,
stepsTotal: 1,
children: <></>,
onFinish: noop,
maxWidth: TOUR_POPOVER_WIDTH,
},
];

export const PrebuiltRulesUpgradeTour: FC = () => {
const { storage } = useKibana().services;

const restoredState = useMemo<EuiTourState>(
() => ({
...tourConfig,
...storage.get(TOUR_STORAGE_KEY),
}),
[storage]
);

const [tourSteps, , tourState] = useEuiTour(stepsConfig, restoredState);

useEffect(() => {
const { isTourActive, currentTourStep } = tourState;
storage.set(TOUR_STORAGE_KEY, { isTourActive, currentTourStep });
}, [tourState, storage]);

const isTourAnchorMounted = useIsElementMounted(PER_FIELD_UPGRADE_TOUR_ANCHOR);
const isFlyoutOpen = useIsElementMounted(PREBUILT_RULE_UPDATE_FLYOUT_ANCHOR);
const shouldShowRuleUpgradeTour = isTourAnchorMounted && !isFlyoutOpen;

const enhancedSteps = useMemo(
() =>
tourSteps.map((item) => ({
...item,
content: item.content,
})),
[tourSteps]
);

return shouldShowRuleUpgradeTour ? (
<EuiTourStep
{...enhancedSteps[0]}
/**
* children={undefined} is needed to narrow down EuiTourStepProps. Without
* it we get a TS error: Types of property 'anchor' are incompatible.
*/
// eslint-disable-next-line react/no-children-prop
children={undefined}
anchor={`#${PER_FIELD_UPGRADE_TOUR_ANCHOR}`}
anchorPosition="downCenter"
/>
) : null;
};
Loading

0 comments on commit 2cb907a

Please sign in to comment.