-
Notifications
You must be signed in to change notification settings - Fork 740
Expand file tree
/
Copy pathdirectoryTreeNode.test.ts
More file actions
201 lines (151 loc) · 7.71 KB
/
directoryTreeNode.test.ts
File metadata and controls
201 lines (151 loc) · 7.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { default as assert } from 'assert';
import { DirectoryTreeNode } from '../../../view/treeNodes/directoryTreeNode';
import { TreeNode, TreeNodeParent } from '../../../view/treeNodes/treeNode';
/**
* Minimal mock for a file-like child node that supports checkboxState.
* This is NOT a DirectoryTreeNode so allChildrenViewed() treats it as a leaf file.
*/
class MockFileNode extends TreeNode {
public checkboxState?: { state: vscode.TreeItemCheckboxState; tooltip?: string; accessibilityInformation?: vscode.AccessibilityInformation };
constructor(parent: TreeNodeParent) {
super(parent);
}
getTreeItem(): vscode.TreeItem {
return this;
}
}
function createMockParent(): TreeNodeParent {
return {
refresh: () => { },
reveal: () => Promise.resolve(),
children: undefined,
view: {} as any
} as any;
}
describe('DirectoryTreeNode', function () {
describe('allChildrenViewed', function () {
it('returns true when all file children are checked', function () {
const dirNode = new DirectoryTreeNode(createMockParent(), 'src');
const file1 = new MockFileNode(dirNode);
file1.checkboxState = { state: vscode.TreeItemCheckboxState.Checked };
const file2 = new MockFileNode(dirNode);
file2.checkboxState = { state: vscode.TreeItemCheckboxState.Checked };
(dirNode._children as any[]).push(file1, file2);
assert.strictEqual(dirNode.allChildrenViewed(), true);
});
it('returns false when some file children are unchecked', function () {
const dirNode = new DirectoryTreeNode(createMockParent(), 'src');
const file1 = new MockFileNode(dirNode);
file1.checkboxState = { state: vscode.TreeItemCheckboxState.Checked };
const file2 = new MockFileNode(dirNode);
file2.checkboxState = { state: vscode.TreeItemCheckboxState.Unchecked };
(dirNode._children as any[]).push(file1, file2);
assert.strictEqual(dirNode.allChildrenViewed(), false);
});
it('returns false when a file child has no checkboxState', function () {
const dirNode = new DirectoryTreeNode(createMockParent(), 'src');
const file1 = new MockFileNode(dirNode);
file1.checkboxState = { state: vscode.TreeItemCheckboxState.Checked };
const file2 = new MockFileNode(dirNode);
// file2 has no checkboxState
(dirNode._children as any[]).push(file1, file2);
assert.strictEqual(dirNode.allChildrenViewed(), false);
});
it('returns true when nested directories have all children checked', function () {
const parentDir = new DirectoryTreeNode(createMockParent(), 'src');
const childDir = new DirectoryTreeNode(parentDir, 'utils');
const file1 = new MockFileNode(childDir);
file1.checkboxState = { state: vscode.TreeItemCheckboxState.Checked };
(childDir._children as any[]).push(file1);
parentDir._children.push(childDir);
assert.strictEqual(parentDir.allChildrenViewed(), true);
});
it('returns false when nested directories have unchecked children', function () {
const parentDir = new DirectoryTreeNode(createMockParent(), 'src');
const childDir = new DirectoryTreeNode(parentDir, 'utils');
const file1 = new MockFileNode(childDir);
file1.checkboxState = { state: vscode.TreeItemCheckboxState.Unchecked };
(childDir._children as any[]).push(file1);
parentDir._children.push(childDir);
assert.strictEqual(parentDir.allChildrenViewed(), false);
});
it('returns true when empty directory has no children', function () {
const dirNode = new DirectoryTreeNode(createMockParent(), 'empty');
assert.strictEqual(dirNode.allChildrenViewed(), true);
});
});
describe('updateCheckboxFromChildren', function () {
it('sets checkbox to Checked when all children are viewed', function () {
const dirNode = new DirectoryTreeNode(createMockParent(), 'src');
const file1 = new MockFileNode(dirNode);
file1.checkboxState = { state: vscode.TreeItemCheckboxState.Checked };
(dirNode._children as any[]).push(file1);
dirNode.updateCheckboxFromChildren();
assert.strictEqual(dirNode.checkboxState?.state, vscode.TreeItemCheckboxState.Checked);
});
it('sets checkbox to Unchecked when not all children are viewed', function () {
const dirNode = new DirectoryTreeNode(createMockParent(), 'src');
const file1 = new MockFileNode(dirNode);
file1.checkboxState = { state: vscode.TreeItemCheckboxState.Checked };
const file2 = new MockFileNode(dirNode);
file2.checkboxState = { state: vscode.TreeItemCheckboxState.Unchecked };
(dirNode._children as any[]).push(file1, file2);
dirNode.updateCheckboxFromChildren();
assert.strictEqual(dirNode.checkboxState?.state, vscode.TreeItemCheckboxState.Unchecked);
});
it('propagates state through nested directories', function () {
const parentDir = new DirectoryTreeNode(createMockParent(), 'src');
const childDir = new DirectoryTreeNode(parentDir, 'utils');
const file1 = new MockFileNode(childDir);
file1.checkboxState = { state: vscode.TreeItemCheckboxState.Checked };
(childDir._children as any[]).push(file1);
parentDir._children.push(childDir);
// Update bottom-up (child first, then parent)
childDir.updateCheckboxFromChildren();
assert.strictEqual(childDir.checkboxState?.state, vscode.TreeItemCheckboxState.Checked);
parentDir.updateCheckboxFromChildren();
assert.strictEqual(parentDir.checkboxState?.state, vscode.TreeItemCheckboxState.Checked);
});
it('updates parent to Unchecked when a child is unchecked after being checked', function () {
const parentDir = new DirectoryTreeNode(createMockParent(), 'src');
const file1 = new MockFileNode(parentDir);
file1.checkboxState = { state: vscode.TreeItemCheckboxState.Checked };
const file2 = new MockFileNode(parentDir);
file2.checkboxState = { state: vscode.TreeItemCheckboxState.Checked };
(parentDir._children as any[]).push(file1, file2);
// All checked → parent should be Checked
parentDir.updateCheckboxFromChildren();
assert.strictEqual(parentDir.checkboxState?.state, vscode.TreeItemCheckboxState.Checked);
// Uncheck one file → parent should be Unchecked
file2.checkboxState = { state: vscode.TreeItemCheckboxState.Unchecked };
parentDir.updateCheckboxFromChildren();
assert.strictEqual(parentDir.checkboxState?.state, vscode.TreeItemCheckboxState.Unchecked);
});
it('updates correctly with mixed file and directory children', function () {
const rootDir = new DirectoryTreeNode(createMockParent(), 'root');
const subDir = new DirectoryTreeNode(rootDir, 'sub');
const subFile = new MockFileNode(subDir);
subFile.checkboxState = { state: vscode.TreeItemCheckboxState.Checked };
(subDir._children as any[]).push(subFile);
const rootFile = new MockFileNode(rootDir);
rootFile.checkboxState = { state: vscode.TreeItemCheckboxState.Checked };
(rootDir._children as any[]).push(subDir);
(rootDir._children as any[]).push(rootFile);
// Update bottom-up
subDir.updateCheckboxFromChildren();
rootDir.updateCheckboxFromChildren();
assert.strictEqual(rootDir.checkboxState?.state, vscode.TreeItemCheckboxState.Checked);
// Uncheck the sub-directory file
subFile.checkboxState = { state: vscode.TreeItemCheckboxState.Unchecked };
subDir.updateCheckboxFromChildren();
rootDir.updateCheckboxFromChildren();
assert.strictEqual(subDir.checkboxState?.state, vscode.TreeItemCheckboxState.Unchecked);
assert.strictEqual(rootDir.checkboxState?.state, vscode.TreeItemCheckboxState.Unchecked);
});
});
});