fix(renderer): drop redundant ternary, add loadChildren idempotence test

This commit is contained in:
2026-06-05 14:50:56 +05:30
parent fa5a46d5ac
commit 17f318dd75
2 changed files with 24 additions and 2 deletions
+2 -2
View File
@@ -46,7 +46,7 @@ function entryToNode(entry: FileEntry): FileNode {
name: entry.name, name: entry.name,
path: entry.path, path: entry.path,
isDirectory: entry.isDirectory, isDirectory: entry.isDirectory,
children: entry.isDirectory ? null : null, children: null, // lazy: start unloaded; loadChildren populates this
}; };
} }
@@ -97,7 +97,7 @@ export const useFileStore = create<FileState>()(
let found = false; let found = false;
updateNode(tree, dirPath, (node) => { updateNode(tree, dirPath, (node) => {
if (node.loaded) return; if (node.loaded === true) return;
found = true; found = true;
}); });
if (!found) return; if (!found) return;
+22
View File
@@ -139,6 +139,28 @@ describe('useFileStore', () => {
expect(fakeList).not.toHaveBeenCalled(); expect(fakeList).not.toHaveBeenCalled();
}); });
it('loadChildren is idempotent: does not re-fetch when already loaded', async () => {
fakeList.mockResolvedValue({
ok: true,
data: [{ name: 'src', path: '/root/src', isDirectory: true }],
});
await useFileStore.getState().openFolder('/root');
fakeList.mockClear();
fakeList.mockResolvedValue({
ok: true,
data: [{ name: 'foo.ts', path: '/root/src/foo.ts', isDirectory: false }],
});
await useFileStore.getState().loadChildren('/root/src');
const callsAfterFirst = fakeList.mock.calls.length;
expect(callsAfterFirst).toBe(1);
// Second call should be a no-op (loaded is true)
await useFileStore.getState().loadChildren('/root/src');
expect(fakeList.mock.calls.length).toBe(callsAfterFirst);
});
// --- toggleExpanded --- // --- toggleExpanded ---
it('toggleExpanded adds and removes paths from the expanded Set', () => { it('toggleExpanded adds and removes paths from the expanded Set', () => {