Skip to content

Commit d777d8c

Browse files
authored
VSCODE-73: Add connect with connection string to connect form (#61)
1 parent b5ed64c commit d777d8c

File tree

7 files changed

+176
-73
lines changed

7 files changed

+176
-73
lines changed

src/test/suite/views/webviewController.test.ts

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ suite('Connect Form View Test Suite', () => {
104104
assert(testConnectionController.isCurrentlyConnected());
105105
assert(
106106
testConnectionController.getActiveConnectionName() ===
107-
'localhost:27018'
107+
'localhost:27018'
108108
);
109109
assert(
110110
testConnectionController.getActiveConnectionModel()?.port === 27018
@@ -221,7 +221,6 @@ suite('Connect Form View Test Suite', () => {
221221
});
222222
});
223223

224-
225224
test('web view sends an unsuccessful connect result on an unsuccessful connection', (done) => {
226225
const testExtensionContext = new TestExtensionContext();
227226
const testStorageController = new StorageController(testExtensionContext);
@@ -380,4 +379,53 @@ suite('Connect Form View Test Suite', () => {
380379
action: 'file_action'
381380
});
382381
});
382+
383+
test('web view runs the "connectWithURI" command when open connection string input is recieved', (done) => {
384+
const testExtensionContext = new TestExtensionContext();
385+
const testStorageController = new StorageController(testExtensionContext);
386+
387+
const testConnectionController = new ConnectionController(
388+
new StatusView(testExtensionContext),
389+
testStorageController
390+
);
391+
392+
let messageRecieved;
393+
const fakeWebview = {
394+
html: '',
395+
onDidReceiveMessage: (callback): void => {
396+
messageRecieved = callback;
397+
}
398+
};
399+
400+
const fakeVSCodeExecuteCommand = sinon.fake();
401+
sinon.replace(vscode.commands, 'executeCommand', fakeVSCodeExecuteCommand);
402+
403+
const fakeVSCodeCreateWebviewPanel = sinon.fake.returns({
404+
webview: fakeWebview
405+
});
406+
sinon.replace(
407+
vscode.window,
408+
'createWebviewPanel',
409+
fakeVSCodeCreateWebviewPanel
410+
);
411+
412+
const testWebviewController = new WebviewController(
413+
testConnectionController
414+
);
415+
416+
testWebviewController.showConnectForm(
417+
mdbTestExtension.testExtensionContext
418+
);
419+
420+
messageRecieved({
421+
command: MESSAGE_TYPES.OPEN_CONNECTION_STRING_INPUT
422+
});
423+
424+
setTimeout(() => {
425+
assert(fakeVSCodeExecuteCommand.called);
426+
assert(fakeVSCodeExecuteCommand.firstArg === 'mdb.connectWithURI');
427+
428+
done();
429+
}, 50);
430+
});
383431
});

src/views/webview-app/components/connect-form/connection-form.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import classnames from 'classnames';
22
import * as React from 'react';
33
import { connect } from 'react-redux';
44

5-
import { ActionTypes, ConnectionFormChangedAction } from '../../store/actions';
5+
import {
6+
ActionTypes,
7+
ConnectionFormChangedAction,
8+
OpenConnectionStringInputAction
9+
} from '../../store/actions';
610
import FormGroup from './form-group';
711
import HostInput from './host/host-input';
812
import PortInput from './host/port-input';
@@ -31,13 +35,18 @@ type stateProps = {
3135

3236
type dispatchProps = {
3337
onConnectionFormChanged: () => void;
38+
onOpenConnectionStringInput: () => void;
3439
};
3540

3641
type props = stateProps & dispatchProps;
3742

3843
class ConnectionForm extends React.Component<props> {
3944
static displayName = 'ConnectionForm';
4045

46+
onConnectWithConnectionStringClicked = (): void => {
47+
this.props.onOpenConnectionStringInput();
48+
};
49+
4150
/**
4251
* Renders a port input.
4352
*
@@ -136,6 +145,12 @@ class ConnectionForm extends React.Component<props> {
136145
className={classnames(styles['connect-form'])}
137146
>
138147
<h1>Connect to MongoDB</h1>
148+
<div>
149+
Enter your connection details below, or{' '}
150+
<a href="#" onClick={this.onConnectWithConnectionStringClicked}>
151+
connect with a connection string
152+
</a>
153+
</div>
139154
<div className={classnames(styles.fields)}>
140155
{this.renderHostnameArea()}
141156
{this.renderConnectionOptionsArea()}
@@ -170,6 +185,9 @@ const mapDispatchToProps: dispatchProps = {
170185
// Resets URL validation if form was changed.
171186
onConnectionFormChanged: (): ConnectionFormChangedAction => ({
172187
type: ActionTypes.CONNECTION_FORM_CHANGED
188+
}),
189+
onOpenConnectionStringInput: (): OpenConnectionStringInputAction => ({
190+
type: ActionTypes.OPEN_CONNECTION_STRING_INPUT
173191
})
174192
};
175193

src/views/webview-app/extension-app-message-constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FilePickerActionTypes } from './store/actions';
33
export enum MESSAGE_TYPES {
44
CONNECT = 'CONNECT',
55
CONNECT_RESULT = 'CONNECT_RESULT',
6+
OPEN_CONNECTION_STRING_INPUT = 'OPEN_CONNECTION_STRING_INPUT',
67
OPEN_FILE_PICKER = 'OPEN_FILE_PICKER',
78
FILE_PICKER_RESULTS = 'FILE_PICKER_RESULTS'
89
}
@@ -23,6 +24,10 @@ export interface ConnectResultsMessage extends BasicWebviewMessage {
2324
connectionMessage: string;
2425
}
2526

27+
export interface OpenConnectionStringInputMessage extends BasicWebviewMessage {
28+
command: MESSAGE_TYPES.OPEN_CONNECTION_STRING_INPUT;
29+
}
30+
2631
// Note: In the app this is tightly coupled with 'externals.ts'.
2732
export interface OpenFilePickerMessage extends BasicWebviewMessage {
2833
command: MESSAGE_TYPES.OPEN_FILE_PICKER;

src/views/webview-app/externals.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ interface ConnectMessage extends BasicWebviewMessage {
1313
connectionModel: object;
1414
}
1515

16+
interface OpenConnectionStringInputMessage extends BasicWebviewMessage {
17+
command: 'OPEN_CONNECTION_STRING_INPUT';
18+
}
19+
1620
interface FilePickerMessage {
1721
command: 'OPEN_FILE_PICKER';
1822
action: string;
1923
multi: boolean;
2024
}
2125

22-
type WebviewMessage = ConnectMessage | FilePickerMessage;
26+
type WebviewMessage =
27+
| ConnectMessage
28+
| FilePickerMessage
29+
| OpenConnectionStringInputMessage;
2330

2431
interface VSCodeApi {
2532
postMessage: (message: WebviewMessage) => void;

src/views/webview-app/store/actions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export enum ActionTypes {
1818
ON_CHANGE_SSL_CA = 'ON_CHANGE_SSL_CA',
1919
ON_CHANGE_SSL_CERT = 'ON_CHANGE_SSL_CERT',
2020
ON_CHANGE_SSL_KEY = 'ON_CHANGE_SSL_KEY',
21+
OPEN_CONNECTION_STRING_INPUT = 'OPEN_CONNECTION_STRING_INPUT',
2122
PASSWORD_CHANGED = 'PASSWORD_CHANGED',
2223
PORT_CHANGED = 'PORT_CHANGED',
2324
READ_PREFERENCE_CHANGED = 'READ_PREFERENCE_CHANGED',
@@ -125,6 +126,10 @@ export interface OnChangeSSLKeyAction extends BaseAction {
125126
type: ActionTypes.ON_CHANGE_SSL_KEY;
126127
}
127128

129+
export interface OpenConnectionStringInputAction extends BaseAction {
130+
type: ActionTypes.OPEN_CONNECTION_STRING_INPUT;
131+
}
132+
128133
export interface PasswordChangedAction extends BaseAction {
129134
type: ActionTypes.PASSWORD_CHANGED;
130135
mongodbPassword: string;
@@ -236,6 +241,7 @@ export type Actions =
236241
| OnChangeSSLCAAction
237242
| OnChangeSSLCertAction
238243
| OnChangeSSLKeyAction
244+
| OpenConnectionStringInputAction
239245
| PasswordChangedAction
240246
| PortChangedAction
241247
| ReadPreferenceChangedAction

0 commit comments

Comments
 (0)