Browse: Fix General folder not showing in FolderPicker (#57156) (#57158)

(cherry picked from commit 4d0dd0647e)

Co-authored-by: Laura Fernández <laura.fernandez@grafana.com>
This commit is contained in:
Grot (@grafanabot)
2022-10-18 06:52:33 -04:00
committed by GitHub
co-authored by Laura Fernández
parent cbdc5e59cf
commit ef74b7a486
2 changed files with 69 additions and 5 deletions
@@ -4,6 +4,7 @@ import React from 'react';
import selectEvent from 'react-select-event';
import { selectors } from '@grafana/e2e-selectors';
import { contextSrv } from 'app/core/core';
import * as api from 'app/features/manage-dashboards/state/actions';
import { DashboardSearchHit } from '../../../features/search/types';
@@ -74,6 +75,69 @@ describe('FolderPicker', () => {
expect(screen.getByText(newFolder.title)).toBeInTheDocument();
});
});
it('should show the General folder by default for editors', async () => {
jest
.spyOn(api, 'searchFolders')
.mockResolvedValue([
{ title: 'Dash 1', id: 1 } as DashboardSearchHit,
{ title: 'Dash 2', id: 2 } as DashboardSearchHit,
]);
jest.spyOn(contextSrv, 'hasAccess').mockReturnValue(true);
const onChangeFn = jest.fn();
render(<FolderPicker onChange={onChangeFn} />);
expect(await screen.findByTestId(selectors.components.FolderPicker.containerV2)).toBeInTheDocument();
const pickerContainer = screen.getByLabelText(selectors.components.FolderPicker.input);
selectEvent.openMenu(pickerContainer);
const pickerOptions = await screen.findAllByLabelText('Select option');
expect(pickerOptions[0]).toHaveTextContent('General');
});
it('should not show the General folder by default if showRoot is false', async () => {
jest
.spyOn(api, 'searchFolders')
.mockResolvedValue([
{ title: 'Dash 1', id: 1 } as DashboardSearchHit,
{ title: 'Dash 2', id: 2 } as DashboardSearchHit,
]);
jest.spyOn(contextSrv, 'hasAccess').mockReturnValue(true);
const onChangeFn = jest.fn();
render(<FolderPicker onChange={onChangeFn} showRoot={false} />);
expect(await screen.findByTestId(selectors.components.FolderPicker.containerV2)).toBeInTheDocument();
const pickerContainer = screen.getByLabelText(selectors.components.FolderPicker.input);
selectEvent.openMenu(pickerContainer);
const pickerOptions = await screen.findAllByLabelText('Select option');
expect(pickerOptions[0]).not.toHaveTextContent('General');
});
it('should not show the General folder by default for not editors', async () => {
jest
.spyOn(api, 'searchFolders')
.mockResolvedValue([
{ title: 'Dash 1', id: 1 } as DashboardSearchHit,
{ title: 'Dash 2', id: 2 } as DashboardSearchHit,
]);
jest.spyOn(contextSrv, 'hasAccess').mockReturnValue(false);
const onChangeFn = jest.fn();
render(<FolderPicker onChange={onChangeFn} />);
expect(await screen.findByTestId(selectors.components.FolderPicker.containerV2)).toBeInTheDocument();
const pickerContainer = screen.getByLabelText(selectors.components.FolderPicker.input);
selectEvent.openMenu(pickerContainer);
const pickerOptions = await screen.findAllByLabelText('Select option');
expect(pickerOptions[0]).not.toHaveTextContent('General');
});
});
describe('getInitialValues', () => {
@@ -68,10 +68,10 @@ export function FolderPicker(props: Props) {
onClear,
enableReset,
initialFolderId,
initialTitle,
permissionLevel,
rootName,
showRoot,
initialTitle = '',
permissionLevel = PermissionLevelString.Edit,
rootName = 'General',
showRoot = true,
skipInitialLoad,
accessControlMetadata,
customAdd,
@@ -106,7 +106,7 @@ export function FolderPicker(props: Props) {
initialTitle !== '' &&
!options.find((option) => option.label === initialTitle)
) {
Boolean(initialTitle) && options.unshift({ label: initialTitle, value: initialFolderId });
options.unshift({ label: initialTitle, value: initialFolderId });
}
if (enableCreateNew && Boolean(customAdd)) {
return [...options, { value: VALUE_FOR_ADD, label: ADD_NEW_FOLER_OPTION, title: query }];