Skip to content

Commit 827b1d3

Browse files
[ui-importer] show field separator and other dropdown only for CSV (#4123)
1 parent 4d9bd21 commit 827b1d3

File tree

5 files changed

+85
-34
lines changed

5 files changed

+85
-34
lines changed

desktop/core/src/desktop/js/apps/newimporter/ImporterFilePreview/SourceConfiguration/SourceConfiguration.test.tsx

+39-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ import { render, fireEvent, waitFor } from '@testing-library/react';
33
import userEvent from '@testing-library/user-event';
44
import '@testing-library/jest-dom';
55
import SourceConfiguration from './SourceConfiguration';
6-
import { FileFormatResponse } from '../../types';
7-
import { separator } from '../../constants';
6+
import { FileFormatResponse, ImporterFileTypes } from '../../types';
87

98
describe('SourceConfiguration Component', () => {
109
const mockSetFileFormat = jest.fn();
1110
const mockFileFormat: FileFormatResponse = {
1211
quoteChar: '"',
1312
recordSeparator: '\\n',
14-
type: 'csv',
13+
type: ImporterFileTypes.EXCEL,
1514
hasHeader: true,
1615
fieldSeparator: ',',
1716
status: 0
@@ -22,27 +21,59 @@ describe('SourceConfiguration Component', () => {
2221
});
2322

2423
it('should render the component', () => {
25-
const { getByText, getAllByRole } = render(
24+
const { getByText } = render(
2625
<SourceConfiguration fileFormat={mockFileFormat} setFileFormat={mockSetFileFormat} />
2726
);
2827
expect(getByText('Configure source')).toBeInTheDocument();
29-
expect(getAllByRole('combobox')).toHaveLength(5);
3028
});
3129

32-
it('calls setFileFormat on option change', async () => {
30+
it('should call setFileFormat on option change', async () => {
3331
const { getByText, getAllByRole } = render(
3432
<SourceConfiguration fileFormat={mockFileFormat} setFileFormat={mockSetFileFormat} />
3533
);
3634

3735
const selectElement = getAllByRole('combobox')[0];
3836
await userEvent.click(selectElement);
39-
fireEvent.click(getByText(separator[3].label));
37+
fireEvent.click(getByText('CSV'));
4038

4139
await waitFor(() =>
4240
expect(mockSetFileFormat).toHaveBeenCalledWith({
4341
...mockFileFormat,
44-
fieldSeparator: separator[3].value
42+
type: ImporterFileTypes.CSV
4543
})
4644
);
4745
});
46+
47+
it('should show fieldSepator and other downdown when fileType is CSV', () => {
48+
const { getAllByRole, getByText } = render(
49+
<SourceConfiguration
50+
fileFormat={{ ...mockFileFormat, type: ImporterFileTypes.CSV }}
51+
setFileFormat={mockSetFileFormat}
52+
/>
53+
);
54+
55+
const selectElement = getAllByRole('combobox');
56+
57+
expect(selectElement).toHaveLength(5);
58+
expect(getByText('File Type')).toBeInTheDocument();
59+
expect(getByText('Has Header')).toBeInTheDocument();
60+
expect(getByText('Field Separator')).toBeInTheDocument();
61+
expect(getByText('Record Separator')).toBeInTheDocument();
62+
expect(getByText('Quote Character')).toBeInTheDocument();
63+
});
64+
65+
it('should not show fieldSepator and other downdown when fileType is not CSV', () => {
66+
const { getAllByRole, getByText, queryByText } = render(
67+
<SourceConfiguration fileFormat={mockFileFormat} setFileFormat={mockSetFileFormat} />
68+
);
69+
70+
const selectElement = getAllByRole('combobox');
71+
72+
expect(selectElement).toHaveLength(2);
73+
expect(getByText('File Type')).toBeInTheDocument();
74+
expect(getByText('Has Header')).toBeInTheDocument();
75+
expect(queryByText('Field Separator')).not.toBeInTheDocument();
76+
expect(queryByText('Record Separator')).not.toBeInTheDocument();
77+
expect(queryByText('Quote Character')).not.toBeInTheDocument();
78+
});
4879
});

desktop/core/src/desktop/js/apps/newimporter/ImporterFilePreview/SourceConfiguration/SourceConfiguration.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ const SourceConfiguration = ({
4545
[fileFormat, setFileFormat]
4646
);
4747

48+
const filteredSourceConfigs = sourceConfigs.filter(config => !config.hidden?.(fileFormat?.type));
49+
4850
return (
4951
<details className="hue-importer-configuration">
5052
<summary className="hue-importer-configuration__summary">
5153
<ConfigureIcon />
5254
{t('Configure source')}
5355
</summary>
5456
<div className="hue-importer-configuration-options">
55-
{sourceConfigs.map(config => (
57+
{filteredSourceConfigs.map(config => (
5658
<div key={config.name}>
5759
<label htmlFor={config.name}>{t(config.label)}</label>
5860
<Select

desktop/core/src/desktop/js/apps/newimporter/ImporterSourceSelector/ImporterSourceSelector.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Button from 'cuix/dist/components/Button';
1919
import DocumentationIcon from '@cloudera/cuix-core/icons/react/DocumentationIcon';
2020
import { hueWindow } from 'types/types';
2121

22-
import { FileMetaData, LocalFileUploadResponse } from '../types';
22+
import { FileMetaData, ImporterFileSource, LocalFileUploadResponse } from '../types';
2323
import { i18nReact } from '../../../utils/i18nReact';
2424
import { UPLOAD_LOCAL_FILE_API_URL } from '../api';
2525
import useSaveData from '../../../utils/hooks/useSaveData/useSaveData';
@@ -72,7 +72,7 @@ const ImporterSourceSelector = ({ setFileMetaData }: ImporterSourceSelectorProps
7272
setFileMetaData({
7373
path: data.local_file_url,
7474
type: data.file_type,
75-
source: 'localfile'
75+
source: ImporterFileSource.LOCAL
7676
});
7777
},
7878
onError: error => {

desktop/core/src/desktop/js/apps/newimporter/constants.ts

+26-19
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17-
import { FileFormatResponse } from './types';
17+
import { FileFormatResponse, ImporterFileTypes } from './types';
1818

1919
export const separator = [
2020
{ value: ',', label: 'Comma (,)' },
@@ -32,38 +32,45 @@ export const separator = [
3232
export const sourceConfigs: {
3333
name: keyof FileFormatResponse;
3434
label: string;
35-
options: { label: string; value: string | boolean }[];
35+
hidden?: (type?: ImporterFileTypes) => boolean;
36+
options: {
37+
label: string;
38+
value: string | boolean;
39+
}[];
3640
}[] = [
41+
{
42+
name: 'type',
43+
label: 'File Type',
44+
options: [
45+
{ value: ImporterFileTypes.CSV, label: 'CSV' },
46+
{ value: ImporterFileTypes.JSON, label: 'JSON' },
47+
{ value: ImporterFileTypes.EXCEL, label: 'Excel' }
48+
]
49+
},
50+
{
51+
name: 'hasHeader',
52+
label: 'Has Header',
53+
options: [
54+
{ value: true, label: 'Yes' },
55+
{ value: false, label: 'No' }
56+
]
57+
},
3758
{
3859
name: 'fieldSeparator',
3960
label: 'Field Separator',
61+
hidden: (type?: ImporterFileTypes) => type !== ImporterFileTypes.CSV,
4062
options: separator
4163
},
4264
{
4365
name: 'recordSeparator',
4466
label: 'Record Separator',
67+
hidden: (type?: ImporterFileTypes) => type !== ImporterFileTypes.CSV,
4568
options: separator
4669
},
4770
{
4871
name: 'quoteChar',
4972
label: 'Quote Character',
73+
hidden: (type?: ImporterFileTypes) => type !== ImporterFileTypes.CSV,
5074
options: separator
51-
},
52-
{
53-
name: 'hasHeader',
54-
label: 'Has Header',
55-
options: [
56-
{ value: true, label: 'Yes' },
57-
{ value: false, label: 'No' }
58-
]
59-
},
60-
{
61-
name: 'type',
62-
label: 'File Type',
63-
options: [
64-
{ value: 'csv', label: 'CSV File' },
65-
{ value: 'json', label: 'JSON' },
66-
{ value: 'excel', label: 'Excel File' }
67-
]
6875
}
6976
];

desktop/core/src/desktop/js/apps/newimporter/types.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17+
export const enum ImporterFileTypes {
18+
CSV = 'csv',
19+
JSON = 'json',
20+
EXCEL = 'excel'
21+
}
22+
23+
export enum ImporterFileSource {
24+
LOCAL = 'localfile',
25+
REMOTE = 'file'
26+
}
27+
1728
export interface LocalFileUploadResponse {
1829
local_file_url: string;
19-
file_type: string;
30+
file_type: ImporterFileTypes;
2031
}
2132

2233
export interface FileFormatResponse {
@@ -25,13 +36,13 @@ export interface FileFormatResponse {
2536
quoteChar: string;
2637
recordSeparator: string;
2738
status: number;
28-
type: string;
39+
type: ImporterFileTypes;
2940
}
3041

3142
export interface FileMetaData {
3243
path: string;
33-
type?: string;
34-
source: 'localfile' | 'file';
44+
type: ImporterFileTypes;
45+
source: ImporterFileSource;
3546
}
3647

3748
export type GuessFieldTypesColumn = {

0 commit comments

Comments
 (0)