From 264d70fe3f612cd8c32986f5e7632e220d197d70 Mon Sep 17 00:00:00 2001 From: wancat Date: Tue, 29 Aug 2023 13:27:47 +0800 Subject: [PATCH] feat: add directly discover tests --- packages/file-graph/fileGraph.js | 16 ++----- packages/file-graph/fileGraph.test.js | 62 ++++++++++++++++++++------- 2 files changed, 50 insertions(+), 28 deletions(-) diff --git a/packages/file-graph/fileGraph.js b/packages/file-graph/fileGraph.js index 7bfda7d..a2a924e 100644 --- a/packages/file-graph/fileGraph.js +++ b/packages/file-graph/fileGraph.js @@ -1,17 +1,13 @@ import FileNode from './fileNode.js' -export default class FileGraph { - constructor(root) { - this.root = new FileNode(root) - } - +export default class FileGraph extends FileNode { add(files) { // Logic to add the file node to the graph // You can implement the logic to find the appropriate location in the file graph // If the file node already exists, throw an error files.forEach((file) => { - this.root.addChild(file, { makeParents: true }) + this.addChild(file, { makeParents: true }) }) } @@ -21,7 +17,7 @@ export default class FileGraph { // If the file node doesn't exist, throw an error files.forEach((file) => { - this.root.removeChild(file) + this.removeChild(file) }) } @@ -36,7 +32,7 @@ export default class FileGraph { newNode.addChild(file, { makeParents: true }) }) - this.root.merge(newNode) + this.merge(newNode) } handleEvent(event) { @@ -54,8 +50,4 @@ export default class FileGraph { // Implement other cases if needed } } - - toJSON() { - return this.root.toJSON() - } } diff --git a/packages/file-graph/fileGraph.test.js b/packages/file-graph/fileGraph.test.js index 7f6447a..cff996a 100644 --- a/packages/file-graph/fileGraph.test.js +++ b/packages/file-graph/fileGraph.test.js @@ -30,9 +30,9 @@ describe('FileGraph', () => { fileGraph.add(filesToAdd) - console.log(fileGraph.root.toString()) - expect(fileGraph.root.children.length).toBe(1) - expect(fileGraph.root.children[0].children[0].children.length).toBe(2) + console.log(fileGraph.toString()) + expect(fileGraph.children.length).toBe(1) + expect(fileGraph.children[0].children[0].children.length).toBe(2) }) test('remove files from graph', () => { @@ -76,14 +76,44 @@ describe('FileGraph', () => { fileGraph.remove(filesToRemove) - console.log(fileGraph.root.toString()) - expect(fileGraph.root.children[0].children[0].children.length).toBe(1) - expect(fileGraph.root.children[0].children[0].children[0].name).toBe( - 'folder1' + console.log(fileGraph.toString()) + expect(fileGraph.children[0].children[0].children.length).toBe(1) + expect(fileGraph.children[0].children[0].children[0].name).toBe('folder1') + expect(fileGraph.children[0].children[0].children[0].children.length).toBe( + 0 + ) + }) + + test('discover', () => { + const fileGraph = new FileGraph(root) + const discoverFiles = [ + { + path: '/home/user/folder1', + type: 'folder', + discovered: true, + }, + { + path: '/home/user/folder1/file1.txt', + type: 'file', + discovered: true, + }, + { + path: '/home/user/folder1/file2.txt', + type: 'file', + discovered: true, + }, + { + path: '/home/user/folder1/file3.txt', + type: 'file', + discovered: true, + }, + ] + + fileGraph.discover(discoverFiles) + console.log(fileGraph.toString()) + expect(fileGraph.children[0].children[0].children[0].children.length).toBe( + 3 ) - expect( - fileGraph.root.children[0].children[0].children[0].children.length - ).toBe(0) }) test('discover new files', () => { @@ -113,7 +143,7 @@ describe('FileGraph', () => { ] fileGraph.add(filesToAdd) - console.log(fileGraph.root.toString()) + console.log(fileGraph.toString()) const newFiles = [ { @@ -140,10 +170,10 @@ describe('FileGraph', () => { fileGraph.discover(newFiles) - console.log(fileGraph.root.toString()) - expect(fileGraph.root.children[0].children[0].children.length).toBe(2) - expect( - fileGraph.root.children[0].children[0].children[0].children.length - ).toBe(3) + console.log(fileGraph.toString()) + expect(fileGraph.children[0].children[0].children.length).toBe(2) + expect(fileGraph.children[0].children[0].children[0].children.length).toBe( + 3 + ) }) })