renamed DialogTitle to DialogHeader + tests

This commit is contained in:
ronronscelestes 2022-05-30 11:19:02 +02:00
parent 1a9400564f
commit 78da4c84d6
3 changed files with 142 additions and 10 deletions

View File

@ -10,7 +10,7 @@ import getTrad from '../../utils/getTrad';
import { findRecursiveFolderMetadatas } from '../../utils'; import { findRecursiveFolderMetadatas } from '../../utils';
import { useFolderStructure } from '../../hooks/useFolderStructure'; import { useFolderStructure } from '../../hooks/useFolderStructure';
export const DialogTitle = ({ currentFolder, onChangeFolder }) => { export const DialogHeader = ({ currentFolder, onChangeFolder }) => {
const { formatMessage } = useIntl(); const { formatMessage } = useIntl();
const { data, isLoading } = useFolderStructure(); const { data, isLoading } = useFolderStructure();
@ -26,11 +26,15 @@ export const DialogTitle = ({ currentFolder, onChangeFolder }) => {
<ModalHeader> <ModalHeader>
<Stack horizontal spacing={4}> <Stack horizontal spacing={4}>
{currentFolder && ( {currentFolder && (
<button type="button" onClick={() => onChangeFolder(folderMetadatas?.parentId)}> <button
aria-label="Go back"
type="button"
onClick={() => onChangeFolder(folderMetadatas?.parentId)}
>
<Icon color="neutral500" as={ArrowLeft} /> <Icon color="neutral500" as={ArrowLeft} />
</button> </button>
)} )}
<Breadcrumbs label="Category model, name field"> <Breadcrumbs label={`Add new assets ${folderLabel ? `, ${folderLabel}` : ''}`}>
<Crumb> <Crumb>
{formatMessage({ {formatMessage({
id: getTrad('header.actions.add-assets'), id: getTrad('header.actions.add-assets'),
@ -44,12 +48,12 @@ export const DialogTitle = ({ currentFolder, onChangeFolder }) => {
); );
}; };
DialogTitle.defaultProps = { DialogHeader.defaultProps = {
currentFolder: undefined, currentFolder: undefined,
onChangeFolder: undefined, onChangeFolder: undefined,
}; };
DialogTitle.propTypes = { DialogHeader.propTypes = {
currentFolder: PropTypes.number, currentFolder: PropTypes.number,
onChangeFolder: PropTypes.func, onChangeFolder: PropTypes.func,
}; };

View File

@ -20,7 +20,7 @@ import { useFolders } from '../../hooks/useFolders';
import useModalQueryParams from '../../hooks/useModalQueryParams'; import useModalQueryParams from '../../hooks/useModalQueryParams';
import { AssetDefinition } from '../../constants'; import { AssetDefinition } from '../../constants';
import getAllowedFiles from '../../utils/getAllowedFiles'; import getAllowedFiles from '../../utils/getAllowedFiles';
import { DialogTitle } from './DialogTitle'; import { DialogHeader } from './DialogHeader';
import { DialogFooter } from './DialogFooter'; import { DialogFooter } from './DialogFooter';
import { EditAssetDialog } from '../EditAssetDialog'; import { EditAssetDialog } from '../EditAssetDialog';
import { moveElement } from '../../utils/moveElement'; import { moveElement } from '../../utils/moveElement';
@ -98,7 +98,7 @@ export const AssetDialog = ({
if (isLoading) { if (isLoading) {
return ( return (
<ModalLayout onClose={onClose} labelledBy="asset-dialog-title" aria-busy> <ModalLayout onClose={onClose} labelledBy="asset-dialog-title" aria-busy>
<DialogTitle /> <DialogHeader />
<Flex justifyContent="center" paddingTop={4} paddingBottom={4}> <Flex justifyContent="center" paddingTop={4} paddingBottom={4}>
<Loader> <Loader>
{formatMessage({ {formatMessage({
@ -115,7 +115,7 @@ export const AssetDialog = ({
if (hasError) { if (hasError) {
return ( return (
<ModalLayout onClose={onClose} labelledBy="asset-dialog-title"> <ModalLayout onClose={onClose} labelledBy="asset-dialog-title">
<DialogTitle /> <DialogHeader />
<AnErrorOccurred /> <AnErrorOccurred />
<DialogFooter onClose={onClose} /> <DialogFooter onClose={onClose} />
</ModalLayout> </ModalLayout>
@ -125,7 +125,7 @@ export const AssetDialog = ({
if (!canRead) { if (!canRead) {
return ( return (
<ModalLayout onClose={onClose} labelledBy="asset-dialog-title"> <ModalLayout onClose={onClose} labelledBy="asset-dialog-title">
<DialogTitle /> <DialogHeader />
<NoPermissions /> <NoPermissions />
<DialogFooter onClose={onClose} /> <DialogFooter onClose={onClose} />
</ModalLayout> </ModalLayout>
@ -160,7 +160,7 @@ export const AssetDialog = ({
return ( return (
<ModalLayout onClose={onClose} labelledBy="asset-dialog-title" aria-busy={isLoading}> <ModalLayout onClose={onClose} labelledBy="asset-dialog-title" aria-busy={isLoading}>
<DialogTitle currentFolder={queryObject?.folder} onChangeFolder={handleFolderChange} /> <DialogHeader currentFolder={queryObject?.folder} onChangeFolder={handleFolderChange} />
<TabGroup <TabGroup
label={formatMessage({ label={formatMessage({

View File

@ -0,0 +1,128 @@
import React from 'react';
import { ThemeProvider, lightTheme } from '@strapi/design-system';
import { IntlProvider } from 'react-intl';
import { QueryClientProvider, QueryClient } from 'react-query';
import { fireEvent, render, screen } from '@testing-library/react';
import { useFolderStructure } from '../../../hooks/useFolderStructure';
import { DialogHeader } from '../DialogHeader';
jest.mock('../../../utils', () => ({
...jest.requireActual('../../../utils'),
getTrad: x => x,
}));
jest.mock('../../../hooks/useFolderStructure');
const setup = props => {
const withDefaults = {
currentFolder: null,
onChangeFolder: jest.fn(),
...props,
};
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
refetchOnWindowFocus: false,
},
},
});
return render(
<QueryClientProvider client={queryClient}>
<ThemeProvider theme={lightTheme}>
<IntlProvider locale="en" messages={{}}>
<DialogHeader {...withDefaults} />
</IntlProvider>
</ThemeProvider>
</QueryClientProvider>
);
};
describe('Upload || components || DialogHeader', () => {
it('should render folder name and back button', () => {
useFolderStructure.mockReturnValueOnce({
isLoading: false,
error: null,
data: [
{
value: null,
label: 'Media Library',
children: [
{
value: 1,
label: 'Cats',
children: [
{
value: 2,
label: 'Michka',
children: [],
},
],
},
],
},
],
});
const handleChangeFolderSpy = jest.fn();
const { queryByText } = setup({ currentFolder: 2, onChangeFolder: handleChangeFolderSpy });
expect(queryByText('Michka')).toBeInTheDocument();
const goBackButton = screen.getByLabelText('Go back');
expect(goBackButton).toBeInTheDocument();
fireEvent.click(goBackButton);
expect(handleChangeFolderSpy).toHaveBeenCalled();
});
it('should truncate long folder name', () => {
useFolderStructure.mockReturnValueOnce({
isLoading: false,
error: null,
data: [
{
value: null,
label: 'Media Library',
children: [
{
value: 1,
label: 'This is a really really long folder name that should be truncated',
children: [],
},
],
},
],
});
const { queryByText } = setup({ currentFolder: 1 });
expect(
queryByText('This is a really really long folder name that should be trun...')
).toBeInTheDocument();
});
it('should not render folder name and back button', () => {
useFolderStructure.mockReturnValueOnce({
isLoading: false,
error: null,
data: [
{
value: null,
label: 'Media Library',
children: [
{
value: 1,
label: 'Cats',
children: [],
},
],
},
],
});
const { queryByText } = setup();
expect(queryByText('Cats')).not.toBeInTheDocument();
expect(screen.queryByLabelText('Go back')).not.toBeInTheDocument();
});
});