diff --git a/tests/test_layer1_render.test.mjs b/tests/test_layer1_render.test.mjs new file mode 100644 index 0000000..77919fe --- /dev/null +++ b/tests/test_layer1_render.test.mjs @@ -0,0 +1,48 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; + +import { render } from '../upload/frontend/table/render.js'; + +function elements() { + return { tableBodyEl: { innerHTML: '' }, countEl: { textContent: '' } }; +} + +function fileNode() { + return { + id: 'file-1', + kind: 'file', + name: 'document.txt', + path: 'folder/document.txt', + file: { size: 7 }, + children: [], + expanded: true, + }; +} + +test('render: папка по умолчанию скрывает дочерние файлы', () => { + const child = fileNode(); + const state = { + nodes: [{ + id: 'folder-1', + kind: 'folder', + name: 'folder', + path: 'folder', + children: [child], + expanded: false, + }], + }; + const output = elements(); + + render(state, output); + + assert.match(output.tableBodyEl.innerHTML, /data-toggle="folder-1"/); + assert.match(output.tableBodyEl.innerHTML, /aria-expanded="false"/); + assert.doesNotMatch(output.tableBodyEl.innerHTML, /data-path="folder\/document\.txt"/); + assert.equal(output.countEl.textContent, '1 файлов · 7 B'); + + state.nodes[0].expanded = true; + render(state, output); + + assert.match(output.tableBodyEl.innerHTML, /data-path="folder\/document\.txt"/); + assert.match(output.tableBodyEl.innerHTML, /aria-expanded="true"/); +}); \ No newline at end of file diff --git a/upload/frontend/table/on_folder_change.js b/upload/frontend/table/on_folder_change.js index 0cd5890..5ffed6c 100644 --- a/upload/frontend/table/on_folder_change.js +++ b/upload/frontend/table/on_folder_change.js @@ -31,7 +31,7 @@ export function onFolderChange(state, cfg, elements) { if (!roots.has(rootName)) { // Один root на выбранный каталог позволяет сохранить дерево целиком. roots.set(rootName, { id: crypto.randomUUID(), kind: 'folder', name: rootName, - path: rootName, children: [], expanded: true }); + path: rootName, children: [], expanded: false }); } const root = roots.get(rootName); // Вставляет узел по его пути, создавая отсутствующие промежуточные папки. @@ -44,7 +44,7 @@ export function onFolderChange(state, cfg, elements) { if (!child) { child = last ? node : { id: crypto.randomUUID(), kind: 'folder', name: part, path: `${rootName}/${nodeParts.slice(0, index + 1).join('/')}`, - children: [], expanded: true }; + children: [], expanded: false }; current.children.push(child); } current = child; diff --git a/upload/frontend/zip/list_zip_files.js b/upload/frontend/zip/list_zip_files.js index 9e3ff01..0a9e4d4 100644 --- a/upload/frontend/zip/list_zip_files.js +++ b/upload/frontend/zip/list_zip_files.js @@ -36,9 +36,9 @@ function safeEntryParts(entryName) { return parts; } -/** Создаёт единый узел file, folder или zip с уникальным id и раскрытым состоянием. */ +/** Создаёт единый узел file, folder или zip с уникальным id и начальным состоянием раскрытия. */ function node(kind, name, path, children = [], file = null) { - return { id: crypto.randomUUID(), kind, name, path, children, file, expanded: true }; + return { id: crypto.randomUUID(), kind, name, path, children, file, expanded: kind === 'file' }; } /** Вставляет leaf или вложенное дерево по сегментам пути, создавая folder-узлы. */