2021-05-04 06:59:40 -05:00
|
|
|
|
import { within } from '@testing-library/dom';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
import { render, screen, waitFor } from '@testing-library/react';
|
2021-05-04 06:59:40 -05:00
|
|
|
|
import userEvent from '@testing-library/user-event';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
import React from 'react';
|
|
|
|
|
|
2021-05-04 06:59:40 -05:00
|
|
|
|
import { PanelPluginMeta, PluginType } from '@grafana/data';
|
2023-01-29 22:14:12 -06:00
|
|
|
|
import { Panel } from '@grafana/schema';
|
2021-05-04 06:59:40 -05:00
|
|
|
|
|
|
|
|
|
import { backendSrv } from '../../../../core/services/backend_srv';
|
2021-10-25 06:55:06 -05:00
|
|
|
|
import * as panelUtils from '../../../panel/state/util';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
import * as api from '../../state/api';
|
2023-01-29 22:14:12 -06:00
|
|
|
|
import { LibraryElementsSearchResult } from '../../types';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
|
|
|
|
|
import { LibraryPanelsSearch, LibraryPanelsSearchProps } from './LibraryPanelsSearch';
|
2021-05-04 06:59:40 -05:00
|
|
|
|
|
|
|
|
|
jest.mock('@grafana/runtime', () => ({
|
2022-02-02 06:02:32 -06:00
|
|
|
|
...(jest.requireActual('@grafana/runtime') as unknown as object),
|
2021-05-04 06:59:40 -05:00
|
|
|
|
config: {
|
|
|
|
|
panels: {
|
|
|
|
|
timeseries: {
|
|
|
|
|
info: { logos: { small: '' } },
|
|
|
|
|
name: 'Time Series',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
jest.mock('debounce-promise', () => {
|
|
|
|
|
const debounce = (fn: any) => {
|
|
|
|
|
const debounced = () =>
|
|
|
|
|
Promise.resolve([
|
2022-11-17 02:22:57 -06:00
|
|
|
|
{ label: 'General', value: { uid: '', title: 'General' } },
|
|
|
|
|
{ label: 'Folder1', value: { id: 'xMsQdBfWz', title: 'Folder1' } },
|
|
|
|
|
{ label: 'Folder2', value: { id: 'wfTJJL5Wz', title: 'Folder2' } },
|
2021-05-04 06:59:40 -05:00
|
|
|
|
]);
|
|
|
|
|
return debounced;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return debounce;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
async function getTestContext(
|
|
|
|
|
propOverrides: Partial<LibraryPanelsSearchProps> = {},
|
2021-05-11 00:10:19 -05:00
|
|
|
|
searchResult: LibraryElementsSearchResult = { elements: [], perPage: 40, page: 1, totalCount: 0 }
|
2021-05-04 06:59:40 -05:00
|
|
|
|
) {
|
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
const pluginInfo: any = { logos: { small: '', large: '' } };
|
|
|
|
|
const graph: PanelPluginMeta = {
|
|
|
|
|
name: 'Graph',
|
|
|
|
|
id: 'graph',
|
|
|
|
|
info: pluginInfo,
|
|
|
|
|
baseUrl: '',
|
|
|
|
|
type: PluginType.panel,
|
|
|
|
|
module: '',
|
|
|
|
|
sort: 0,
|
|
|
|
|
};
|
|
|
|
|
const timeseries: PanelPluginMeta = {
|
|
|
|
|
name: 'Time Series',
|
|
|
|
|
id: 'timeseries',
|
|
|
|
|
info: pluginInfo,
|
|
|
|
|
baseUrl: '',
|
|
|
|
|
type: PluginType.panel,
|
|
|
|
|
module: '',
|
|
|
|
|
sort: 1,
|
|
|
|
|
};
|
|
|
|
|
const getSpy = jest
|
|
|
|
|
.spyOn(backendSrv, 'get')
|
|
|
|
|
.mockResolvedValue({ sortOptions: [{ displaName: 'Desc', name: 'alpha-desc' }] });
|
|
|
|
|
const getLibraryPanelsSpy = jest.spyOn(api, 'getLibraryPanels').mockResolvedValue(searchResult);
|
2021-10-25 06:55:06 -05:00
|
|
|
|
const getAllPanelPluginMetaSpy = jest.spyOn(panelUtils, 'getAllPanelPluginMeta').mockReturnValue([graph, timeseries]);
|
2021-05-04 06:59:40 -05:00
|
|
|
|
|
|
|
|
|
const props: LibraryPanelsSearchProps = {
|
|
|
|
|
onClick: jest.fn(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Object.assign(props, propOverrides);
|
|
|
|
|
const { rerender } = render(<LibraryPanelsSearch {...props} />);
|
|
|
|
|
|
|
|
|
|
await waitFor(() => expect(getLibraryPanelsSpy).toHaveBeenCalled());
|
|
|
|
|
expect(getLibraryPanelsSpy).toHaveBeenCalledTimes(1);
|
2022-04-21 07:15:21 -05:00
|
|
|
|
jest.clearAllMocks();
|
2021-05-04 06:59:40 -05:00
|
|
|
|
|
|
|
|
|
return { rerender, getLibraryPanelsSpy, getSpy, getAllPanelPluginMetaSpy };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('LibraryPanelsSearch', () => {
|
|
|
|
|
describe('when mounted with default options', () => {
|
|
|
|
|
it('should show input filter and library panels view', async () => {
|
|
|
|
|
await getTestContext();
|
|
|
|
|
|
|
|
|
|
expect(screen.getByPlaceholderText(/search by name/i)).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText(/no library panels found./i)).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('and user searches for library panel by name or description', () => {
|
|
|
|
|
it('should call api with correct params', async () => {
|
|
|
|
|
const { getLibraryPanelsSpy } = await getTestContext();
|
|
|
|
|
|
2022-04-21 07:15:21 -05:00
|
|
|
|
await userEvent.type(screen.getByPlaceholderText(/search by name/i), 'a');
|
2021-05-04 06:59:40 -05:00
|
|
|
|
await waitFor(() => expect(getLibraryPanelsSpy).toHaveBeenCalled());
|
2022-04-21 07:44:36 -05:00
|
|
|
|
await waitFor(() =>
|
|
|
|
|
expect(getLibraryPanelsSpy).toHaveBeenCalledWith({
|
|
|
|
|
searchString: 'a',
|
2022-11-02 10:49:02 -05:00
|
|
|
|
folderFilterUIDs: [],
|
2022-04-21 07:44:36 -05:00
|
|
|
|
page: 0,
|
|
|
|
|
typeFilter: [],
|
|
|
|
|
perPage: 40,
|
|
|
|
|
})
|
|
|
|
|
);
|
2021-05-04 06:59:40 -05:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('when mounted with showSort', () => {
|
|
|
|
|
it('should show input filter and library panels view and sort', async () => {
|
|
|
|
|
await getTestContext({ showSort: true });
|
|
|
|
|
|
|
|
|
|
expect(screen.getByPlaceholderText(/search by name/i)).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText(/no library panels found./i)).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText(/sort \(default a–z\)/i)).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('and user changes sorting', () => {
|
|
|
|
|
it('should call api with correct params', async () => {
|
|
|
|
|
const { getLibraryPanelsSpy } = await getTestContext({ showSort: true });
|
|
|
|
|
|
2022-04-21 07:15:21 -05:00
|
|
|
|
await userEvent.type(screen.getByText(/sort \(default a–z\)/i), 'Desc{enter}');
|
|
|
|
|
await waitFor(() =>
|
|
|
|
|
expect(getLibraryPanelsSpy).toHaveBeenCalledWith({
|
|
|
|
|
searchString: '',
|
|
|
|
|
sortDirection: 'alpha-desc',
|
2022-11-02 10:49:02 -05:00
|
|
|
|
folderFilterUIDs: [],
|
2022-04-21 07:15:21 -05:00
|
|
|
|
page: 0,
|
|
|
|
|
typeFilter: [],
|
|
|
|
|
perPage: 40,
|
|
|
|
|
})
|
|
|
|
|
);
|
2021-05-04 06:59:40 -05:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('when mounted with showPanelFilter', () => {
|
|
|
|
|
it('should show input filter and library panels view and panel filter', async () => {
|
|
|
|
|
await getTestContext({ showPanelFilter: true });
|
|
|
|
|
|
|
|
|
|
expect(screen.getByPlaceholderText(/search by name/i)).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText(/no library panels found./i)).toBeInTheDocument();
|
2021-11-23 06:11:26 -06:00
|
|
|
|
expect(screen.getByRole('combobox', { name: /panel type filter/i })).toBeInTheDocument();
|
2021-05-04 06:59:40 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('and user changes panel filter', () => {
|
|
|
|
|
it('should call api with correct params', async () => {
|
|
|
|
|
const { getLibraryPanelsSpy } = await getTestContext({ showPanelFilter: true });
|
|
|
|
|
|
2022-04-21 07:15:21 -05:00
|
|
|
|
await userEvent.type(screen.getByRole('combobox', { name: /panel type filter/i }), 'Graph{enter}');
|
|
|
|
|
await userEvent.type(screen.getByRole('combobox', { name: /panel type filter/i }), 'Time Series{enter}');
|
|
|
|
|
await waitFor(() =>
|
|
|
|
|
expect(getLibraryPanelsSpy).toHaveBeenCalledWith({
|
|
|
|
|
searchString: '',
|
2022-11-02 10:49:02 -05:00
|
|
|
|
folderFilterUIDs: [],
|
2022-04-21 07:15:21 -05:00
|
|
|
|
page: 0,
|
|
|
|
|
typeFilter: ['graph', 'timeseries'],
|
|
|
|
|
perPage: 40,
|
|
|
|
|
})
|
|
|
|
|
);
|
2021-05-04 06:59:40 -05:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('when mounted with showPanelFilter', () => {
|
|
|
|
|
it('should show input filter and library panels view and folder filter', async () => {
|
|
|
|
|
await getTestContext({ showFolderFilter: true });
|
|
|
|
|
|
|
|
|
|
expect(screen.getByPlaceholderText(/search by name/i)).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText(/no library panels found./i)).toBeInTheDocument();
|
2021-11-23 06:11:26 -06:00
|
|
|
|
expect(screen.getByRole('combobox', { name: /folder filter/i })).toBeInTheDocument();
|
2021-05-04 06:59:40 -05:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('and user changes folder filter', () => {
|
|
|
|
|
it('should call api with correct params', async () => {
|
2022-11-02 10:49:02 -05:00
|
|
|
|
const { getLibraryPanelsSpy } = await getTestContext(
|
|
|
|
|
{ showFolderFilter: true, currentFolderUID: 'wXyZ1234' },
|
|
|
|
|
{
|
|
|
|
|
elements: [
|
|
|
|
|
{
|
|
|
|
|
name: 'Library Panel Name',
|
|
|
|
|
uid: 'uid',
|
|
|
|
|
description: 'Library Panel Description',
|
2022-11-17 02:22:57 -06:00
|
|
|
|
folderUid: '',
|
2023-01-29 22:14:12 -06:00
|
|
|
|
model: { type: 'timeseries', title: 'A title' } as Panel,
|
2022-11-02 10:49:02 -05:00
|
|
|
|
type: 'timeseries',
|
|
|
|
|
version: 1,
|
|
|
|
|
meta: {
|
|
|
|
|
folderName: 'General',
|
|
|
|
|
folderUid: '',
|
|
|
|
|
connectedDashboards: 0,
|
|
|
|
|
created: '2021-01-01 12:00:00',
|
|
|
|
|
createdBy: { id: 1, name: 'Admin', avatarUrl: '' },
|
|
|
|
|
updated: '2021-01-01 12:00:00',
|
|
|
|
|
updatedBy: { id: 1, name: 'Admin', avatarUrl: '' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
perPage: 40,
|
|
|
|
|
page: 1,
|
|
|
|
|
totalCount: 0,
|
|
|
|
|
}
|
|
|
|
|
);
|
2021-05-04 06:59:40 -05:00
|
|
|
|
|
2022-04-21 07:15:21 -05:00
|
|
|
|
await userEvent.click(screen.getByRole('combobox', { name: /folder filter/i }));
|
2022-11-02 10:49:02 -05:00
|
|
|
|
await userEvent.type(screen.getByRole('combobox', { name: /folder filter/i }), 'library', {
|
2021-05-04 06:59:40 -05:00
|
|
|
|
skipClick: true,
|
|
|
|
|
});
|
2022-11-02 10:49:02 -05:00
|
|
|
|
|
|
|
|
|
await waitFor(() => {
|
2022-04-21 07:15:21 -05:00
|
|
|
|
expect(getLibraryPanelsSpy).toHaveBeenCalledWith({
|
|
|
|
|
searchString: '',
|
2022-11-02 10:49:02 -05:00
|
|
|
|
folderFilterUIDs: ['wXyZ1234'],
|
2022-04-21 07:15:21 -05:00
|
|
|
|
page: 0,
|
|
|
|
|
typeFilter: [],
|
|
|
|
|
perPage: 40,
|
2022-11-02 10:49:02 -05:00
|
|
|
|
});
|
|
|
|
|
});
|
2021-05-04 06:59:40 -05:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('when mounted without showSecondaryActions and there is one panel', () => {
|
|
|
|
|
it('should show correct row and no delete button', async () => {
|
|
|
|
|
await getTestContext(
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
page: 1,
|
|
|
|
|
totalCount: 1,
|
|
|
|
|
perPage: 40,
|
2021-05-11 00:10:19 -05:00
|
|
|
|
elements: [
|
2021-05-04 06:59:40 -05:00
|
|
|
|
{
|
|
|
|
|
name: 'Library Panel Name',
|
|
|
|
|
uid: 'uid',
|
|
|
|
|
description: 'Library Panel Description',
|
2022-11-17 02:22:57 -06:00
|
|
|
|
folderUid: '',
|
2023-01-29 22:14:12 -06:00
|
|
|
|
model: { type: 'timeseries', title: 'A title' } as Panel,
|
2021-05-04 06:59:40 -05:00
|
|
|
|
type: 'timeseries',
|
|
|
|
|
version: 1,
|
|
|
|
|
meta: {
|
2021-05-05 04:09:12 -05:00
|
|
|
|
folderName: 'General',
|
|
|
|
|
folderUid: '',
|
2021-05-04 06:59:40 -05:00
|
|
|
|
connectedDashboards: 0,
|
|
|
|
|
created: '2021-01-01 12:00:00',
|
|
|
|
|
createdBy: { id: 1, name: 'Admin', avatarUrl: '' },
|
|
|
|
|
updated: '2021-01-01 12:00:00',
|
|
|
|
|
updatedBy: { id: 1, name: 'Admin', avatarUrl: '' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const card = () => screen.getByLabelText(/plugin visualization item time series/i);
|
|
|
|
|
|
|
|
|
|
expect(screen.queryByText(/no library panels found./i)).not.toBeInTheDocument();
|
|
|
|
|
expect(card()).toBeInTheDocument();
|
|
|
|
|
expect(within(card()).getByText(/library panel name/i)).toBeInTheDocument();
|
|
|
|
|
expect(within(card()).getByText(/library panel description/i)).toBeInTheDocument();
|
|
|
|
|
expect(within(card()).queryByLabelText(/delete button on panel type card/i)).not.toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('when mounted with showSecondaryActions and there is one panel', () => {
|
|
|
|
|
it('should show correct row and delete button', async () => {
|
|
|
|
|
await getTestContext(
|
|
|
|
|
{ showSecondaryActions: true },
|
|
|
|
|
{
|
|
|
|
|
page: 1,
|
|
|
|
|
totalCount: 1,
|
|
|
|
|
perPage: 40,
|
2021-05-11 00:10:19 -05:00
|
|
|
|
elements: [
|
2021-05-04 06:59:40 -05:00
|
|
|
|
{
|
|
|
|
|
name: 'Library Panel Name',
|
|
|
|
|
uid: 'uid',
|
|
|
|
|
description: 'Library Panel Description',
|
2022-11-17 02:22:57 -06:00
|
|
|
|
folderUid: '',
|
2023-01-29 22:14:12 -06:00
|
|
|
|
model: { type: 'timeseries', title: 'A title' } as Panel,
|
2021-05-04 06:59:40 -05:00
|
|
|
|
type: 'timeseries',
|
|
|
|
|
version: 1,
|
|
|
|
|
meta: {
|
2021-05-05 04:09:12 -05:00
|
|
|
|
folderName: 'General',
|
|
|
|
|
folderUid: '',
|
2021-05-04 06:59:40 -05:00
|
|
|
|
connectedDashboards: 0,
|
|
|
|
|
created: '2021-01-01 12:00:00',
|
|
|
|
|
createdBy: { id: 1, name: 'Admin', avatarUrl: '' },
|
|
|
|
|
updated: '2021-01-01 12:00:00',
|
|
|
|
|
updatedBy: { id: 1, name: 'Admin', avatarUrl: '' },
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const card = () => screen.getByLabelText(/plugin visualization item time series/i);
|
|
|
|
|
|
|
|
|
|
expect(screen.queryByText(/no library panels found./i)).not.toBeInTheDocument();
|
|
|
|
|
expect(card()).toBeInTheDocument();
|
|
|
|
|
expect(within(card()).getByText(/library panel name/i)).toBeInTheDocument();
|
|
|
|
|
expect(within(card()).getByText(/library panel description/i)).toBeInTheDocument();
|
|
|
|
|
expect(within(card()).getByLabelText(/delete button on panel type card/i)).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|