Skip to content

Commit ea188c4

Browse files
authored
VSCODE-127: Sort connection tree items by label before displaying (#128)
1 parent 95af206 commit ea188c4

File tree

2 files changed

+83
-5
lines changed

2 files changed

+83
-5
lines changed

src/explorer/mdbConnectionsTreeItem.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ import { SavedConnection } from '../storage/storageController';
88
const rootLabel = 'Connections';
99
const rootTooltip = 'Your MongoDB connections';
1010

11+
function sortTreeItemsByLabel(treeItems: vscode.TreeItem[]): vscode.TreeItem[] {
12+
return treeItems.sort(
13+
(
14+
a: vscode.TreeItem,
15+
b: vscode.TreeItem
16+
) => (a.label || '').localeCompare(b.label || '')
17+
);
18+
}
19+
1120
export default class MDBConnectionsTreeItem extends vscode.TreeItem
1221
implements TreeItemParent, vscode.TreeDataProvider<vscode.TreeItem> {
1322
contextValue = 'mdbConnectionsTreeItem';
@@ -102,13 +111,17 @@ export default class MDBConnectionsTreeItem extends vscode.TreeItem
102111
notYetEstablishConnectionTreeItem.description = 'connecting...';
103112

104113
// When we're connecting to a new connection we add simple node showing the connecting status.
105-
return Promise.resolve([
106-
...Object.values(this._connectionTreeItems),
107-
notYetEstablishConnectionTreeItem
108-
]);
114+
return Promise.resolve(
115+
sortTreeItemsByLabel([
116+
...Object.values(this._connectionTreeItems),
117+
notYetEstablishConnectionTreeItem
118+
])
119+
);
109120
}
110121

111-
return Promise.resolve(Object.values(this._connectionTreeItems));
122+
return Promise.resolve(
123+
sortTreeItemsByLabel(Object.values(this._connectionTreeItems))
124+
);
112125
}
113126

114127
connectionsDidChange(): void {

src/test/suite/explorer/explorerController.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,71 @@ suite('Explorer Controller Test Suite', function () {
235235
}
236236
});
237237

238+
test('shows connection names sorted alphabetically in the tree', async () => {
239+
const testConnectionController =
240+
mdbTestExtension.testExtensionController._connectionController;
241+
const testExplorerController =
242+
mdbTestExtension.testExtensionController._explorerController;
243+
const treeController = testExplorerController.getTreeController();
244+
245+
try {
246+
await testConnectionController.addNewConnectionStringAndConnect(
247+
TEST_DATABASE_URI
248+
);
249+
} catch (error) {
250+
assert(false);
251+
}
252+
253+
const connectionId =
254+
testConnectionController.getActiveConnectionId() || '';
255+
256+
testConnectionController._connections.aaa = {
257+
connectionModel: testConnectionController._connections[connectionId].connectionModel,
258+
driverUrl: '',
259+
name: 'aaa',
260+
id: 'aaa',
261+
storageLocation: StorageScope.WORKSPACE
262+
};
263+
264+
testConnectionController._connections.zzz = {
265+
connectionModel: testConnectionController._connections[connectionId].connectionModel,
266+
driverUrl: '',
267+
name: 'zzz',
268+
id: 'zzz',
269+
storageLocation: StorageScope.WORKSPACE
270+
};
271+
272+
try {
273+
const treeControllerChildren = await treeController.getChildren();
274+
const connectionsItems = await treeControllerChildren[0].getChildren();
275+
276+
assert(
277+
connectionsItems.length === 3,
278+
`Expected there be 3 connection tree item, found ${connectionsItems.length}`
279+
);
280+
assert(
281+
connectionsItems[0].label === 'aaa',
282+
`First connection tree item should have label "aaa" found ${connectionsItems[0].label}`
283+
);
284+
assert(
285+
connectionsItems[2].label === 'zzz',
286+
`First connection tree item should have label "zzz" found ${connectionsItems[0].label}`
287+
);
288+
289+
testConnectionController._connections.zzz.name = '111';
290+
291+
const afterAdditionConnectionsItems = await treeControllerChildren[0].getChildren();
292+
assert(
293+
afterAdditionConnectionsItems[0].label === '111',
294+
`First connection tree item should have label "111" found ${afterAdditionConnectionsItems[0].label}`
295+
);
296+
297+
testExplorerController.deactivate();
298+
} catch (error) {
299+
assert(false, error);
300+
}
301+
});
302+
238303
test('shows the databases of connected connection in tree', async () => {
239304
const testConnectionController =
240305
mdbTestExtension.testExtensionController._connectionController;

0 commit comments

Comments
 (0)