Skip to content

Commit f59f093

Browse files
committed
Add unique ID generation for TreeNodeInfo and corresponding tests (#19481)
* Add unique ID generation for TreeNodeInfo and corresponding tests * Remove unused variable declaration for TreeNodeInfo in tests
1 parent 57d0793 commit f59f093

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/objectExplorer/nodes/treeNodeInfo.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ObjectExplorerUtils } from "../objectExplorerUtils";
1010
import * as Constants from "../../constants/constants";
1111
import { ITreeNodeInfo, ObjectMetadata } from "vscode-mssql";
1212
import { IConnectionProfile } from "../../models/interfaces";
13+
import { generateGuid } from "../../models/utils";
1314

1415
export class TreeNodeInfo extends vscode.TreeItem implements ITreeNodeInfo {
1516
private _nodePath: string;
@@ -68,9 +69,9 @@ export class TreeNodeInfo extends vscode.TreeItem implements ITreeNodeInfo {
6869
this.id = this.generateId();
6970
}
7071

71-
// Gernating a unique ID for the node
72+
// Generating a unique ID for the node
7273
protected generateId(): string {
73-
return `${this._connectionProfile?.id}-${this._nodePath}-${Date.now()}`;
74+
return `${this._connectionProfile?.id}-${this._nodePath}-${generateGuid()}`;
7475
}
7576

7677
public static fromNodeInfo(

test/unit/treeNodeInfo.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { expect } from "chai";
7+
import { TreeNodeInfo } from "../../src/objectExplorer/nodes/treeNodeInfo";
8+
9+
suite("TreeNodeInfo", () => {
10+
test("When creating multiple TreeNodeInfo in quick succession, the nodePath should be unique", () => {
11+
const node1 = new TreeNodeInfo(
12+
"node_label",
13+
undefined,
14+
undefined,
15+
"node_path",
16+
undefined,
17+
undefined,
18+
"session_id",
19+
undefined,
20+
undefined,
21+
undefined,
22+
undefined,
23+
);
24+
25+
const node2 = new TreeNodeInfo(
26+
"node_label",
27+
undefined,
28+
undefined,
29+
"node_path",
30+
undefined,
31+
undefined,
32+
"session_id",
33+
undefined,
34+
undefined,
35+
undefined,
36+
undefined,
37+
);
38+
expect(node1.id).to.not.equal(node2.id, "Node IDs should be unique");
39+
});
40+
});

0 commit comments

Comments
 (0)