Skip to content

Commit 505d463

Browse files
committed
e2e tests
1 parent 71b89e6 commit 505d463

File tree

6 files changed

+173
-14
lines changed

6 files changed

+173
-14
lines changed

packages/compass-e2e-tests/helpers/commands/set-validation.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CompassBrowser } from '../compass-browser';
22
import * as Selectors from '../selectors';
33

4-
export async function setValidation(
4+
export async function setValidationWithinValidationTab(
55
browser: CompassBrowser,
66
value: string
77
): Promise<void> {
@@ -32,3 +32,29 @@ export async function setValidation(
3232
// replaced
3333
await browser.pause(2000);
3434
}
35+
36+
export async function setValidation(
37+
browser: CompassBrowser,
38+
{
39+
connectionName,
40+
database,
41+
collection,
42+
validator,
43+
}: {
44+
connectionName: string;
45+
database: string;
46+
collection: string;
47+
validator: string;
48+
}
49+
): Promise<void> {
50+
await browser.navigateToCollectionTab(
51+
connectionName,
52+
database,
53+
collection,
54+
'Validation'
55+
);
56+
await browser.clickVisible(Selectors.AddRuleButton);
57+
const element = browser.$(Selectors.ValidationEditor);
58+
await element.waitForDisplayed();
59+
await browser.setValidationWithinValidationTab(validator);
60+
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,10 @@ export const ImportAnalyzeError =
654654
export const ImportConfirm =
655655
'[data-testid="import-modal"] [data-testid="import-button"]';
656656
export const ImportToast = '[data-testid="toast-import-toast"]';
657+
export const ImportToastErrorDetailsBtn =
658+
'[data-testid="toast-import-toast"] [data-testid="import-error-details-button"]';
659+
export const ImportErrorDetailsModal =
660+
'[data-testid="import-error-details-modal"]';
657661
export const ImportToastAbort = '[data-testid="toast-action-stop"]';
658662
export const ImportFieldLabel =
659663
'[data-testid="import-modal"] .import-field-label';

packages/compass-e2e-tests/tests/collection-documents-tab.test.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -691,16 +691,12 @@ FindIterable<Document> result = collection.find(filter);`);
691691
const REQUIRE_PHONE_VALIDATOR =
692692
'{ $jsonSchema: { bsonType: "object", required: [ "phone" ] } }';
693693
beforeEach(async function () {
694-
await browser.navigateToCollectionTab(
695-
DEFAULT_CONNECTION_NAME_1,
696-
'test',
697-
'numbers',
698-
'Validation'
699-
);
700-
await browser.clickVisible(Selectors.AddRuleButton);
701-
const element = browser.$(Selectors.ValidationEditor);
702-
await element.waitForDisplayed();
703-
await browser.setValidation(REQUIRE_PHONE_VALIDATOR);
694+
await browser.setValidation({
695+
connectionName: DEFAULT_CONNECTION_NAME_1,
696+
database: 'test',
697+
collection: 'numbers',
698+
validator: REQUIRE_PHONE_VALIDATOR,
699+
});
704700
});
705701

706702
it('Shows error info when inserting', async function () {

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

+133
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,139 @@ describe('Collection import', function () {
509509
await toastElement.waitForDisplayed({ reverse: true });
510510
});
511511

512+
context('with validation', function () {
513+
beforeEach(async function () {
514+
const FAILING_VALIDATOR =
515+
'{ $jsonSchema: { bsonType: "object", required: [ "abcdefgh" ] } }';
516+
await browser.setValidation({
517+
connectionName: DEFAULT_CONNECTION_NAME_1,
518+
database: 'test',
519+
collection: 'extended-json-file',
520+
validator: FAILING_VALIDATOR,
521+
});
522+
});
523+
524+
afterEach(async function () {
525+
await browser.navigateWithinCurrentCollectionTabs('Validation');
526+
await browser.setValidationWithinValidationTab('{}');
527+
});
528+
529+
it('with JSON + abort on error checked, it displays a validation error with details', async function () {
530+
const jsonPath = path.resolve(
531+
__dirname,
532+
'..',
533+
'fixtures',
534+
'three-documents.json'
535+
);
536+
537+
await browser.navigateWithinCurrentCollectionTabs('Documents');
538+
539+
// open the import modal
540+
await browser.clickVisible(Selectors.AddDataButton);
541+
const insertDocumentOption = browser.$(Selectors.ImportFileOption);
542+
await insertDocumentOption.waitForDisplayed();
543+
await browser.clickVisible(Selectors.ImportFileOption);
544+
545+
// Select the file.
546+
await browser.selectFile(Selectors.ImportFileInput, jsonPath);
547+
// Wait for the modal to appear.
548+
const importModal = browser.$(Selectors.ImportModal);
549+
await importModal.waitForDisplayed();
550+
551+
// Click the stop on errors checkbox.
552+
const stopOnErrorsCheckbox = browser.$(
553+
Selectors.ImportStopOnErrorsCheckbox
554+
);
555+
const stopOnErrorsLabel = stopOnErrorsCheckbox.parentElement();
556+
await stopOnErrorsLabel.click();
557+
558+
// Confirm import.
559+
await browser.clickVisible(Selectors.ImportConfirm);
560+
561+
// Wait for the modal to go away.
562+
await importModal.waitForDisplayed({ reverse: true });
563+
564+
// Wait for the error toast to appear
565+
const toastElement = browser.$(Selectors.ImportToast);
566+
await toastElement.waitForDisplayed();
567+
const errorText = await toastElement.getText();
568+
expect(errorText).to.include('Document failed validation');
569+
570+
// Visit error details
571+
await browser.clickVisible(Selectors.ImportToastErrorDetailsBtn);
572+
const errorDetailsModal = browser.$(Selectors.ImportErrorDetailsModal);
573+
await errorDetailsModal.waitForDisplayed();
574+
expect(await errorDetailsModal.getText()).to.include(
575+
'schemaRulesNotSatisfied'
576+
);
577+
await browser.clickVisible(Selectors.ErrorDetailsCloseButton);
578+
579+
// Close the toast
580+
await browser
581+
.$(Selectors.closeToastButton(Selectors.ImportToast))
582+
.waitForDisplayed();
583+
await browser.clickVisible(
584+
Selectors.closeToastButton(Selectors.ImportToast)
585+
);
586+
await toastElement.waitForDisplayed({ reverse: true });
587+
});
588+
589+
it('with CSV + abort on error unchecked, it includes the details in a file', async function () {
590+
const filename = 'array-documents.csv';
591+
const csvPath = path.resolve(__dirname, '..', 'fixtures', filename);
592+
593+
await browser.navigateWithinCurrentCollectionTabs('Documents');
594+
595+
// open the import modal
596+
await browser.clickVisible(Selectors.AddDataButton);
597+
const insertDocumentOption = browser.$(Selectors.ImportFileOption);
598+
await insertDocumentOption.waitForDisplayed();
599+
await browser.clickVisible(Selectors.ImportFileOption);
600+
601+
// Select the file.
602+
await browser.selectFile(Selectors.ImportFileInput, csvPath);
603+
// Wait for the modal to appear.
604+
const importModal = browser.$(Selectors.ImportModal);
605+
await importModal.waitForDisplayed();
606+
607+
// Confirm import.
608+
await browser.clickVisible(Selectors.ImportConfirm);
609+
610+
// Wait for the modal to go away.
611+
await importModal.waitForDisplayed({ reverse: true });
612+
613+
// Wait for the error toast to appear
614+
const toastElement = browser.$(Selectors.ImportToast);
615+
await toastElement.waitForDisplayed();
616+
const errorText = await toastElement.getText();
617+
console.log({ errorText });
618+
expect(errorText).to.include('Document failed validation');
619+
expect(errorText).to.include('VIEW LOG');
620+
621+
// Find the log file
622+
const logFilePath = path.resolve(
623+
compass.userDataPath || '',
624+
compass.appName || '',
625+
'ImportErrorLogs',
626+
`import-${filename}.log`
627+
);
628+
await expect(fs.stat(logFilePath)).to.not.be.rejected;
629+
630+
// Check the log file contents for 3 errors.
631+
const logFileContent = await fs.readFile(logFilePath, 'utf-8');
632+
expect(logFileContent.includes('schemaRulesNotSatisfied:'));
633+
634+
// Close the toast
635+
await browser
636+
.$(Selectors.closeToastButton(Selectors.ImportToast))
637+
.waitForDisplayed();
638+
await browser.clickVisible(
639+
Selectors.closeToastButton(Selectors.ImportToast)
640+
);
641+
await toastElement.waitForDisplayed({ reverse: true });
642+
});
643+
});
644+
512645
it('supports CSV files', async function () {
513646
const csvPath = path.resolve(__dirname, '..', 'fixtures', 'listings.csv');
514647

packages/compass-e2e-tests/tests/collection-validation-tab.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('Collection validation tab', function () {
5050
const element = browser.$(Selectors.ValidationEditor);
5151
await element.waitForDisplayed();
5252

53-
await browser.setValidation(validation);
53+
await browser.setValidationWithinValidationTab(validation);
5454
}
5555

5656
context('when the schema validation is set or modified', function () {
@@ -99,7 +99,7 @@ describe('Collection validation tab', function () {
9999
// Reset the validation again to make everything valid for future tests
100100

101101
// the automatic indentation and brackets makes multi-line values very fiddly here
102-
await browser.setValidation(PASSING_VALIDATOR);
102+
await browser.setValidationWithinValidationTab(PASSING_VALIDATOR);
103103
await browser.clickVisible(Selectors.ValidationLoadMatchingDocumentsBtn);
104104
await browser.clickVisible(
105105
Selectors.ValidationLoadNotMatchingDocumentsBtn

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export function showFailedToast(
234234
{showErrorDetails && (
235235
<Link
236236
onClick={showErrorDetails}
237-
data-testid="insert-document-error-details-button"
237+
data-testid="import-error-details-button"
238238
>
239239
View error details
240240
</Link>

0 commit comments

Comments
 (0)