Skip to content

Commit 78f026f

Browse files
authored
fix(import): If listCSVFields() or analyzeCSVFields() fails it will display the error in the modal COMPASS-6737 (#4278)
* If listCSVFields() or analyzeCSVFields() fails it will display the error in the modal * close the modal if open * rather just close the modal when disconnecting * better: set isOpen inside the reducer * minus exclusive test * lint * log the full error message if it doesn't match * move the tests out of the beforeEach hook * sans exclusive test.. * workaround for race conditions where things sometimes break before it can abort
1 parent e4f2643 commit 78f026f

File tree

6 files changed

+75
-11
lines changed

6 files changed

+75
-11
lines changed
39 Bytes
Binary file not shown.

packages/compass-e2e-tests/helpers/insert-data.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export async function createDummyCollections(): Promise<void> {
6666
promises.push(createBlankCollection(db, 'extended-json-file'));
6767
promises.push(createBlankCollection(db, 'csv-file'));
6868
promises.push(createBlankCollection(db, 'bom-csv-file'));
69+
promises.push(createBlankCollection(db, 'latin1'));
6970
promises.push(createBlankCollection(db, 'broken-delimiter'));
7071
promises.push(createBlankCollection(db, 'numbers'));
7172
promises.push(createBlankCollection(db, 'import-stop-first-error'));

packages/compass-e2e-tests/helpers/selectors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,16 @@ export const ImportFileInput = '#import-file_file_input';
558558
export const FileTypeJSON = '[data-testid="select-file-type-json"]';
559559
export const FileTypeCSV = '[data-testid="select-file-type-csv"]';
560560
export const ImportSkipAnalyze = '[data-testid="skip-csv-analyze-button"]';
561+
export const ImportAnalyzeError =
562+
'[data-testid="import-modal"] [data-testid="analyze-error"]';
561563
export const ImportConfirm =
562564
'[data-testid="import-modal"] [data-testid="import-button"]';
563565
export const ImportToast = '[data-testid="toast-import-toast"]';
564566
export const ImportToastAbort =
565567
'[data-testid="toast-import-toast"] [data-testid="toast-action-stop"]';
566568

569+
export const ImportModalCloseButton = `${ImportModal} [aria-label*="Close"]`;
570+
567571
export const importPreviewFieldHeaderField = (fieldName: string): string => {
568572
return `[data-testid="import-preview-field-type-select-menu-${fieldName}"]`;
569573
};

packages/compass-e2e-tests/tests/collection-import.test.ts

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ describe('Collection import', function () {
9797
await createNumbersCollection();
9898
await createDummyCollections();
9999
await browser.connectWithConnectionString();
100+
101+
delete process.env.COMPASS_E2E_TEST_IMPORT_ABORT_TIMEOUT;
100102
});
101103

102104
after(async function () {
@@ -920,7 +922,11 @@ describe('Collection import', function () {
920922
});
921923

922924
describe('aborting an import', function () {
923-
beforeEach(async function () {
925+
afterEach(function () {
926+
delete process.env.COMPASS_E2E_TEST_IMPORT_ABORT_TIMEOUT;
927+
});
928+
929+
it('aborts an in progress import', async function () {
924930
process.env.COMPASS_E2E_TEST_IMPORT_ABORT_TIMEOUT = 'true';
925931

926932
// 16116 documents.
@@ -947,13 +953,7 @@ describe('Collection import', function () {
947953

948954
// Confirm import.
949955
await browser.clickVisible(Selectors.ImportConfirm);
950-
});
951956

952-
afterEach(function () {
953-
delete process.env.COMPASS_E2E_TEST_IMPORT_ABORT_TIMEOUT;
954-
});
955-
956-
it('aborts an in progress import', async function () {
957957
// Wait for the in progress toast to appear and click stop.
958958
await browser.clickVisible(Selectors.ImportToastAbort);
959959

@@ -966,7 +966,12 @@ describe('Collection import', function () {
966966

967967
// Check it displays that the import was aborted.
968968
const toastText = await toastElement.getText();
969-
expect(toastText).to.include('Import aborted.');
969+
try {
970+
expect(toastText).to.include('Import aborted');
971+
} catch (err) {
972+
console.log(toastText);
973+
throw err;
974+
}
970975

971976
// Check at least one and fewer than 16116 documents were imported.
972977
const messageElement = await browser.$(
@@ -985,6 +990,32 @@ describe('Collection import', function () {
985990
});
986991

987992
it('aborts when disconnected', async function () {
993+
process.env.COMPASS_E2E_TEST_IMPORT_ABORT_TIMEOUT = 'true';
994+
995+
// 16116 documents.
996+
const csvPath = path.resolve(__dirname, '..', 'fixtures', 'listings.csv');
997+
998+
await browser.navigateToCollectionTab(
999+
'test',
1000+
'import-abort',
1001+
'Documents'
1002+
);
1003+
1004+
// Open the import modal.
1005+
await browser.clickVisible(Selectors.AddDataButton);
1006+
const insertDocumentOption = await browser.$(Selectors.ImportFileOption);
1007+
await insertDocumentOption.waitForDisplayed();
1008+
await browser.clickVisible(Selectors.ImportFileOption);
1009+
1010+
// Select the file.
1011+
await browser.selectFile(Selectors.ImportFileInput, csvPath);
1012+
1013+
// Wait for the modal to appear.
1014+
const importModal = await browser.$(Selectors.ImportModal);
1015+
await importModal.waitForDisplayed();
1016+
1017+
// Confirm import.
1018+
await browser.clickVisible(Selectors.ImportConfirm);
9881019
// Wait for the in progress toast to appear.
9891020
await browser.$(Selectors.ImportToastAbort).waitForDisplayed();
9901021

@@ -1001,7 +1032,12 @@ describe('Collection import', function () {
10011032
const toastElement = await browser.$(Selectors.ImportToast);
10021033
// Check it displays that the import was aborted.
10031034
const toastText = await toastElement.getText();
1004-
expect(toastText).to.include('Import aborted.');
1035+
try {
1036+
expect(toastText).to.include('Import aborted');
1037+
} catch (err) {
1038+
console.log(toastText);
1039+
throw err;
1040+
}
10051041

10061042
// Close toast.
10071043
await browser.clickVisible(

packages/compass-import-export/src/components/import-modal.tsx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ const fieldsHeadingStylesLight = css({
5858
borderBottom: `2px solid ${palette.gray.light2}`,
5959
});
6060

61+
const analyzeContainerStyles = css({
62+
// Remove double spacing between the analyze container and the form action
63+
// buttons caused by analyze always being the last item when visible.
64+
marginBottom: 0,
65+
});
66+
6167
type ImportModalProps = {
6268
isOpen: boolean;
6369
ns: string;
@@ -94,6 +100,7 @@ type ImportModalProps = {
94100
setFieldType: (path: string, bsonType: string) => void;
95101
previewLoaded: boolean;
96102
csvAnalyzed: boolean;
103+
analyzeError?: Error;
97104
};
98105

99106
function ImportModal({
@@ -122,6 +129,7 @@ function ImportModal({
122129
setFieldType,
123130
previewLoaded,
124131
csvAnalyzed,
132+
analyzeError,
125133
}: ImportModalProps) {
126134
const darkMode = useDarkMode();
127135

@@ -185,8 +193,8 @@ function ImportModal({
185193
ignoreBlanks={ignoreBlanks}
186194
setIgnoreBlanks={setIgnoreBlanks}
187195
/>
188-
{fileType === 'csv' && (
189-
<FormFieldContainer>
196+
{fileType === 'csv' && !analyzeError && (
197+
<FormFieldContainer className={analyzeContainerStyles}>
190198
<Body
191199
as="h3"
192200
className={cx(
@@ -215,6 +223,12 @@ function ImportModal({
215223
{errors.length > 0 && (
216224
<ErrorSummary errors={errors.map((error) => error.message)} />
217225
)}
226+
{analyzeError && (
227+
<ErrorSummary
228+
data-testid="analyze-error"
229+
errors={[analyzeError.message]}
230+
/>
231+
)}
218232
</ModalBody>
219233
<ModalFooter>
220234
<Button
@@ -258,6 +272,7 @@ const mapStateToProps = (state: RootImportState) => ({
258272
values: state.importData.values,
259273
previewLoaded: state.importData.previewLoaded,
260274
csvAnalyzed: state.importData.analyzeStatus === 'COMPLETED',
275+
analyzeError: state.importData.analyzeError,
261276
});
262277

263278
/**

packages/compass-import-export/src/modules/import.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ const loadTypes = (
598598
);
599599
dispatch({
600600
type: ANALYZE_FAILED,
601+
error: err,
601602
});
602603
}
603604
};
@@ -654,6 +655,12 @@ const loadCSVPreviewDocs = (): ThunkAction<
654655
'Failed to load preview docs',
655656
err
656657
);
658+
659+
// The most likely way to get here is if the file is not encoded as UTF8.
660+
dispatch({
661+
type: ANALYZE_FAILED,
662+
error: err,
663+
});
657664
}
658665
};
659666
};
@@ -1104,6 +1111,7 @@ const reducer = (state = INITIAL_STATE, action: AnyAction): State => {
11041111
...state,
11051112
analyzeAbortController: undefined,
11061113
abortController: undefined,
1114+
isOpen: false,
11071115
};
11081116
}
11091117

0 commit comments

Comments
 (0)