assert.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { waitFor } from '@testing-library/react';
  2. import { ExploreId } from '../../../../types';
  3. import { withinExplore } from './setup';
  4. export const assertQueryHistoryExists = async (query: string, exploreId: ExploreId = ExploreId.left) => {
  5. const selector = withinExplore(exploreId);
  6. expect(await selector.findByText('1 queries')).toBeInTheDocument();
  7. const queryItem = selector.getByLabelText('Query text');
  8. expect(queryItem).toHaveTextContent(query);
  9. };
  10. export const assertQueryHistory = async (expectedQueryTexts: string[], exploreId: ExploreId = ExploreId.left) => {
  11. const selector = withinExplore(exploreId);
  12. await waitFor(() => {
  13. expect(selector.getByText(new RegExp(`${expectedQueryTexts.length} queries`))).toBeInTheDocument();
  14. const queryTexts = selector.getAllByLabelText('Query text');
  15. expectedQueryTexts.forEach((expectedQueryText, queryIndex) => {
  16. expect(queryTexts[queryIndex]).toHaveTextContent(expectedQueryText);
  17. });
  18. });
  19. };
  20. export const assertQueryHistoryComment = async (
  21. expectedQueryComments: string[],
  22. exploreId: ExploreId = ExploreId.left
  23. ) => {
  24. const selector = withinExplore(exploreId);
  25. await waitFor(() => {
  26. expect(selector.getByText(new RegExp(`${expectedQueryComments.length} queries`))).toBeInTheDocument();
  27. const queryComments = selector.getAllByLabelText('Query comment');
  28. expectedQueryComments.forEach((expectedQueryText, queryIndex) => {
  29. expect(queryComments[queryIndex]).toHaveTextContent(expectedQueryText);
  30. });
  31. });
  32. };
  33. export const assertQueryHistoryIsStarred = async (expectedStars: boolean[], exploreId: ExploreId = ExploreId.left) => {
  34. const selector = withinExplore(exploreId);
  35. const starButtons = selector.getAllByRole('button', { name: /Star query|Unstar query/ });
  36. await waitFor(() =>
  37. expectedStars.forEach((starred, queryIndex) => {
  38. expect(starButtons[queryIndex]).toHaveAccessibleName(starred ? 'Unstar query' : 'Star query');
  39. })
  40. );
  41. };
  42. export const assertQueryHistoryTabIsSelected = (
  43. tabName: 'Query history' | 'Starred' | 'Settings',
  44. exploreId: ExploreId = ExploreId.left
  45. ) => {
  46. expect(withinExplore(exploreId).getByRole('tab', { name: `Tab ${tabName}`, selected: true })).toBeInTheDocument();
  47. };
  48. export const assertDataSourceFilterVisibility = (visible: boolean, exploreId: ExploreId = ExploreId.left) => {
  49. const filterInput = withinExplore(exploreId).queryByLabelText('Filter queries for data sources(s)');
  50. if (visible) {
  51. expect(filterInput).toBeInTheDocument();
  52. } else {
  53. expect(filterInput).not.toBeInTheDocument();
  54. }
  55. };
  56. export const assertQueryHistoryElementsShown = (
  57. shown: number,
  58. total: number,
  59. exploreId: ExploreId = ExploreId.left
  60. ) => {
  61. expect(withinExplore(exploreId).queryByText(`Showing ${shown} of ${total}`)).toBeInTheDocument();
  62. };
  63. export const assertLoadMoreQueryHistoryNotVisible = (exploreId: ExploreId = ExploreId.left) => {
  64. expect(withinExplore(exploreId).queryByRole('button', { name: 'Load more' })).not.toBeInTheDocument();
  65. };