Skip to content

Commit bf39230

Browse files
committed
refactor: rename to presetConnections, update test strict equality, use switch
1 parent 0850375 commit bf39230

File tree

7 files changed

+47
-38
lines changed

7 files changed

+47
-38
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
1919
"typescript.tsc.autoDetect": "off",
20-
"mdb.presetSavedConnections": [
20+
"mdb.presetConnections": [
2121
{
2222
"name": "Preset Database",
2323
"connectionString": "mongodb://localhost:27017"

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@
550550
},
551551
{
552552
"command": "mdb.editPresetConnections",
553-
"when": "view == mongoDBConnectionExplorer && (viewItem == connectedPresetConnectionTreeItem)",
553+
"when": "view == mongoDBConnectionExplorer && viewItem == connectedPresetConnectionTreeItem",
554554
"group": "3@2"
555555
},
556556
{
@@ -590,7 +590,7 @@
590590
},
591591
{
592592
"command": "mdb.editPresetConnections",
593-
"when": "view == mongoDBConnectionExplorer && (viewItem == disconnectedPresetConnectionTreeItem)",
593+
"when": "view == mongoDBConnectionExplorer && viewItem == disconnectedPresetConnectionTreeItem",
594594
"group": "2@2"
595595
},
596596
{
@@ -1193,7 +1193,7 @@
11931193
"default": "",
11941194
"description": "Specify a shell command that is run to start the browser for authenticating with the OIDC identity provider for the server connection. Leave this empty for default browser."
11951195
},
1196-
"mdb.presetSavedConnections": {
1196+
"mdb.presetConnections": {
11971197
"scope": "window",
11981198
"type": "array",
11991199
"description": "Defines preset saved connections. Can be used to share connection configurations in a workspace or global scope. Do not store sensitive credentials here.",

src/connectionController.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ export default class ConnectionController {
177177
if (!source) {
178178
const mdbConfiguration = vscode.workspace.getConfiguration('mdb');
179179

180-
const presetConnections = mdbConfiguration?.inspect(
181-
'presetSavedConnections'
182-
);
180+
const presetConnections = mdbConfiguration?.inspect('presetConnections');
183181

184182
if (presetConnections?.workspaceValue) {
185183
source = 'workspaceSettings';
@@ -189,20 +187,28 @@ export default class ConnectionController {
189187
// If no preset settings exist in workspace and global scope,
190188
// set a default one inside the workspace and open it.
191189
source = 'workspaceSettings';
192-
await mdbConfiguration.update('presetSavedConnections', [
190+
await mdbConfiguration.update('presetConnections', [
193191
{
194192
name: 'Preset Database',
195193
connectionString: 'mongodb://localhost:27017',
196194
},
197195
]);
198196
}
199197
}
200-
if (originTreeItem?.source === 'globalSettings') {
201-
await vscode.commands.executeCommand('workbench.action.openSettingsJson');
202-
} else {
203-
await vscode.commands.executeCommand(
204-
'workbench.action.openWorkspaceSettingsFile'
205-
);
198+
switch (source) {
199+
case 'globalSettings':
200+
await vscode.commands.executeCommand(
201+
'workbench.action.openSettingsJson'
202+
);
203+
break;
204+
case 'workspaceSettings':
205+
case 'user':
206+
await vscode.commands.executeCommand(
207+
'workbench.action.openWorkspaceSettingsFile'
208+
);
209+
break;
210+
default:
211+
throw new Error('Unknown preset connection source');
206212
}
207213
}
208214

src/explorer/connectionTreeItem.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ export default class ConnectionTreeItem
209209
return Object.values(this._childrenCache);
210210
}
211211

212-
private async _buildChildrenCacheForDatabases(dataService: DataService) {
212+
private async _buildChildrenCacheForDatabases(
213+
dataService: DataService
214+
): Promise<Record<string, DatabaseTreeItem>> {
213215
const databases = await this.listDatabases();
214216
databases.sort((a: string, b: string) => a.localeCompare(b));
215217

@@ -231,7 +233,9 @@ export default class ConnectionTreeItem
231233
return newChildrenCache;
232234
}
233235

234-
private async _buildChildrenCacheForStreams(dataService: DataService) {
236+
private async _buildChildrenCacheForStreams(
237+
dataService: DataService
238+
): Promise<Record<string, StreamProcessorTreeItem>> {
235239
const processors = await this.listStreamProcessors();
236240
processors.sort((a, b) => a.name.localeCompare(b.name));
237241

src/mdbExtensionController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export default class MDBExtensionController implements vscode.Disposable {
162162

163163
subscribeToConfigurationChanges(): void {
164164
const subscription = vscode.workspace.onDidChangeConfiguration((event) => {
165-
if (event.affectsConfiguration('mdb.presetSavedConnections')) {
165+
if (event.affectsConfiguration('mdb.presetConnections')) {
166166
void this._connectionController.loadSavedConnections();
167167
}
168168
});

src/storage/connectionStorage.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,28 +180,27 @@ export class ConnectionStorage {
180180
);
181181
}
182182

183-
_loadPresetSavedConnections(): LoadedConnection[] {
183+
_loadPresetConnections(): LoadedConnection[] {
184184
const configuration = vscode.workspace.getConfiguration('mdb');
185-
const presetSavedConnectionsInfo = configuration.inspect<
186-
PresetSavedConnection[]
187-
>('presetSavedConnections');
185+
const presetConnectionsInfo =
186+
configuration.inspect<PresetSavedConnection[]>('presetConnections');
188187

189-
if (!presetSavedConnectionsInfo) {
188+
if (!presetConnectionsInfo) {
190189
return [];
191190
}
192191

193-
const combinedPresetSavedConnections: PresetSavedConnectionWithSource[] = [
194-
...(presetSavedConnectionsInfo?.globalValue ?? []).map((preset) => ({
192+
const combinedPresetConnections: PresetSavedConnectionWithSource[] = [
193+
...(presetConnectionsInfo?.globalValue ?? []).map((preset) => ({
195194
...preset,
196195
source: 'globalSettings' as const,
197196
})),
198-
...(presetSavedConnectionsInfo?.workspaceValue ?? []).map((preset) => ({
197+
...(presetConnectionsInfo?.workspaceValue ?? []).map((preset) => ({
199198
...preset,
200199
source: 'workspaceSettings' as const,
201200
})),
202201
];
203202

204-
return combinedPresetSavedConnections.map((presetConnection) => ({
203+
return combinedPresetConnections.map((presetConnection) => ({
205204
id: uuidv4(),
206205
name: presetConnection.name,
207206
connectionOptions: {
@@ -250,9 +249,9 @@ export class ConnectionStorage {
250249
})
251250
);
252251

253-
const presetSavedConnections = this._loadPresetSavedConnections();
252+
const presetConnections = this._loadPresetConnections();
254253

255-
return [...loadedConnections, ...presetSavedConnections];
254+
return [...loadedConnections, ...presetConnections];
256255
}
257256

258257
async removeConnection(connectionId: string): Promise<void> {

src/test/suite/storage/connectionStorage.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,11 @@ suite('Connection Storage Test Suite', function () {
349349
],
350350
vscode.WorkspaceConfiguration
351351
>;
352-
let inspectPresetSavedConnectionsStub: sinon.SinonStub;
352+
let inspectPresetConnectionsStub: sinon.SinonStub;
353353

354354
beforeEach(() => {
355355
testSandbox.restore();
356-
inspectPresetSavedConnectionsStub = testSandbox.stub();
356+
inspectPresetConnectionsStub = testSandbox.stub();
357357
});
358358

359359
test('loads the preset connections', async () => {
@@ -362,12 +362,12 @@ suite('Connection Storage Test Suite', function () {
362362
'getConfiguration'
363363
);
364364
getConfigurationStub.returns({
365-
inspect: inspectPresetSavedConnectionsStub,
365+
inspect: inspectPresetConnectionsStub,
366366
get: () => undefined,
367367
} as any);
368368

369-
inspectPresetSavedConnectionsStub
370-
.withArgs('presetSavedConnections')
369+
inspectPresetConnectionsStub
370+
.withArgs('presetConnections')
371371
.returns(presetConnections);
372372

373373
const loadedConnections = await testConnectionStorage.loadConnections();
@@ -391,7 +391,7 @@ suite('Connection Storage Test Suite', function () {
391391
const connection = loadedConnections[i];
392392
const expected = expectedConnectionValues[i];
393393
expect(connection.name).equals(expected.name);
394-
expect(connection.connectionOptions.connectionString).contains(
394+
expect(connection.connectionOptions.connectionString).equals(
395395
expected.connectionString
396396
);
397397
expect(connection.source).equals(expected.source);
@@ -407,12 +407,12 @@ suite('Connection Storage Test Suite', function () {
407407
'getConfiguration'
408408
);
409409
getConfigurationStub.returns({
410-
inspect: inspectPresetSavedConnectionsStub,
410+
inspect: inspectPresetConnectionsStub,
411411
get: () => undefined,
412412
} as any);
413413

414-
inspectPresetSavedConnectionsStub
415-
.withArgs('presetSavedConnections')
414+
inspectPresetConnectionsStub
415+
.withArgs('presetConnections')
416416
.returns(presetConnections);
417417

418418
const loadedConnections = await testConnectionStorage.loadConnections();
@@ -442,7 +442,7 @@ suite('Connection Storage Test Suite', function () {
442442
const connection = loadedConnections[i];
443443
const expected = expectedConnectionValues[i];
444444
expect(connection.name).equals(expected.name);
445-
expect(connection.connectionOptions.connectionString).contains(
445+
expect(connection.connectionOptions.connectionString).equals(
446446
expected.connectionString
447447
);
448448
expect(connection.source).equals(expected.source);

0 commit comments

Comments
 (0)