-
Notifications
You must be signed in to change notification settings - Fork 0
/
children.test.ts
118 lines (89 loc) · 2.99 KB
/
children.test.ts
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
import flexi from "../../src";
import { PathType } from "../../src/types";
import testData from "../jest/createTestData";
describe("path", () => {
describe("children", () => {
it("can list files", () => {
const dir = flexi
.path(testData.testDir)
.append("can_list_files/")
.write();
dir.append("file1.txt").write();
dir.append("file2.js").write();
expect(dir.children()).toHaveLength(2);
});
it("can list directories", () => {
const dir = flexi
.path(testData.testDir)
.append("can_list_directories/")
.write();
dir.append("sub1/").write();
dir.append("sub2/").write();
dir.append("sub3/").write();
expect(dir.children()).toHaveLength(3);
});
it("can list files and directories", () => {
const dir = flexi
.path(testData.testDir)
.append("can_list_files_and_directories/")
.write();
dir.append("sub1/").write();
dir.append("sub2/").write();
dir.append("file1.txt").write();
dir.append("file2.js").write();
const result = dir.children();
expect(result.filter(x => x.type() === PathType.Directory)).toHaveLength(
2
);
expect(result.filter(x => x.type() === PathType.File)).toHaveLength(2);
});
it("should be empty when directory is empty", () => {
const empty = flexi
.path(testData.testDir)
.append("empty_when_directory_is_empty")
.write();
expect(empty.children()).toBeEmpty();
});
it("should be empty when path is file", () => {
const file = flexi
.path(testData.testDir)
.append("empty_when_path_is_file.js")
.write();
expect(file.children()).toBeEmpty();
});
it("should be empty when path is invalid", () => {
const invalid = flexi.path("invalid");
expect(invalid.children()).toBeEmpty();
});
it("can filter content", () => {
const dir = flexi
.path(testData.testDir)
.append("can_filter_content/")
.write();
dir.append("file1.txt").write();
dir.append("file2.txt").write();
dir.append("another.txt").write();
dir.append("dir1/").write();
expect(dir.children(x => x.name.startsWith("file"))).toHaveLength(2);
});
it("can walk until found", () => {
const dir = flexi
.path(testData.testDir)
.append("walk_until_found/")
.write();
dir.append("deep", "path", "below/").write();
const result = dir.children(x => x.name === "below", { recursive: true });
expect(result).toHaveLength(1);
expect(result[0].name).toBe("below");
});
it("should be empty when walked and not found", () => {
const dir = flexi
.path(testData.testDir)
.append("empty_when_walked_and_not_found/")
.write();
dir.append("deep", "path", "below/").write();
const result = dir.children(x => x.name === "invalid");
expect(result).toBeEmpty();
});
});
});