fix: merge discovery state (with test)

* update fileNode.toString to add discovery state
This commit is contained in:
wancat committed 2023-08-29 16:28:29 +08:00
1 parent f360d783f8
commit 6b94b1ae81
2 files changed
+40 -5

No files matched your search

+11 -5
View File
@@ -80,6 +80,9 @@ export default class FileNode extends File {
// If this is the same node, merge the children
if (fileNode.path === this.path) {
// Update the state of this node
this.discovered = this.discovered || fileNode.discovered
fileNode.children.forEach((child) => {
const existingChild = this.children.find((c) => c.path === child.path)
if (existingChild) {
@@ -116,12 +119,15 @@ export default class FileNode extends File {
}
}
toString() {
const indent = ' '.repeat(this.path.split('/').length - 1)
const suffix = this.isDirectory() ? '/' : ''
toString(level = 0) {
let { name } = this
const indent = ' '.repeat(level)
name = `${indent}${name}`
if (this.isDirectory()) name += '/'
if (!this.discovered) name += `*`
const childrenString = this.children
.map((child) => child.toString())
.map((child) => child.toString(level + 1))
.join('')
return `${indent}${this.name}${suffix}\n${childrenString}`
return `${name}\n${childrenString}`
}
}
+29
View File
@@ -193,6 +193,35 @@ describe('FileNode', () => {
mergeTest(files1, files2, expected)
})
test('merge-hidden', () => {
const root = {
path: '/',
type: 'folder',
}
const files1 = [
root,
{ path: '/a', type: 'file', discovered: true },
{ path: '/.b', type: 'file', discovered: false },
{ path: '/c', type: 'folder', discovered: true },
{ path: '/c/.d', type: 'file', discovered: false },
]
const files2 = [
{ path: '/c', type: 'folder', discovered: true },
{ path: '/c/.d', type: 'file', discovered: true },
]
const expected = [
root,
{ path: '/a', type: 'file', discovered: true },
{ path: '/.b', type: 'file', discovered: false },
{ path: '/c', type: 'folder', discovered: true },
{ path: '/c/.d', type: 'file', discovered: true },
]
mergeTest(files1, files2, expected)
})
test('remove-child', () => {
const root = {
path: '/',