Skip to content

Commit c1811dd

Browse files
committed
fix: hide preset connections on removal
1 parent 089ffd4 commit c1811dd

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

src/connectionController.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,11 @@ export default class ConnectionController {
743743
async onRemoveMongoDBConnection(): Promise<boolean> {
744744
log.info('mdb.removeConnection command called');
745745

746-
const connectionIds = Object.keys(this._connections);
746+
const connectionIds = Object.entries(this._connections).filter(
747+
([, connection]) =>
748+
connection.source !== 'globalSettings' &&
749+
connection.source !== 'workspaceSettings'
750+
);
747751

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

755759
if (connectionIds.length === 1) {
756-
return this.removeMongoDBConnection(connectionIds[0]);
760+
const [id] = connectionIds[0];
761+
return this.removeMongoDBConnection(id);
757762
}
758763

759764
// There is more than 1 possible connection to remove.
760765
// We attach the index of the connection so that we can infer their pick.
761766
const connectionNameToRemove: string | undefined =
762767
await vscode.window.showQuickPick(
763768
connectionIds.map(
764-
(id, index) => `${index + 1}: ${this._connections[id].name}`
769+
([, connection], index) => `${index + 1}: ${connection.name}`
765770
),
766771
{
767772
placeHolder: 'Choose a connection to remove...',
@@ -775,7 +780,7 @@ export default class ConnectionController {
775780
// We attach the index of the connection so that we can infer their pick.
776781
const connectionIndexToRemove =
777782
Number(connectionNameToRemove.split(':', 1)[0]) - 1;
778-
const connectionIdToRemove = connectionIds[connectionIndexToRemove];
783+
const [connectionIdToRemove] = connectionIds[connectionIndexToRemove];
779784

780785
return this.removeMongoDBConnection(connectionIdToRemove);
781786
}

src/test/suite/connectionController.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,53 @@ suite('Connection Controller Test Suite', function () {
135135
expect(successfullyRemovedMongoDBConnection).to.be.false;
136136
});
137137

138+
test('"removeMongoDBConnection()" hides preset connections', async () => {
139+
const connectionBase: Omit<LoadedConnection, 'id' | 'name'> = {
140+
connectionOptions: {
141+
connectionString: 'localhost:3000',
142+
},
143+
storageLocation: StorageLocation.NONE,
144+
secretStorageLocation: SecretStorageLocation.SecretStorage,
145+
};
146+
147+
testConnectionController._connections['1234'] = {
148+
...connectionBase,
149+
name: 'valid 1',
150+
id: '1234',
151+
};
152+
testConnectionController._connections['5678'] = {
153+
...connectionBase,
154+
name: 'valid 2',
155+
id: '5678',
156+
source: 'user',
157+
};
158+
testConnectionController._connections['3333'] = {
159+
...connectionBase,
160+
id: '3333',
161+
name: 'invalid 1',
162+
source: 'workspaceSettings',
163+
};
164+
testConnectionController._connections['3333'] = {
165+
...connectionBase,
166+
id: '3333',
167+
name: 'invalid 2',
168+
source: 'workspaceSettings',
169+
};
170+
171+
const showQuickPickStub = sinon
172+
.stub(vscode.window, 'showQuickPick')
173+
.resolves(undefined);
174+
const successfullyRemovedMongoDBConnection =
175+
await testConnectionController.onRemoveMongoDBConnection();
176+
177+
expect(showErrorMessageStub).not.called;
178+
expect(showQuickPickStub.firstCall.firstArg).deep.equal([
179+
'1: valid 1',
180+
'2: valid 2',
181+
]);
182+
expect(successfullyRemovedMongoDBConnection).to.be.false;
183+
});
184+
138185
test('when adding a new connection it disconnects from the current connection', async () => {
139186
const succesfullyConnected =
140187
await testConnectionController.addNewConnectionStringAndConnect(

0 commit comments

Comments
 (0)