Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko committed Nov 6, 2020
1 parent 9d5595d commit ae9faba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,15 @@ describe('alert_form', () => {

it('renders alert type description', async () => {
await setup();
wrapper.find('[data-test-subj="my-alert-type-SelectOption"]').first().simulate('click');
wrapper.find('button[data-test-subj="my-alert-type-SelectOption"]').first().simulate('click');
const alertDescription = wrapper.find('[data-test-subj="alertDescription"]');
expect(alertDescription.exists()).toBeTruthy();
expect(alertDescription.first().text()).toContain('Alert when testing');
});

it('renders alert type documentation link', async () => {
await setup();
wrapper.find('[data-test-subj="my-alert-type-SelectOption"]').first().simulate('click');
wrapper.find('button[data-test-subj="my-alert-type-SelectOption"]').first().simulate('click');
const alertDocumentationLink = wrapper.find('[data-test-subj="alertDocumentationLink"]');
expect(alertDocumentationLink.exists()).toBeTruthy();
expect(alertDocumentationLink.first().prop('href')).toBe('https://localhost.local/docs');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,7 @@ export const AlertForm = ({
>([]);
const [searchText, setSearchText] = useState<string | undefined>();
const [inputText, setInputText] = useState<string | undefined>();
const [solutions, setSolutions] = useState<
Array<{
id: string;
title: string;
}>
>([]);
const [solutions, setSolutions] = useState<Map<string, string> | undefined>(undefined);
const [solutionsFilter, setSolutionFilter] = useState<string[]>([]);

// load alert types
Expand All @@ -176,28 +171,23 @@ export const AlertForm = ({
const availableAlertTypesResult = getAvailableAlertTypes(alertTypesResult);
setAvailableAlertTypes(availableAlertTypesResult);

const solutionsResult = alertTypesResult.reduce(
(result: Array<{ id: string; title: string }>, alertTypeItem) => {
if (
availableAlertTypesResult.find(
(availableItem) => availableItem.alertTypeModel.id === alertTypeItem.id
) &&
!result.find((solution) => solution.id === alertTypeItem.producer)
) {
result.push({
id: alertTypeItem.producer,
title:
(kibanaFeatures
? getProducerFeatureName(alertTypeItem.producer, kibanaFeatures)
: capitalize(alertTypeItem.producer)) ?? capitalize(alertTypeItem.producer),
});
const solutionsResult = availableAlertTypesResult.reduce(
(result: Map<string, string>, alertTypeItem) => {
if (!result.has(alertTypeItem.alertType.producer)) {
result.set(
alertTypeItem.alertType.producer,
(kibanaFeatures
? getProducerFeatureName(alertTypeItem.alertType.producer, kibanaFeatures)
: capitalize(alertTypeItem.alertType.producer)) ??
capitalize(alertTypeItem.alertType.producer)
);
}
return result;
},
[]
new Map()
);
setSolutions(
solutionsResult.sort((a, b) => a.title.toString().localeCompare(b.title.toString()))
new Map([...solutionsResult.entries()].sort(([, a], [, b]) => a.localeCompare(b)))
);
} catch (e) {
toastNotifications.addDanger({
Expand Down Expand Up @@ -312,7 +302,9 @@ export const AlertForm = ({
);

const alertTypeNodes = Object.entries(alertTypesByProducer)
.sort(([a], [b]) => a.localeCompare(b))
.sort(([a], [b]) =>
solutions ? solutions.get(a)!.localeCompare(solutions.get(b)!) : a.localeCompare(b)
)
.map(([solution, items], groupIndex) => (
<Fragment key={`group${groupIndex}`}>
<EuiTitle
Expand Down Expand Up @@ -698,13 +690,15 @@ export const AlertForm = ({
)}
/>
</EuiFlexItem>
<EuiFlexItem grow={false}>
<SolutionFilter
key="solution-filter"
solutions={solutions}
onChange={(selectedSolutions: string[]) => setSolutionFilter(selectedSolutions)}
/>
</EuiFlexItem>
{solutions ? (
<EuiFlexItem grow={false}>
<SolutionFilter
key="solution-filter"
solutions={solutions}
onChange={(selectedSolutions: string[]) => setSolutionFilter(selectedSolutions)}
/>
</EuiFlexItem>
) : null}
</EuiFlexGroup>
</EuiFormRow>
<EuiSpacer />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ import React, { useEffect, useState } from 'react';
import { FormattedMessage } from '@kbn/i18n/react';
import { EuiFilterGroup, EuiPopover, EuiFilterButton, EuiFilterSelectItem } from '@elastic/eui';

interface Solution {
id: string;
title: string;
}

interface SolutionFilterProps {
solutions: Solution[];
solutions: Map<string, string>;
onChange?: (selectedSolutions: string[]) => void;
}

Expand Down Expand Up @@ -48,27 +43,27 @@ export const SolutionFilter: React.FunctionComponent<SolutionFilterProps> = ({
>
<FormattedMessage
id="xpack.triggersActionsUI.sections.alertForm.solutionFilterLabel"
defaultMessage="Filter by solution:"
defaultMessage="Filter by solution"
/>
</EuiFilterButton>
}
>
<div className="euiFilterSelect__items">
{solutions.map((item) => (
{[...solutions.entries()].map(([id, title]) => (
<EuiFilterSelectItem
key={item.id}
key={id}
onClick={() => {
const isPreviouslyChecked = selectedValues.includes(item.id);
const isPreviouslyChecked = selectedValues.includes(id);
if (isPreviouslyChecked) {
setSelectedValues(selectedValues.filter((val) => val !== item.id));
setSelectedValues(selectedValues.filter((val) => val !== id));
} else {
setSelectedValues([...selectedValues, item.id]);
setSelectedValues([...selectedValues, id]);
}
}}
checked={selectedValues.includes(item.id) ? 'on' : undefined}
data-test-subj={`solution${item.id}FilterOption`}
checked={selectedValues.includes(id) ? 'on' : undefined}
data-test-subj={`solution${id}FilterOption`}
>
{item.title}
{title}
</EuiFilterSelectItem>
))}
</div>
Expand Down

0 comments on commit ae9faba

Please sign in to comment.