grafana/public/test/helpers/selectOptionInTest.ts
Sonia Aguilar 55055d0b9b
Alerting: Fix RuleEditor flaky test for existing grafana managed alert (#59780)
* Fix RuleEditor flaky test for existing grafana managed alert

* Fix RuleEditor flaky test for creating a new grafana managed alert

* Fix tests for cloud rules

* Split tests for different rule types

* Move common clickSelectOption method to helpers

* Remove from RuleEditorCloudRules.test.tsx  a repeated cloud rule test

* Move existing grafana rule test case to a new file

* Split RuleEditorCloudRules test: move checking only allowed data sources test case to a new test file

* Re-use common ui const

* Re-use renderRuleEditor method

* Create getDiscoverFeaturesMock to reduce code
2022-12-07 16:15:33 +01:00

24 lines
1.1 KiB
TypeScript

import { Matcher, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { select } from 'react-select-event';
import { byRole } from 'testing-library-selector';
// Used to select an option or options from a Select in unit tests
export const selectOptionInTest = async (
input: HTMLElement,
optionOrOptions: string | RegExp | Array<string | RegExp>
) => await waitFor(() => select(input, optionOrOptions, { container: document.body }));
// Finds the parent of the Select so you can assert if it has a value
export const getSelectParent = (input: HTMLElement) =>
input.parentElement?.parentElement?.parentElement?.parentElement?.parentElement;
export const clickSelectOption = async (selectElement: HTMLElement, optionText: string): Promise<void> => {
await userEvent.click(byRole('combobox').get(selectElement));
await selectOptionInTest(selectElement, optionText);
};
export const clickSelectOptionMatch = async (selectElement: HTMLElement, optionText: Matcher): Promise<void> => {
await userEvent.click(byRole('combobox').get(selectElement));
await selectOptionInTest(selectElement, optionText as string);
};