generated from pagopa/template-payments-java-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [P4ADEV-2011] file uploader (#49)
- Loading branch information
Showing
19 changed files
with
336 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { describe, it, vi, expect, beforeEach } from 'vitest'; | ||
import { fireEvent, render, screen, within } from '@testing-library/react'; | ||
import ImportFlow from './ImportFlowPage'; | ||
import { useParams } from 'react-router-dom'; | ||
|
||
vi.mock('react-router-dom', () => ({ | ||
useNavigate: vi.fn(), | ||
useParams: vi.fn(), | ||
})); | ||
|
||
describe('ImportFlow', () => { | ||
const mockUseParams = vi.mocked(useParams); | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe('no select Config', () => { | ||
beforeEach(() => { | ||
mockUseParams.mockReturnValue({ category: 'reporting' }); | ||
}); | ||
|
||
it('renders without select', () => { | ||
render(<ImportFlow/>); | ||
|
||
expect(screen.getByText('commons.routes.REPORTING_IMPORT_FLOW')).toBeDefined(); | ||
expect(screen.getByText('commons.flowImport.description')).toBeDefined(); | ||
expect(screen.getByText('commons.flowImport.boxTitle')).toBeDefined(); | ||
expect(screen.getByText('commons.flowImport.boxDescription')).toBeDefined(); | ||
expect(screen.getByText('commons.flowImport.manualLink')).toBeDefined(); | ||
expect(screen.queryByText('commons.requiredFieldDescription')).toBeNull(); | ||
expect(screen.queryByLabelText('commons.flowType')).toBeNull(); | ||
}); | ||
|
||
it('should enable button when a file is uploaded', async () => { | ||
|
||
render(<ImportFlow />); | ||
|
||
const file = new File(['content'], 'test.zip', { type: 'application/zip' }); | ||
const dropZone = screen.getByTestId('drop-zone'); | ||
|
||
fireEvent.dragOver(dropZone); | ||
fireEvent.drop(dropZone, { | ||
dataTransfer: { | ||
files: [file] | ||
} | ||
}); | ||
|
||
await vi.waitFor(() => expect(screen.getAllByText('test.zip')).toBeDefined()); | ||
const successButton = screen.getByTestId('success-button'); | ||
|
||
expect(successButton).toHaveProperty('disabled', false); | ||
}); | ||
}); | ||
|
||
describe('select config', () => { | ||
beforeEach(() => { | ||
mockUseParams.mockReturnValue({ category: 'treasury' }); | ||
}); | ||
|
||
it('renders with select', () => { | ||
render(<ImportFlow />); | ||
|
||
expect(screen.getByText('commons.routes.TREASURY_IMPORT_FLOW')).toBeDefined(); | ||
expect(screen.getByText('commons.requiredFieldDescription')).toBeDefined(); | ||
expect(screen.getByRole('select-flowType')).toBeDefined(); | ||
expect(screen.getByTestId('success-button')).toHaveProperty('disabled', true); | ||
}); | ||
|
||
it('should show all flow type options when select is clicked', () => { | ||
render(<ImportFlow />); | ||
|
||
const selectCombo = screen.getByRole('combobox', { name: 'commons.flowType' }); | ||
fireEvent.mouseDown(selectCombo); | ||
|
||
const listbox = within(screen.getByRole('listbox')); | ||
|
||
const options = [ | ||
'Giornale di Cassa XLS', | ||
'Giornale di Cassa CSV', | ||
'Giornale di Cassa OPI', | ||
'Estrato conto poste' | ||
]; | ||
|
||
options.forEach(option => { | ||
expect(listbox.getByText(option)).toBeDefined(); | ||
}); | ||
}); | ||
it('should enable button when a file is uploaded and a flow type is selected', async () => { | ||
|
||
render(<ImportFlow />); | ||
|
||
const file = new File(['content'], 'test.zip', { type: 'application/zip' }); | ||
const dropZone = screen.getByTestId('drop-zone'); | ||
|
||
fireEvent.dragOver(dropZone); | ||
fireEvent.drop(dropZone, { | ||
dataTransfer: { | ||
files: [file] | ||
} | ||
}); | ||
|
||
await vi.waitFor(() => expect(screen.getAllByText('test.zip')).toBeDefined()); | ||
|
||
const selectCombo = screen.getByRole('combobox', { name: 'commons.flowType' }); | ||
fireEvent.mouseDown(selectCombo); | ||
|
||
const firstOption = within(screen.getByRole('listbox')).getAllByRole('option')[0]; | ||
fireEvent.click(firstOption); | ||
|
||
expect(screen.getByTestId('success-button')).toHaveProperty('disabled', false); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { Box, Button, FormControl, Grid, InputLabel, MenuItem, Select, Typography } from '@mui/material'; | ||
import { theme } from '@pagopa/mui-italia'; | ||
import FileUploader from '../FileUploader/FileUploader'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { AltRoute, ArrowBack } from '@mui/icons-material'; | ||
import { useState } from 'react'; | ||
import { useNavigate, useParams } from 'react-router-dom'; | ||
import TitleComponent from '../TitleComponent/TitleComponent'; | ||
import { importFlowConfig } from '../../models/ImportDetails'; | ||
|
||
const ImportFlow = () => { | ||
|
||
const { t } = useTranslation(); | ||
const navigate = useNavigate(); | ||
const { category } = useParams<{category: string}>(); | ||
|
||
const [uploading, setUploading] = useState(false); | ||
const [progress, setProgress] = useState(0); | ||
const [flowType, setFlowType] = useState(''); | ||
const [file, setFile] = useState<File | null>(null); | ||
|
||
const config = importFlowConfig[category as keyof typeof importFlowConfig]; | ||
|
||
const handleDisabledButton = () => { | ||
const defaultCondition = uploading || !file; | ||
return config?.flowTypes ? (!flowType || defaultCondition) : defaultCondition; | ||
}; | ||
|
||
return ( | ||
<> | ||
<Grid container direction="column" alignItems="center" marginTop={2}> | ||
<Grid container direction="column" alignItems="left" marginTop={2} ml={1} mb={4}> | ||
<TitleComponent | ||
title={t(config.title)} | ||
description={t('commons.flowImport.description')} | ||
/> | ||
<Box bgcolor={theme.palette.common.white} borderRadius={0.5} p={3} gap={3}> | ||
<Grid item lg={12} mb={2}> | ||
<Grid item lg={12} mb={2}> | ||
<Typography variant='h6' gutterBottom>{t('commons.flowImport.boxTitle')}</Typography> | ||
<Typography variant='caption' gutterBottom>{t('commons.flowImport.boxDescription')}</Typography> | ||
</Grid> | ||
<Button variant='naked' size='small'>{t('commons.flowImport.manualLink')}</Button> | ||
</Grid> | ||
{config?.requiredFieldDescription && | ||
<Typography variant="caption" mb={3} display={'block'} sx={{ color: theme.palette.error.dark }}> | ||
{t(config.requiredFieldDescription)} | ||
</Typography> | ||
} | ||
<Box borderRadius={1} border={1} p={3} gap={2} borderColor={theme.palette.divider}> | ||
<FileUploader | ||
uploading={uploading} | ||
setUploading={setUploading} | ||
progress={progress} | ||
setProgress={setProgress} | ||
file={file} setFile={setFile} | ||
description={t('FileUploaderFlowImport.description')} | ||
requiredFileText={t('FileUploaderFlowImport.requiredFileText')} | ||
fileExtensionsAllowed={config.fileExtensionsAllowed} | ||
/> | ||
</Box> | ||
{config?.flowTypes && | ||
<Box borderRadius={1} border={1} p={3} gap={2} mt={3} borderColor={theme.palette.divider}> | ||
<Grid container direction={'row'} mb={3}> | ||
<AltRoute sx={{ transform: 'rotate(90deg)' }}/> | ||
<Typography fontWeight={600} ml={1}> | ||
{t('commons.flowType')} | ||
</Typography> | ||
</Grid> | ||
<FormControl role='select-flowType' required fullWidth size="small"> | ||
<InputLabel id='select-label'>{t('commons.flowType')}</InputLabel> | ||
<Select | ||
value={flowType} | ||
labelId='select-label' | ||
label={t('commons.flowType')} | ||
onChange={(event) => setFlowType(event.target.value)} | ||
> | ||
{config.flowTypes.map((option, i) => ( | ||
<MenuItem key={i} value={option}> | ||
{option} | ||
</MenuItem> | ||
))} | ||
</Select> | ||
</FormControl> | ||
</Box> | ||
} | ||
</Box> | ||
</Grid> | ||
</Grid> | ||
<Grid container direction={'row'} justifyContent={'space-between'} ml={1}> | ||
<Grid item> | ||
<Button | ||
size="large" | ||
variant="outlined" | ||
fullWidth | ||
startIcon={<ArrowBack />} | ||
onClick={() => navigate(config.backRoute) } | ||
> | ||
{t('commons.exit')} | ||
</Button> | ||
</Grid> | ||
<Grid item> | ||
<Button | ||
data-testid="success-button" | ||
size="large" | ||
variant="contained" | ||
fullWidth | ||
disabled = {handleDisabledButton()} | ||
onClick={() => navigate(config.successRoute) } | ||
> | ||
{t('commons.flowImport.uploadButton')} | ||
</Button> | ||
</Grid> | ||
</Grid> | ||
</> | ||
); | ||
}; | ||
|
||
export default ImportFlow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.