2022-05-30 17:36:16 +02:00
|
|
|
import { render, screen } from '@testing-library/react';
|
2022-04-22 14:33:13 +01:00
|
|
|
import React from 'react';
|
2022-06-02 17:06:07 +02:00
|
|
|
import selectEvent from 'react-select-event';
|
2020-02-13 10:13:03 +00:00
|
|
|
|
2022-05-30 17:36:16 +02:00
|
|
|
import { selectors } from '@grafana/e2e-selectors';
|
2021-08-20 11:53:48 +02:00
|
|
|
import * as api from 'app/features/manage-dashboards/state/actions';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
2021-08-20 11:53:48 +02:00
|
|
|
import { DashboardSearchHit } from '../../../features/search/types';
|
2020-02-13 10:13:03 +00:00
|
|
|
|
2022-04-22 14:33:13 +01:00
|
|
|
import { FolderPicker, getInitialValues } from './FolderPicker';
|
|
|
|
|
|
2020-02-13 10:13:03 +00:00
|
|
|
describe('FolderPicker', () => {
|
2022-05-30 17:36:16 +02:00
|
|
|
it('should render', async () => {
|
2021-08-20 11:53:48 +02:00
|
|
|
jest
|
|
|
|
|
.spyOn(api, 'searchFolders')
|
|
|
|
|
.mockResolvedValue([
|
|
|
|
|
{ title: 'Dash 1', id: 1 } as DashboardSearchHit,
|
|
|
|
|
{ title: 'Dash 2', id: 2 } as DashboardSearchHit,
|
|
|
|
|
]);
|
2022-05-30 17:36:16 +02:00
|
|
|
|
|
|
|
|
render(<FolderPicker onChange={jest.fn()} />);
|
|
|
|
|
expect(await screen.findByTestId(selectors.components.FolderPicker.containerV2)).toBeInTheDocument();
|
2020-02-13 10:13:03 +00:00
|
|
|
});
|
2022-06-02 17:06:07 +02:00
|
|
|
|
|
|
|
|
it('Should apply filter to the folders search results', async () => {
|
|
|
|
|
jest
|
|
|
|
|
.spyOn(api, 'searchFolders')
|
|
|
|
|
.mockResolvedValue([
|
|
|
|
|
{ title: 'Dash 1', id: 1 } as DashboardSearchHit,
|
|
|
|
|
{ title: 'Dash 2', id: 2 } as DashboardSearchHit,
|
|
|
|
|
{ title: 'Dash 3', id: 3 } as DashboardSearchHit,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
render(<FolderPicker onChange={jest.fn()} filter={(hits) => hits.filter((h) => h.id !== 2)} />);
|
|
|
|
|
|
|
|
|
|
const pickerContainer = screen.getByLabelText(selectors.components.FolderPicker.input);
|
|
|
|
|
selectEvent.openMenu(pickerContainer);
|
|
|
|
|
|
|
|
|
|
const pickerOptions = await screen.findAllByLabelText('Select option');
|
|
|
|
|
|
|
|
|
|
expect(pickerOptions).toHaveLength(2);
|
|
|
|
|
expect(pickerOptions[0]).toHaveTextContent('Dash 1');
|
|
|
|
|
expect(pickerOptions[1]).toHaveTextContent('Dash 3');
|
|
|
|
|
});
|
2020-02-13 10:13:03 +00:00
|
|
|
});
|
2021-08-20 11:53:48 +02:00
|
|
|
|
|
|
|
|
describe('getInitialValues', () => {
|
|
|
|
|
describe('when called with folderId and title', () => {
|
|
|
|
|
it('then it should return folderId and title', async () => {
|
|
|
|
|
const getFolder = jest.fn().mockResolvedValue({});
|
|
|
|
|
const folder = await getInitialValues({ folderId: 0, folderName: 'Some title', getFolder });
|
|
|
|
|
|
|
|
|
|
expect(folder).toEqual({ label: 'Some title', value: 0 });
|
|
|
|
|
expect(getFolder).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('when called with just a folderId', () => {
|
|
|
|
|
it('then it should call api to retrieve title', async () => {
|
|
|
|
|
const getFolder = jest.fn().mockResolvedValue({ id: 0, title: 'Title from api' });
|
|
|
|
|
const folder = await getInitialValues({ folderId: 0, getFolder });
|
|
|
|
|
|
|
|
|
|
expect(folder).toEqual({ label: 'Title from api', value: 0 });
|
|
|
|
|
expect(getFolder).toHaveBeenCalledTimes(1);
|
|
|
|
|
expect(getFolder).toHaveBeenCalledWith(0);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('when called without folderId', () => {
|
|
|
|
|
it('then it should throw an error', async () => {
|
|
|
|
|
const getFolder = jest.fn().mockResolvedValue({});
|
|
|
|
|
await expect(getInitialValues({ getFolder })).rejects.toThrow();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|