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

pull/57099/head^2
Laura Fernández 3 years ago committed by GitHub
parent 3963ed3754
commit 4d0dd0647e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 64
      public/app/core/components/Select/FolderPicker.test.tsx
  2. 10
      public/app/core/components/Select/FolderPicker.tsx

@ -4,6 +4,7 @@ import React from 'react';
import selectEvent from 'react-select-event'; import selectEvent from 'react-select-event';
import { selectors } from '@grafana/e2e-selectors'; import { selectors } from '@grafana/e2e-selectors';
import { contextSrv } from 'app/core/core';
import * as api from 'app/features/manage-dashboards/state/actions'; import * as api from 'app/features/manage-dashboards/state/actions';
import { DashboardSearchHit } from '../../../features/search/types'; import { DashboardSearchHit } from '../../../features/search/types';
@ -74,6 +75,69 @@ describe('FolderPicker', () => {
expect(screen.getByText(newFolder.title)).toBeInTheDocument(); 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', () => { describe('getInitialValues', () => {

@ -68,10 +68,10 @@ export function FolderPicker(props: Props) {
onClear, onClear,
enableReset, enableReset,
initialFolderId, initialFolderId,
initialTitle, initialTitle = '',
permissionLevel, permissionLevel = PermissionLevelString.Edit,
rootName, rootName = 'General',
showRoot, showRoot = true,
skipInitialLoad, skipInitialLoad,
accessControlMetadata, accessControlMetadata,
customAdd, customAdd,
@ -106,7 +106,7 @@ export function FolderPicker(props: Props) {
initialTitle !== '' && initialTitle !== '' &&
!options.find((option) => option.label === 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)) { if (enableCreateNew && Boolean(customAdd)) {
return [...options, { value: VALUE_FOR_ADD, label: ADD_NEW_FOLER_OPTION, title: query }]; return [...options, { value: VALUE_FOR_ADD, label: ADD_NEW_FOLER_OPTION, title: query }];

Loading…
Cancel
Save