Skip to content

Commit 54b8246

Browse files
Add test cases for tree context menu functionality
Added comprehensive unit tests for various tree context menu operations, including checking/unchecking nodes and selective operations at different levels. - Imported relevant functions from `tree-utils` for testing. - Defined sample tree structure for consistent test setup. - Implemented the following test cases: - `checkAllChildren`: Verify all nodes are marked as checked. - `checkAllFolders`: Verify only folder nodes are checked. - `uncheckAllChildren`: Verify all nodes are marked as unchecked. - `checkWithoutChildren`: Test node is checked without affecting its children. - `checkOnlyFoldersAtLevel`: Test only folder nodes at a specific level are checked. - `checkOnlyFilesAtLevel`: Test only file nodes at a specific level are checked. - `checkAllChildrenAtLevel`: Verify all nodes at a specific level are checked.
1 parent 81ad8d8 commit 54b8246

File tree

4 files changed

+389
-1
lines changed

4 files changed

+389
-1
lines changed

src/test/extension.test.ts

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ import * as path from 'path';
55
import { buildTreeNode } from '../extension';
66
import * as fs from 'fs';
77
import * as os from "os";
8+
// Import tree-utils functions for testing context menu options
9+
import {
10+
checkAllChildren,
11+
checkAllFolders,
12+
uncheckAllChildren,
13+
checkWithoutChildren,
14+
checkOnlyFoldersAtLevel,
15+
checkOnlyFilesAtLevel,
16+
checkAllChildrenAtLevel
17+
} from './tree-utils';
818

919
suite('JetTreeMark-vscode Test Suite', () => {
1020
test('Extension is present in the registry', () => {
@@ -63,4 +73,150 @@ suite('JetTreeMark-vscode Test Suite', () => {
6373
assert.ok(bNode && bNode.type === 'file', 'b.txt should exist in subdir');
6474
});
6575
});
66-
});
76+
// Test suite for context menu options
77+
suite('Context Menu Options Tests', () => {
78+
// Sample tree structure for testing
79+
let sampleTree: any[];
80+
81+
setup(() => {
82+
// Create a sample tree structure before each test
83+
sampleTree = [
84+
{
85+
id: 'root',
86+
name: 'root',
87+
type: 'folder',
88+
checked: false,
89+
indeterminate: false,
90+
children: [
91+
{
92+
id: 'folder1',
93+
name: 'folder1',
94+
type: 'folder',
95+
checked: false,
96+
indeterminate: false,
97+
children: [
98+
{
99+
id: 'file1',
100+
name: 'file1.txt',
101+
type: 'file',
102+
checked: false
103+
},
104+
{
105+
id: 'file2',
106+
name: 'file2.txt',
107+
type: 'file',
108+
checked: false
109+
}
110+
]
111+
},
112+
{
113+
id: 'folder2',
114+
name: 'folder2',
115+
type: 'folder',
116+
checked: false,
117+
indeterminate: false,
118+
children: [
119+
{
120+
id: 'file3',
121+
name: 'file3.txt',
122+
type: 'file',
123+
checked: false
124+
}
125+
]
126+
},
127+
{
128+
id: 'file4',
129+
name: 'file4.txt',
130+
type: 'file',
131+
checked: false
132+
}
133+
]
134+
}
135+
];
136+
});
137+
138+
test('checkAllChildren sets all nodes to checked', () => {
139+
const result = checkAllChildren(sampleTree);
140+
141+
// Check that all nodes are checked
142+
assert.strictEqual(result[0].checked, true, 'Root node should be checked');
143+
assert.strictEqual(result[0].children![0].checked, true, 'folder1 should be checked');
144+
assert.strictEqual(result[0].children![0].children![0].checked, true, 'file1 should be checked');
145+
assert.strictEqual(result[0].children![0].children![1].checked, true, 'file2 should be checked');
146+
assert.strictEqual(result[0].children![1].checked, true, 'folder2 should be checked');
147+
assert.strictEqual(result[0].children![1].children![0].checked, true, 'file3 should be checked');
148+
assert.strictEqual(result[0].children![2].checked, true, 'file4 should be checked');
149+
});
150+
151+
test('checkAllFolders sets only folder nodes to checked', () => {
152+
const result = checkAllFolders(sampleTree);
153+
154+
// Check that only folder nodes are checked
155+
assert.strictEqual(result[0].checked, true, 'Root node should be checked');
156+
assert.strictEqual(result[0].children![0].checked, true, 'folder1 should be checked');
157+
assert.strictEqual(result[0].children![0].children![0].checked, false, 'file1 should not be checked');
158+
assert.strictEqual(result[0].children![0].children![1].checked, false, 'file2 should not be checked');
159+
assert.strictEqual(result[0].children![1].checked, true, 'folder2 should be checked');
160+
assert.strictEqual(result[0].children![1].children![0].checked, false, 'file3 should not be checked');
161+
assert.strictEqual(result[0].children![2].checked, false, 'file4 should not be checked');
162+
});
163+
164+
test('uncheckAllChildren sets all nodes to unchecked', () => {
165+
// First check all nodes
166+
sampleTree = checkAllChildren(sampleTree);
167+
168+
// Then uncheck all nodes
169+
const result = uncheckAllChildren(sampleTree);
170+
171+
// Check that all nodes are unchecked
172+
assert.strictEqual(result[0].checked, false, 'Root node should be unchecked');
173+
assert.strictEqual(result[0].children![0].checked, false, 'folder1 should be unchecked');
174+
assert.strictEqual(result[0].children![0].children![0].checked, false, 'file1 should be unchecked');
175+
assert.strictEqual(result[0].children![0].children![1].checked, false, 'file2 should be unchecked');
176+
assert.strictEqual(result[0].children![1].checked, false, 'folder2 should be unchecked');
177+
assert.strictEqual(result[0].children![1].children![0].checked, false, 'file3 should be unchecked');
178+
assert.strictEqual(result[0].children![2].checked, false, 'file4 should be unchecked');
179+
});
180+
181+
test('checkWithoutChildren checks a node without affecting its children', () => {
182+
// Apply checkWithoutChildren to folder1
183+
const folder1 = sampleTree[0].children![0];
184+
const result = checkWithoutChildren(folder1);
185+
186+
// Check that folder1 is checked but its children are not
187+
assert.strictEqual(result.checked, true, 'folder1 should be checked');
188+
assert.strictEqual(result.children![0].checked, false, 'file1 should not be checked');
189+
assert.strictEqual(result.children![1].checked, false, 'file2 should not be checked');
190+
});
191+
192+
test('checkOnlyFoldersAtLevel checks only folder nodes at a specific level', () => {
193+
// Apply checkOnlyFoldersAtLevel to root's children
194+
const result = checkOnlyFoldersAtLevel(sampleTree[0].children!);
195+
196+
// Check that only folder nodes at this level are checked
197+
assert.strictEqual(result[0].checked, true, 'folder1 should be checked');
198+
assert.strictEqual(result[1].checked, true, 'folder2 should be checked');
199+
assert.strictEqual(result[2].checked, false, 'file4 should not be checked');
200+
});
201+
202+
test('checkOnlyFilesAtLevel checks only file nodes at a specific level', () => {
203+
// Apply checkOnlyFilesAtLevel to root's children
204+
const result = checkOnlyFilesAtLevel(sampleTree[0].children!);
205+
206+
// Check that only file nodes at this level are checked
207+
assert.strictEqual(result[0].checked, false, 'folder1 should not be checked');
208+
assert.strictEqual(result[1].checked, false, 'folder2 should not be checked');
209+
assert.strictEqual(result[2].checked, true, 'file4 should be checked');
210+
});
211+
212+
test('checkAllChildrenAtLevel checks all nodes at a specific level', () => {
213+
// Apply checkAllChildrenAtLevel to root's children
214+
const result = checkAllChildrenAtLevel(sampleTree[0].children!);
215+
216+
// Check that all nodes at this level are checked
217+
assert.strictEqual(result[0].checked, true, 'folder1 should be checked');
218+
assert.strictEqual(result[1].checked, true, 'folder2 should be checked');
219+
assert.strictEqual(result[2].checked, true, 'file4 should be checked');
220+
});
221+
});
222+
});

src/test/tree-utils.ts

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copy of the tree-utils.ts functions for testing purposes
2+
// This file contains the same functions as webview-ui/src/lib/tree-utils.ts
3+
// but adapted to work with the extension test environment
4+
5+
export interface TreeNodeType {
6+
id: string
7+
name: string
8+
type: "file" | "folder"
9+
checked: boolean
10+
indeterminate?: boolean
11+
children?: TreeNodeType[]
12+
}
13+
14+
// Function to add checked property to all nodes in the tree
15+
export function processTreeData(data: TreeNodeType[]) {
16+
return data.map((node) => {
17+
const newNode = { ...node, checked: true };
18+
if (node.children && node.children.length > 0) {
19+
newNode.children = processTreeData(node.children);
20+
}
21+
return newNode;
22+
});
23+
}
24+
25+
// Function to filter out unchecked nodes
26+
export function filterUncheckedNodes(nodes: TreeNodeType[]): TreeNodeType[] {
27+
return nodes
28+
.filter((node) => node.checked)
29+
.map((node) => {
30+
const newNode = { ...node };
31+
if (newNode.children) {
32+
newNode.children = filterUncheckedNodes(newNode.children);
33+
}
34+
delete newNode.indeterminate;
35+
return newNode;
36+
});
37+
}
38+
39+
// Function to format tree as text
40+
export function formatTreeAsText(nodes: TreeNodeType[], prefix = "", isRoot = true): string {
41+
let result = isRoot ? "File Structure:\n\n" : "";
42+
43+
nodes.forEach((node, index) => {
44+
const isLast = index === nodes.length - 1;
45+
const connector = isLast ? "└──" : "├──";
46+
const nodeType = node.type === "folder" ? "📁" : "📄";
47+
48+
result += `${prefix}${connector} ${nodeType} ${node.name}\n`;
49+
50+
if (node.children && node.children.length > 0) {
51+
const childPrefix = prefix + (isLast ? " " : "│ ");
52+
result += formatTreeAsText(node.children, childPrefix, false);
53+
}
54+
});
55+
56+
return result;
57+
}
58+
59+
// Function to check all children of a node
60+
export function checkAllChildren(nodes: TreeNodeType[]): TreeNodeType[] {
61+
return nodes.map((node) => {
62+
const newNode = { ...node, checked: true, indeterminate: false };
63+
if (newNode.children && newNode.children.length > 0) {
64+
newNode.children = checkAllChildren(newNode.children);
65+
}
66+
return newNode;
67+
});
68+
}
69+
70+
// Function to check all folders in the tree
71+
export function checkAllFolders(nodes: TreeNodeType[]): TreeNodeType[] {
72+
return nodes.map((node) => {
73+
const newNode = { ...node };
74+
if (node.type === "folder") {
75+
newNode.checked = true;
76+
newNode.indeterminate = false;
77+
}
78+
79+
if (newNode.children && newNode.children.length > 0) {
80+
newNode.children = checkAllFolders(newNode.children);
81+
}
82+
83+
return newNode;
84+
});
85+
}
86+
87+
// Function to uncheck all children of a node
88+
export function uncheckAllChildren(nodes: TreeNodeType[]): TreeNodeType[] {
89+
return nodes.map((node) => {
90+
const newNode = { ...node, checked: false, indeterminate: false };
91+
if (newNode.children && newNode.children.length > 0) {
92+
newNode.children = uncheckAllChildren(newNode.children);
93+
}
94+
return newNode;
95+
});
96+
}
97+
98+
// Function to check a node without affecting its children
99+
export function checkWithoutChildren(node: TreeNodeType): TreeNodeType {
100+
return { ...node, checked: true, indeterminate: false };
101+
}
102+
103+
// Function to check only folders at a specific level
104+
export function checkOnlyFoldersAtLevel(nodes: TreeNodeType[]): TreeNodeType[] {
105+
return nodes.map((node) => {
106+
const newNode = { ...node };
107+
newNode.checked = node.type === "folder";
108+
newNode.indeterminate = false;
109+
110+
return newNode;
111+
});
112+
}
113+
114+
// Function to check only files at a specific level
115+
export function checkOnlyFilesAtLevel(nodes: TreeNodeType[]): TreeNodeType[] {
116+
return nodes.map((node) => {
117+
const newNode = { ...node };
118+
newNode.checked = node.type === "file";
119+
newNode.indeterminate = false;
120+
121+
return newNode;
122+
});
123+
}
124+
125+
// Function to check all children at a specific level
126+
export function checkAllChildrenAtLevel(nodes: TreeNodeType[]): TreeNodeType[] {
127+
return nodes.map((node) => {
128+
return {...node, checked: true, indeterminate: false};
129+
});
130+
}

0 commit comments

Comments
 (0)