forked from functions-dev/ocp-console-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileTreeView.test.tsx
More file actions
207 lines (172 loc) · 6.08 KB
/
Copy pathFileTreeView.test.tsx
File metadata and controls
207 lines (172 loc) · 6.08 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
202
203
204
205
206
207
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { FileTreeView } from './FileTreeView';
import { FileEntry } from '../../../common/services/types';
const nodeFuncFiles: FileEntry[] = [
{ path: '.github/workflows/func-deploy.yaml', mode: '100644', content: '', type: 'blob' },
{ path: '.gitignore', mode: '100644', content: '', type: 'blob' },
{ path: 'README.md', mode: '100644', content: '', type: 'blob' },
{ path: 'func.yaml', mode: '100644', content: '', type: 'blob' },
{ path: 'index.js', mode: '100644', content: '', type: 'blob' },
{ path: 'package.json', mode: '100644', content: '', type: 'blob' },
{ path: 'test/integration.js', mode: '100644', content: '', type: 'blob' },
{ path: 'test/unit.js', mode: '100644', content: '', type: 'blob' },
];
describe('FileTreeView', () => {
it('renders all file and directory names', () => {
render(
<FileTreeView
files={nodeFuncFiles}
selectedPath={null}
dirtyPaths={new Set()}
onSelect={vi.fn()}
/>,
);
expect(screen.getByText('.github')).toBeInTheDocument();
expect(screen.getByText('test')).toBeInTheDocument();
expect(screen.getByText('func.yaml')).toBeInTheDocument();
expect(screen.getByText('index.js')).toBeInTheDocument();
expect(screen.getByText('package.json')).toBeInTheDocument();
});
it('renders directories before files', () => {
render(
<FileTreeView
files={nodeFuncFiles}
selectedPath={null}
dirtyPaths={new Set()}
onSelect={vi.fn()}
/>,
);
const allText = document.body.textContent ?? '';
const githubPos = allText.indexOf('.github');
const testPos = allText.indexOf('test');
const gitignorePos = allText.indexOf('.gitignore');
expect(githubPos).toBeLessThan(gitignorePos);
expect(testPos).toBeLessThan(gitignorePos);
});
it('renders nested files under their parent directory', () => {
render(
<FileTreeView
files={nodeFuncFiles}
selectedPath={null}
dirtyPaths={new Set()}
onSelect={vi.fn()}
/>,
);
const testDir = screen.getByText('test').closest('[role="treeitem"]');
const nestedItems = testDir?.querySelectorAll('[role="treeitem"]');
const nestedNames = Array.from(nestedItems ?? []).map((el) => el.textContent?.trim());
expect(nestedNames).toContain('integration.js');
expect(nestedNames).toContain('unit.js');
});
it('renders deeply nested paths as nested directories', () => {
render(
<FileTreeView
files={nodeFuncFiles}
selectedPath={null}
dirtyPaths={new Set()}
onSelect={vi.fn()}
/>,
);
expect(screen.getByText('.github')).toBeInTheDocument();
expect(screen.getByText('workflows')).toBeInTheDocument();
expect(screen.getByText('func-deploy.yaml')).toBeInTheDocument();
});
it('renders multiple files under the same directory', () => {
render(
<FileTreeView
files={nodeFuncFiles}
selectedPath={null}
dirtyPaths={new Set()}
onSelect={vi.fn()}
/>,
);
const testDir = screen.getByText('test');
const testGroup = testDir.closest('[role="treeitem"]');
const nested = testGroup?.querySelectorAll('[role="treeitem"]');
expect(nested?.length).toBe(2);
});
it('shows placeholder when no files provided', () => {
render(
<FileTreeView files={[]} selectedPath={null} dirtyPaths={new Set()} onSelect={vi.fn()} />,
);
expect(screen.getByText('No files')).toBeInTheDocument();
expect(screen.getAllByRole('treeitem')).toHaveLength(1);
});
it('does not call onSelect when placeholder is clicked', async () => {
const user = userEvent.setup();
const onSelect = vi.fn();
render(
<FileTreeView files={[]} selectedPath={null} dirtyPaths={new Set()} onSelect={onSelect} />,
);
await user.click(screen.getByText('No files'));
expect(onSelect).not.toHaveBeenCalled();
});
it('calls onSelect when a file is clicked', async () => {
const user = userEvent.setup();
const onSelect = vi.fn();
render(
<FileTreeView
files={nodeFuncFiles}
selectedPath={null}
dirtyPaths={new Set()}
onSelect={onSelect}
/>,
);
await user.click(screen.getByText('func.yaml'));
expect(onSelect).toHaveBeenCalledWith('func.yaml');
});
it('does not call onSelect when a directory is clicked', async () => {
const user = userEvent.setup();
const onSelect = vi.fn();
render(
<FileTreeView
files={nodeFuncFiles}
selectedPath={null}
dirtyPaths={new Set()}
onSelect={onSelect}
/>,
);
await user.click(screen.getByText('test'));
expect(onSelect).not.toHaveBeenCalled();
});
it('shows loading state with spinner', () => {
render(
<FileTreeView
files={[]}
selectedPath={null}
dirtyPaths={new Set()}
isLoading
onSelect={vi.fn()}
/>,
);
expect(screen.getByText('Loading source...')).toBeInTheDocument();
expect(screen.queryByText('No files')).not.toBeInTheDocument();
});
it('renders folder icons on directory nodes but not files', () => {
render(
<FileTreeView
files={nodeFuncFiles}
selectedPath={null}
dirtyPaths={new Set()}
onSelect={vi.fn()}
/>,
);
const testDir = screen.getByText('test').closest('[role="treeitem"]');
expect(testDir?.querySelector('.pf-v6-c-tree-view__node-icon')).toBeInTheDocument();
const fileItem = screen.getByText('index.js').closest('[role="treeitem"]');
expect(fileItem?.querySelector('.pf-v6-c-tree-view__node-icon')).not.toBeInTheDocument();
});
it('shows dirty indicator for modified files', () => {
render(
<FileTreeView
files={nodeFuncFiles}
selectedPath={null}
dirtyPaths={new Set(['func.yaml'])}
onSelect={vi.fn()}
/>,
);
expect(screen.getByText(/func\.yaml \u25CF/)).toBeInTheDocument();
expect(screen.queryByText(/index\.js \u25CF/)).not.toBeInTheDocument();
});
});