SearchResults.test.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { fireEvent, render, screen } from '@testing-library/react';
  2. import React from 'react';
  3. import { selectors } from '@grafana/e2e-selectors';
  4. import { generalFolder, searchResults } from '../testData';
  5. import { SearchLayout } from '../types';
  6. import { Props, SearchResults } from './SearchResults';
  7. beforeEach(() => {
  8. jest.clearAllMocks();
  9. });
  10. const setup = (propOverrides?: Partial<Props>) => {
  11. const props: Props = {
  12. results: searchResults,
  13. onTagSelected: (name: string) => {},
  14. onToggleSection: () => {},
  15. editable: false,
  16. layout: SearchLayout.Folders,
  17. };
  18. Object.assign(props, propOverrides);
  19. render(<SearchResults {...props} />);
  20. };
  21. describe('SearchResults', () => {
  22. it('should render result items', () => {
  23. setup();
  24. expect(screen.getAllByTestId(selectors.components.Search.sectionV2)).toHaveLength(2);
  25. });
  26. it('should render section items for expanded section', () => {
  27. setup();
  28. expect(screen.getAllByText('General', { exact: false })[0]).toBeInTheDocument();
  29. expect(screen.getByTestId(selectors.components.Search.itemsV2)).toBeInTheDocument();
  30. expect(screen.getByTestId(selectors.components.Search.dashboardItem('Test 1'))).toBeInTheDocument();
  31. expect(screen.getByTestId(selectors.components.Search.dashboardItem('Test 2'))).toBeInTheDocument();
  32. // Check search cards aren't in the DOM
  33. expect(screen.queryByTestId(selectors.components.Search.cards)).not.toBeInTheDocument();
  34. expect(screen.queryByTestId(selectors.components.Search.dashboardCard('Test 1'))).not.toBeInTheDocument();
  35. expect(screen.queryByTestId(selectors.components.Search.dashboardCard('Test 2'))).not.toBeInTheDocument();
  36. });
  37. it('should render search card items for expanded section when showPreviews is enabled', () => {
  38. setup({ showPreviews: true });
  39. expect(screen.getAllByText('General', { exact: false })[0]).toBeInTheDocument();
  40. expect(screen.getByTestId(selectors.components.Search.cards)).toBeInTheDocument();
  41. expect(screen.getByTestId(selectors.components.Search.dashboardCard('Test 1'))).toBeInTheDocument();
  42. expect(screen.getByTestId(selectors.components.Search.dashboardCard('Test 2'))).toBeInTheDocument();
  43. // Check search items aren't in the DOM
  44. expect(screen.queryByTestId(selectors.components.Search.itemsV2)).not.toBeInTheDocument();
  45. expect(screen.queryByTestId(selectors.components.Search.dashboardItem('Test 1'))).not.toBeInTheDocument();
  46. expect(screen.queryByTestId(selectors.components.Search.dashboardItem('Test 2'))).not.toBeInTheDocument();
  47. });
  48. it('should not render checkboxes for non-editable results', () => {
  49. setup({ editable: false });
  50. expect(screen.queryAllByRole('checkbox')).toHaveLength(0);
  51. });
  52. it('should render checkboxes for editable results', () => {
  53. setup({ editable: true });
  54. expect(screen.getAllByRole('checkbox')).toHaveLength(4);
  55. });
  56. it('should collapse folder item list on header click', () => {
  57. const mockOnToggleSection = jest.fn();
  58. setup({ onToggleSection: mockOnToggleSection });
  59. fireEvent.click(screen.getAllByText('General', { exact: false })[0]);
  60. expect(mockOnToggleSection).toHaveBeenCalledWith(generalFolder);
  61. });
  62. it('should not throw an error if the search results have an empty title', () => {
  63. const mockOnToggleSection = jest.fn();
  64. const searchResultsEmptyTitle = searchResults.slice();
  65. searchResultsEmptyTitle[0].title = '';
  66. expect(() => {
  67. setup({ results: searchResultsEmptyTitle, onToggleSection: mockOnToggleSection });
  68. }).not.toThrowError();
  69. });
  70. });