2020-04-08 10:14:03 -05:00
|
|
|
import React from 'react';
|
2020-08-20 08:21:42 -05:00
|
|
|
import { render, fireEvent, screen, waitFor, act } from '@testing-library/react';
|
2020-09-04 10:38:27 -05:00
|
|
|
import userEvent from '@testing-library/user-event';
|
2020-04-08 10:14:03 -05:00
|
|
|
import { mockSearch } from './mocks';
|
2020-06-16 03:52:10 -05:00
|
|
|
import { DashboardSearch, Props } from './DashboardSearch';
|
2020-04-08 10:14:03 -05:00
|
|
|
import { searchResults } from '../testData';
|
2020-04-27 07:08:48 -05:00
|
|
|
import { SearchLayout } from '../types';
|
2020-04-08 10:14:03 -05:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
mockSearch.mockClear();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
|
2020-08-20 08:21:42 -05:00
|
|
|
const setup = (testProps?: Partial<Props>) => {
|
2020-04-17 07:29:20 -05:00
|
|
|
const props: any = {
|
|
|
|
onCloseSearch: () => {},
|
2020-06-16 03:52:10 -05:00
|
|
|
...testProps,
|
2020-04-17 07:29:20 -05:00
|
|
|
};
|
2020-08-20 08:21:42 -05:00
|
|
|
render(<DashboardSearch {...props} />);
|
|
|
|
jest.runAllTimers();
|
2020-04-17 07:29:20 -05:00
|
|
|
};
|
|
|
|
|
2020-04-08 10:14:03 -05:00
|
|
|
/**
|
|
|
|
* Need to wrap component render in async act and use jest.runAllTimers to test
|
|
|
|
* calls inside useDebounce hook
|
|
|
|
*/
|
|
|
|
describe('DashboardSearch', () => {
|
|
|
|
it('should call search api with default query when initialised', async () => {
|
2020-08-20 08:21:42 -05:00
|
|
|
setup();
|
|
|
|
|
|
|
|
await waitFor(() => screen.getByPlaceholderText('Search dashboards by name'));
|
2020-04-08 10:14:03 -05:00
|
|
|
|
|
|
|
expect(mockSearch).toHaveBeenCalledTimes(1);
|
|
|
|
expect(mockSearch).toHaveBeenCalledWith({
|
|
|
|
query: '',
|
|
|
|
tag: [],
|
2020-04-17 07:29:20 -05:00
|
|
|
skipRecent: false,
|
|
|
|
skipStarred: false,
|
2020-04-08 10:14:03 -05:00
|
|
|
starred: false,
|
|
|
|
folderIds: [],
|
2020-04-27 07:08:48 -05:00
|
|
|
layout: SearchLayout.Folders,
|
|
|
|
sort: undefined,
|
2020-04-08 10:14:03 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call api with updated query on query change', async () => {
|
2020-08-20 08:21:42 -05:00
|
|
|
setup();
|
2020-04-17 07:29:20 -05:00
|
|
|
|
2020-08-20 08:21:42 -05:00
|
|
|
const input = await screen.findByPlaceholderText('Search dashboards by name');
|
|
|
|
await act((async () => {
|
|
|
|
await fireEvent.input(input, { target: { value: 'Test' } });
|
2020-04-08 10:14:03 -05:00
|
|
|
jest.runAllTimers();
|
2020-08-20 08:21:42 -05:00
|
|
|
}) as any);
|
2020-04-08 10:14:03 -05:00
|
|
|
|
|
|
|
expect(mockSearch).toHaveBeenCalledWith({
|
|
|
|
query: 'Test',
|
2020-04-17 07:29:20 -05:00
|
|
|
skipRecent: false,
|
|
|
|
skipStarred: false,
|
2020-04-08 10:14:03 -05:00
|
|
|
tag: [],
|
|
|
|
starred: false,
|
|
|
|
folderIds: [],
|
2020-04-27 07:08:48 -05:00
|
|
|
layout: SearchLayout.Folders,
|
|
|
|
sort: undefined,
|
2020-04-08 10:14:03 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render 'No results' message when there are no dashboards", async () => {
|
2020-08-20 08:21:42 -05:00
|
|
|
setup();
|
|
|
|
|
|
|
|
const message = await screen.findByText('No dashboards matching your query were found.');
|
|
|
|
expect(message).toBeInTheDocument();
|
2020-04-08 10:14:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should render search results', async () => {
|
2020-08-20 08:21:42 -05:00
|
|
|
mockSearch.mockResolvedValueOnce(searchResults);
|
|
|
|
|
|
|
|
setup();
|
|
|
|
const section = await screen.findAllByLabelText('Search section');
|
|
|
|
expect(section).toHaveLength(2);
|
|
|
|
expect(screen.getAllByLabelText('Search items')).toHaveLength(2);
|
2020-04-08 10:14:03 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should call search with selected tags', async () => {
|
2020-08-20 08:21:42 -05:00
|
|
|
setup();
|
2020-04-08 10:14:03 -05:00
|
|
|
|
2020-08-20 08:21:42 -05:00
|
|
|
await waitFor(() => screen.getByLabelText('Tag filter'));
|
|
|
|
// Get the actual element for the underlying Select component, since Select doesn't accept aria- props
|
|
|
|
const tagComponent = screen.getByLabelText('Tag filter').querySelector('div') as Node;
|
|
|
|
fireEvent.keyDown(tagComponent, { keyCode: 40 });
|
2020-04-17 07:29:20 -05:00
|
|
|
|
2020-09-04 10:38:27 -05:00
|
|
|
const firstTag = await screen.findByText('tag1');
|
|
|
|
userEvent.click(firstTag);
|
2020-06-16 03:52:10 -05:00
|
|
|
|
2020-08-20 08:21:42 -05:00
|
|
|
expect(tagComponent).toBeInTheDocument();
|
2020-06-16 03:52:10 -05:00
|
|
|
|
2020-08-20 08:21:42 -05:00
|
|
|
await waitFor(() =>
|
|
|
|
expect(mockSearch).toHaveBeenCalledWith({
|
|
|
|
query: '',
|
|
|
|
skipRecent: false,
|
|
|
|
skipStarred: false,
|
2020-06-16 03:52:10 -05:00
|
|
|
tag: ['tag1'],
|
2020-08-20 08:21:42 -05:00
|
|
|
starred: false,
|
|
|
|
folderIds: [],
|
|
|
|
layout: SearchLayout.Folders,
|
|
|
|
sort: undefined,
|
2020-06-16 03:52:10 -05:00
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
2020-08-20 08:21:42 -05:00
|
|
|
|
|
|
|
it('should call search api with provided search params', async () => {
|
|
|
|
const params = { query: 'test query', tag: ['tag1'], sort: { value: 'asc' } };
|
|
|
|
setup({ params });
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(mockSearch).toHaveBeenCalledTimes(1);
|
|
|
|
expect(mockSearch).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
query: 'test query',
|
|
|
|
tag: ['tag1'],
|
|
|
|
sort: 'asc',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2020-04-08 10:14:03 -05:00
|
|
|
});
|