interactions.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { fireEvent, screen, within } from '@testing-library/react';
  2. import userEvent from '@testing-library/user-event';
  3. import { selectors } from '@grafana/e2e-selectors';
  4. import { ExploreId } from '../../../../types';
  5. import { withinExplore } from './setup';
  6. export const changeDatasource = async (name: string) => {
  7. const datasourcePicker = (await screen.findByLabelText(selectors.components.DataSourcePicker.container)).children[0];
  8. fireEvent.keyDown(datasourcePicker, { keyCode: 40 });
  9. const option = screen.getByText(name);
  10. fireEvent.click(option);
  11. };
  12. export const inputQuery = async (query: string, exploreId: ExploreId = ExploreId.left) => {
  13. const input = withinExplore(exploreId).getByRole('textbox', { name: 'query' });
  14. await userEvent.clear(input);
  15. await userEvent.type(input, query);
  16. };
  17. export const runQuery = async (exploreId: ExploreId = ExploreId.left) => {
  18. const explore = withinExplore(exploreId);
  19. const toolbar = within(explore.getByLabelText('Explore toolbar'));
  20. const button = toolbar.getByRole('button', { name: /run query/i });
  21. await userEvent.click(button);
  22. };
  23. export const openQueryHistory = async (exploreId: ExploreId = ExploreId.left) => {
  24. const selector = withinExplore(exploreId);
  25. const button = selector.getByRole('button', { name: 'Rich history button' });
  26. await userEvent.click(button);
  27. expect(await selector.findByPlaceholderText('Search queries')).toBeInTheDocument();
  28. };
  29. export const closeQueryHistory = async (exploreId: ExploreId = ExploreId.left) => {
  30. const closeButton = withinExplore(exploreId).getByRole('button', { name: 'Close query history' });
  31. await userEvent.click(closeButton);
  32. };
  33. export const switchToQueryHistoryTab = async (
  34. name: 'Settings' | 'Query History',
  35. exploreId: ExploreId = ExploreId.left
  36. ) => {
  37. await userEvent.click(withinExplore(exploreId).getByRole('tab', { name: `Tab ${name}` }));
  38. };
  39. export const selectStarredTabFirst = async (exploreId: ExploreId = ExploreId.left) => {
  40. const checkbox = withinExplore(exploreId).getByRole('checkbox', {
  41. name: 'Change the default active tab from “Query history” to “Starred”',
  42. });
  43. await userEvent.click(checkbox);
  44. };
  45. export const selectOnlyActiveDataSource = async (exploreId: ExploreId = ExploreId.left) => {
  46. const checkbox = withinExplore(exploreId).getByLabelText(/Only show queries for data source currently active.*/);
  47. await userEvent.click(checkbox);
  48. };
  49. export const starQueryHistory = (queryIndex: number, exploreId: ExploreId = ExploreId.left) => {
  50. invokeAction(queryIndex, 'Star query', exploreId);
  51. };
  52. export const commentQueryHistory = async (
  53. queryIndex: number,
  54. comment: string,
  55. exploreId: ExploreId = ExploreId.left
  56. ) => {
  57. await invokeAction(queryIndex, 'Add comment', exploreId);
  58. const input = withinExplore(exploreId).getByPlaceholderText('An optional description of what the query does.');
  59. await userEvent.clear(input);
  60. await userEvent.type(input, comment);
  61. await invokeAction(queryIndex, 'Submit button', exploreId);
  62. };
  63. export const deleteQueryHistory = (queryIndex: number, exploreId: ExploreId = ExploreId.left) => {
  64. invokeAction(queryIndex, 'Delete query', exploreId);
  65. };
  66. export const loadMoreQueryHistory = async (exploreId: ExploreId = ExploreId.left) => {
  67. const button = withinExplore(exploreId).getByRole('button', { name: 'Load more' });
  68. await userEvent.click(button);
  69. };
  70. const invokeAction = async (queryIndex: number, actionAccessibleName: string, exploreId: ExploreId) => {
  71. const selector = withinExplore(exploreId);
  72. const buttons = selector.getAllByRole('button', { name: actionAccessibleName });
  73. await userEvent.click(buttons[queryIndex]);
  74. };