Skip to content

Commit

Permalink
fix: hide preset connections on removal
Browse files Browse the repository at this point in the history
  • Loading branch information
gagik committed Mar 3, 2025
1 parent 089ffd4 commit c1811dd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/connectionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,11 @@ export default class ConnectionController {
async onRemoveMongoDBConnection(): Promise<boolean> {
log.info('mdb.removeConnection command called');

const connectionIds = Object.keys(this._connections);
const connectionIds = Object.entries(this._connections).filter(
([, connection]) =>
connection.source !== 'globalSettings' &&
connection.source !== 'workspaceSettings'
);

if (connectionIds.length === 0) {
// No active connection(s) to remove.
Expand All @@ -753,15 +757,16 @@ export default class ConnectionController {
}

if (connectionIds.length === 1) {
return this.removeMongoDBConnection(connectionIds[0]);
const [id] = connectionIds[0];
return this.removeMongoDBConnection(id);
}

// There is more than 1 possible connection to remove.
// We attach the index of the connection so that we can infer their pick.
const connectionNameToRemove: string | undefined =
await vscode.window.showQuickPick(
connectionIds.map(
(id, index) => `${index + 1}: ${this._connections[id].name}`
([, connection], index) => `${index + 1}: ${connection.name}`
),
{
placeHolder: 'Choose a connection to remove...',
Expand All @@ -775,7 +780,7 @@ export default class ConnectionController {
// We attach the index of the connection so that we can infer their pick.
const connectionIndexToRemove =
Number(connectionNameToRemove.split(':', 1)[0]) - 1;
const connectionIdToRemove = connectionIds[connectionIndexToRemove];
const [connectionIdToRemove] = connectionIds[connectionIndexToRemove];

return this.removeMongoDBConnection(connectionIdToRemove);
}
Expand Down
47 changes: 47 additions & 0 deletions src/test/suite/connectionController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,53 @@ suite('Connection Controller Test Suite', function () {
expect(successfullyRemovedMongoDBConnection).to.be.false;
});

test('"removeMongoDBConnection()" hides preset connections', async () => {
const connectionBase: Omit<LoadedConnection, 'id' | 'name'> = {
connectionOptions: {
connectionString: 'localhost:3000',
},
storageLocation: StorageLocation.NONE,
secretStorageLocation: SecretStorageLocation.SecretStorage,
};

testConnectionController._connections['1234'] = {
...connectionBase,
name: 'valid 1',
id: '1234',
};
testConnectionController._connections['5678'] = {
...connectionBase,
name: 'valid 2',
id: '5678',
source: 'user',
};
testConnectionController._connections['3333'] = {
...connectionBase,
id: '3333',
name: 'invalid 1',
source: 'workspaceSettings',
};
testConnectionController._connections['3333'] = {
...connectionBase,
id: '3333',
name: 'invalid 2',
source: 'workspaceSettings',
};

const showQuickPickStub = sinon
.stub(vscode.window, 'showQuickPick')
.resolves(undefined);
const successfullyRemovedMongoDBConnection =
await testConnectionController.onRemoveMongoDBConnection();

expect(showErrorMessageStub).not.called;
expect(showQuickPickStub.firstCall.firstArg).deep.equal([
'1: valid 1',
'2: valid 2',
]);
expect(successfullyRemovedMongoDBConnection).to.be.false;
});

test('when adding a new connection it disconnects from the current connection', async () => {
const succesfullyConnected =
await testConnectionController.addNewConnectionStringAndConnect(
Expand Down

0 comments on commit c1811dd

Please sign in to comment.