void;
}): React.ReactElement {
- const { isSRV } = connectionStringUrl;
const { hosts } = fields;
return (
@@ -41,7 +42,7 @@ function GeneralTab({
/>
- {!isSRV && hosts.value.length === 1 && (
+ {!connectionStringUrl.isSRV && hosts.value.length === 1 && (
+ );
+
+ const addHostButton = screen.getByLabelText('Add new host');
+ fireEvent.click(addHostButton);
+ });
+
+ it('the updated connection string should not have directConnection set', function () {
+ expect(
+ setConnectionStringUrlSpy.firstCall.args[0].searchParams.get(
+ 'directConnection'
+ )
+ ).to.equal(null);
+ });
+
+ it('should call to update the connection string url with a new host', function () {
+ expect(setConnectionStringUrlSpy.callCount).to.equal(1);
+ expect(setConnectionStringUrlSpy.firstCall.args[0].toString()).to.equal(
+ 'mongodb://0ranges:p!neapp1es@outerspace:27777,outerspace:27778/?ssl=true'
+ );
+ });
+
+ it('should not call to update the host fields', function () {
+ expect(setConnectionFieldSpy.callCount).to.equal(0);
+ });
+ });
});
diff --git a/packages/connect-form/src/components/advanced-options-tabs/general/host-input.tsx b/packages/connect-form/src/components/advanced-options-tabs/general/host-input.tsx
index 26044b2ced9..614d5cc11a9 100644
--- a/packages/connect-form/src/components/advanced-options-tabs/general/host-input.tsx
+++ b/packages/connect-form/src/components/advanced-options-tabs/general/host-input.tsx
@@ -124,10 +124,13 @@ function HostInput({
// connection string url.
const updatedHosts = [...hosts.value];
updatedHosts[index] = event.target.value;
- setConnectionField('hosts', {
- value: updatedHosts,
- error: (err as Error).message,
- warning: null,
+ setConnectionField({
+ type: 'set-connection-string-field',
+ fieldName: 'hosts',
+ value: {
+ value: updatedHosts,
+ error: (err as Error).message,
+ },
});
}
}
diff --git a/packages/connect-form/src/components/advanced-options-tabs/general/schema-input.spec.tsx b/packages/connect-form/src/components/advanced-options-tabs/general/schema-input.spec.tsx
index 8348aa00512..c8d151337a1 100644
--- a/packages/connect-form/src/components/advanced-options-tabs/general/schema-input.spec.tsx
+++ b/packages/connect-form/src/components/advanced-options-tabs/general/schema-input.spec.tsx
@@ -7,9 +7,11 @@ import ConnectionStringUrl from 'mongodb-connection-string-url';
import SchemaInput from './schema-input';
describe('SchemaInput', function () {
+ let setConnectionFieldSpy: sinon.SinonSpy;
let setConnectionStringUrlSpy: sinon.SinonSpy;
beforeEach(function () {
+ setConnectionFieldSpy = sinon.spy();
setConnectionStringUrlSpy = sinon.spy();
});
@@ -22,7 +24,9 @@ describe('SchemaInput', function () {
);
render(
);
@@ -81,7 +85,9 @@ describe('SchemaInput', function () {
);
render(
);
@@ -124,7 +130,9 @@ describe('SchemaInput', function () {
);
render(
);
@@ -155,7 +163,9 @@ describe('SchemaInput', function () {
);
render(
);
@@ -190,4 +200,42 @@ describe('SchemaInput', function () {
});
});
});
+
+ describe('when there is a conversion error', function () {
+ beforeEach(function () {
+ const connectionStringUrl = new ConnectionStringUrl(
+ 'mongodb://0ranges:p!neapp1es@outerspace:27017/?ssl=true'
+ );
+ render(
+
+ );
+ });
+
+ it('should render the schema conversion error', function () {
+ expect(screen.getByText('aaaa!!!1!')).to.be.visible;
+ });
+
+ describe('when the x button is clicked', function () {
+ beforeEach(function () {
+ const hideErrorButton = screen.getByLabelText('X Icon');
+ fireEvent.click(hideErrorButton);
+ });
+
+ it('should call to hide the error on the fields', function () {
+ expect(setConnectionFieldSpy.callCount).to.equal(1);
+ expect(setConnectionFieldSpy.firstCall.args[0]).to.deep.equal({
+ type: 'set-connection-string-field',
+ fieldName: 'isSRV',
+ value: {
+ conversionError: null,
+ },
+ });
+ });
+ });
+ });
});
diff --git a/packages/connect-form/src/components/advanced-options-tabs/general/schema-input.tsx b/packages/connect-form/src/components/advanced-options-tabs/general/schema-input.tsx
index 844485439ba..244f2d2b511 100644
--- a/packages/connect-form/src/components/advanced-options-tabs/general/schema-input.tsx
+++ b/packages/connect-form/src/components/advanced-options-tabs/general/schema-input.tsx
@@ -1,5 +1,7 @@
import React from 'react';
import {
+ Banner,
+ BannerVariant,
Description,
Label,
RadioBox,
@@ -8,6 +10,7 @@ import {
import ConnectionStringUrl from 'mongodb-connection-string-url';
import { defaultHostname } from '../../../constants/default-connection';
+import { SetConnectionField } from '../../../hooks/use-connect-form';
enum MONGODB_SCHEMA {
MONGODB = 'MONGODB',
@@ -86,32 +89,57 @@ function updateConnectionStringSchema(
function SchemaInput({
connectionStringUrl,
+ schemaConversionError,
+ setConnectionField,
setConnectionStringUrl,
}: {
connectionStringUrl: ConnectionStringUrl;
+ schemaConversionError: string | null;
+ setConnectionField: SetConnectionField;
setConnectionStringUrl: (connectionStringUrl: ConnectionStringUrl) => void;
}): React.ReactElement {
const { isSRV } = connectionStringUrl;
+ function onChangeConnectionSchema(
+ event: React.ChangeEvent
+ ) {
+ try {
+ const newConnectionStringUrl = updateConnectionStringSchema(
+ connectionStringUrl,
+ event.target.value as MONGODB_SCHEMA
+ );
+
+ setConnectionStringUrl(newConnectionStringUrl);
+ } catch (err) {
+ setConnectionField({
+ type: 'set-connection-string-field',
+ fieldName: 'isSRV',
+ value: {
+ conversionError: `Error updating connection schema: ${
+ (err as Error).message
+ }`,
+ },
+ });
+ }
+ }
+
+ function onCloseSchemaConversionError() {
+ setConnectionField({
+ type: 'set-connection-string-field',
+ fieldName: 'isSRV',
+ value: {
+ conversionError: null,
+ },
+ });
+ }
+
return (
<>
Schema
) => {
- try {
- const newConnectionStringUrl = updateConnectionStringSchema(
- connectionStringUrl,
- event.target.value as MONGODB_SCHEMA
- );
-
- setConnectionStringUrl(newConnectionStringUrl);
- } catch (e) {
- // TODO: show a targeted error/warning why we can't convert.
- //
- }
- }}
+ onChange={onChangeConnectionSchema}
>
mongodb
mongodb+srv
@@ -119,6 +147,15 @@ function SchemaInput({
{isSRV ? srvSchemaDescription : regularSchemaDescription}
+ {schemaConversionError && (
+
+ {schemaConversionError}
+
+ )}
>
);
}
diff --git a/packages/connect-form/src/hooks/use-connect-form.ts b/packages/connect-form/src/hooks/use-connect-form.ts
index adbf9116759..65367399975 100644
--- a/packages/connect-form/src/hooks/use-connect-form.ts
+++ b/packages/connect-form/src/hooks/use-connect-form.ts
@@ -6,14 +6,11 @@ import { defaultConnectionString } from '../constants/default-connection';
export interface ConnectFormFields {
hosts: {
value: string[];
- warning: null | string;
error: null | string;
};
- // isSRV: {
- // value: boolean;
- // // warning: null | string,
- // // error: null | string
- // };
+ isSRV: {
+ conversionError: null | string;
+ };
}
export interface ConnectFormState {
connectionStringInvalidError: string | null;
@@ -22,12 +19,22 @@ export interface ConnectFormState {
fields: ConnectFormFields;
}
-export type SetConnectionField = (
- fieldName: 'hosts',
- value: ConnectFormFields['hosts']
-) => void;
+export type SetConnectionFieldAction =
+ | {
+ type: 'set-connection-string-field';
+ fieldName: 'hosts';
+ value: ConnectFormFields['hosts'];
+ }
+ | {
+ type: 'set-connection-string-field';
+ fieldName: 'isSRV';
+ value: ConnectFormFields['isSRV'];
+ };
+
+export type SetConnectionField = (action: SetConnectionFieldAction) => void;
type Action =
+ | SetConnectionFieldAction
| {
type: 'set-connection-string-error';
errorMessage: string | null;
@@ -42,11 +49,6 @@ type Action =
connectionStringInvalidError: string | null;
connectionStringUrl: ConnectionStringUrl;
fields: ConnectFormFields;
- }
- | {
- type: 'set-connection-string-field';
- value: ConnectFormFields['hosts'];
- fieldName: keyof ConnectFormFields;
};
function connectFormReducer(
@@ -90,14 +92,11 @@ export function parseConnectFormFieldStateFromConnectionUrl(
return {
hosts: {
value: connectionStringUrl.hosts,
- warning: null,
error: null,
},
- // isSRV: {
- // value: connectionStringUrl.isSRV,
- // // warning: null,
- // // error: null
- // },
+ isSRV: {
+ conversionError: null,
+ },
};
}
@@ -169,15 +168,15 @@ export function useConnectForm(initialConnectionOptions: ConnectionOptions): [
fields,
});
},
- setConnectionField: (
- fieldName: keyof ConnectFormFields,
- value: ConnectFormFields['hosts']
- ) => {
- dispatch({
+ setConnectionField: (action: SetConnectionFieldAction) => {
+ dispatch(action);
+ /*
+ {
type: 'set-connection-string-field',
- fieldName,
- value,
- });
+ fieldName: action.fieldName,
+ value: action.value,
+ }
+ */
},
},
];
From 26dc8aa093940b25a1f813c7e2fa88145954fc97 Mon Sep 17 00:00:00 2001
From: Anemy
Date: Mon, 29 Nov 2021 22:57:10 -0500
Subject: [PATCH 13/29] improve schema styles
---
.../advanced-options-tabs/general/schema-input.tsx | 8 +++++++-
packages/connect-form/src/hooks/use-connect-form.ts | 7 -------
2 files changed, 7 insertions(+), 8 deletions(-)
diff --git a/packages/connect-form/src/components/advanced-options-tabs/general/schema-input.tsx b/packages/connect-form/src/components/advanced-options-tabs/general/schema-input.tsx
index 244f2d2b511..65ef5726324 100644
--- a/packages/connect-form/src/components/advanced-options-tabs/general/schema-input.tsx
+++ b/packages/connect-form/src/components/advanced-options-tabs/general/schema-input.tsx
@@ -1,3 +1,4 @@
+import { css } from '@emotion/css';
import React from 'react';
import {
Banner,
@@ -6,6 +7,7 @@ import {
Label,
RadioBox,
RadioBoxGroup,
+ spacing,
} from '@mongodb-js/compass-components';
import ConnectionStringUrl from 'mongodb-connection-string-url';
@@ -17,6 +19,10 @@ enum MONGODB_SCHEMA {
MONGODB_SRV = 'MONGODB_SRV',
}
+const descriptionStyles = css({
+ marginTop: spacing[1],
+});
+
const regularSchemaDescription =
'Standard Connection String Format. The standard format of the MongoDB connection URI is used to connect to a MongoDB deployment: standalone, replica set, or a sharded cluster.';
const srvSchemaDescription =
@@ -144,7 +150,7 @@ function SchemaInput({
mongodb
mongodb+srv
-
+
{isSRV ? srvSchemaDescription : regularSchemaDescription}
{schemaConversionError && (
diff --git a/packages/connect-form/src/hooks/use-connect-form.ts b/packages/connect-form/src/hooks/use-connect-form.ts
index 65367399975..b7b0e5a111b 100644
--- a/packages/connect-form/src/hooks/use-connect-form.ts
+++ b/packages/connect-form/src/hooks/use-connect-form.ts
@@ -170,13 +170,6 @@ export function useConnectForm(initialConnectionOptions: ConnectionOptions): [
},
setConnectionField: (action: SetConnectionFieldAction) => {
dispatch(action);
- /*
- {
- type: 'set-connection-string-field',
- fieldName: action.fieldName,
- value: action.value,
- }
- */
},
},
];
From ab740c4a34b608df2725de086e25e06aee1405df Mon Sep 17 00:00:00 2001
From: Anemy
Date: Mon, 29 Nov 2021 23:13:02 -0500
Subject: [PATCH 14/29] Update connection string comments
---
.../components/connection-string-input.tsx | 53 +++++++++----------
1 file changed, 26 insertions(+), 27 deletions(-)
diff --git a/packages/connect-form/src/components/connection-string-input.tsx b/packages/connect-form/src/components/connection-string-input.tsx
index 7772486e15a..a1fa81979f3 100644
--- a/packages/connect-form/src/components/connection-string-input.tsx
+++ b/packages/connect-form/src/components/connection-string-input.tsx
@@ -164,7 +164,6 @@ function ConnectStringInput({
dispatch,
] = useReducer(reducer, {
// If there is a connection string default it to protected.
- // TODO: Should we default to not protected if there is nothing to hide?
enableEditingConnectionString:
!connectionString || connectionString === 'mongodb://localhost:27017/',
showConfirmEditConnectionStringPrompt: false,
@@ -192,6 +191,31 @@ function ConnectStringInput({
editingConnectionString,
]);
+ function onChangeConnectionString(event: ChangeEvent) {
+ const newConnectionString = event.target.value;
+
+ dispatch({
+ type: 'set-editing-connection-string',
+ editingConnectionString: newConnectionString,
+ });
+
+ try {
+ // Ensure it's parsable connection string.
+ const connectionStringUrl = new ConnectionStringUrl(newConnectionString);
+ setConnectionStringUrl(connectionStringUrl);
+ } catch (error) {
+ // Check if starts with url scheme.
+ if (!connectionStringHasValidScheme(newConnectionString)) {
+ setConnectionStringError(
+ 'Invalid schema, expected connection string to start with `mongodb://` or `mongodb+srv://`'
+ );
+ return;
+ }
+
+ setConnectionStringError((error as Error).message);
+ }
+ }
+
const displayedConnectionString = enableEditingConnectionString
? editingConnectionString
: hidePasswordInConnectionString(editingConnectionString);
@@ -239,32 +263,7 @@ function ConnectStringInput({