Skip to content

Commit 9960602

Browse files
gagiknirinchev
andauthored
fix: hide preset connections from the Remove Connection command VSCODE-674 (#969)
Co-authored-by: Nikola Irinchev <[email protected]>
1 parent fdc135c commit 9960602

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/connectionController.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,15 @@ 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)
747+
.map(([id, connection]) => {
748+
return { id, connection };
749+
})
750+
.filter(
751+
({ connection }) =>
752+
connection.source !== 'globalSettings' &&
753+
connection.source !== 'workspaceSettings'
754+
);
747755

748756
if (connectionIds.length === 0) {
749757
// No active connection(s) to remove.
@@ -753,15 +761,15 @@ export default class ConnectionController {
753761
}
754762

755763
if (connectionIds.length === 1) {
756-
return this.removeMongoDBConnection(connectionIds[0]);
764+
return this.removeMongoDBConnection(connectionIds[0].id);
757765
}
758766

759767
// There is more than 1 possible connection to remove.
760768
// We attach the index of the connection so that we can infer their pick.
761769
const connectionNameToRemove: string | undefined =
762770
await vscode.window.showQuickPick(
763771
connectionIds.map(
764-
(id, index) => `${index + 1}: ${this._connections[id].name}`
772+
({ connection }, index) => `${index + 1}: ${connection.name}`
765773
),
766774
{
767775
placeHolder: 'Choose a connection to remove...',
@@ -775,7 +783,7 @@ export default class ConnectionController {
775783
// We attach the index of the connection so that we can infer their pick.
776784
const connectionIndexToRemove =
777785
Number(connectionNameToRemove.split(':', 1)[0]) - 1;
778-
const connectionIdToRemove = connectionIds[connectionIndexToRemove];
786+
const connectionIdToRemove = connectionIds[connectionIndexToRemove].id;
779787

780788
return this.removeMongoDBConnection(connectionIdToRemove);
781789
}

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: 'globalSettings',
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)