grafana/public/app/features/search/components/SearchItem.test.tsx
Alex Khomenko 32f99669ff
Search: refactor tests to use react-testing-library (#27352)
* Search: refactor SearchItem.test.tsx

* Search: refactor SearchResults.test.tsx

* Search: mock search_srv

* Search: refactor SearchResultsFilter.test.tsx

* Search: remove redundant mocks

* Search: fix type errors

* Search: move selectors to e2e-selectors
2020-09-08 18:01:38 +03:00

66 lines
1.8 KiB
TypeScript

import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { SearchItem, Props } from './SearchItem';
import { DashboardSearchItemType } from '../types';
beforeEach(() => {
jest.clearAllMocks();
});
const data = {
id: 1,
uid: 'lBdLINUWk',
title: 'Test 1',
uri: 'db/test1',
url: '/d/lBdLINUWk/test1',
slug: '',
type: DashboardSearchItemType.DashDB,
tags: ['Tag1', 'Tag2'],
isStarred: false,
checked: false,
};
const setup = (propOverrides?: Partial<Props>) => {
const props: Props = {
item: data,
onTagSelected: jest.fn(),
editable: false,
};
Object.assign(props, propOverrides);
render(<SearchItem {...props} />);
};
describe('SearchItem', () => {
it('should render the item', () => {
setup();
expect(screen.getAllByLabelText('Dashboard search item Test 1')).toHaveLength(1);
expect(screen.getAllByText('Test 1')).toHaveLength(1);
});
it('should mark item as checked', () => {
const mockedOnToggleChecked = jest.fn();
setup({ editable: true, onToggleChecked: mockedOnToggleChecked });
const checkbox = screen.getByRole('checkbox');
expect(checkbox).not.toBeChecked();
fireEvent.click(checkbox);
expect(mockedOnToggleChecked).toHaveBeenCalledTimes(1);
expect(mockedOnToggleChecked).toHaveBeenCalledWith(data);
expect(screen.getByRole('checkbox')).toBeChecked();
});
it("should render item's tags", () => {
setup();
expect(screen.getAllByText(/tag/i)).toHaveLength(2);
});
it('should select the tag on tag click', () => {
const mockOnTagSelected = jest.fn();
setup({ onTagSelected: mockOnTagSelected });
fireEvent.click(screen.getByText('Tag1'));
expect(mockOnTagSelected).toHaveBeenCalledTimes(1);
expect(mockOnTagSelected).toHaveBeenCalledWith('Tag1');
});
});