-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathsortUtils.spec.ts
More file actions
138 lines (116 loc) · 4.78 KB
/
Copy pathsortUtils.spec.ts
File metadata and controls
138 lines (116 loc) · 4.78 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { URI } from 'vscode-uri';
import { GetMTimeOfFile, _sortPaths } from './sortUtils.pure';
const getMTimeOfFile: GetMTimeOfFile = async (uri: URI) => {
// will inject this prop in uri init
return (uri as any).lastModified;
};
const sortPaths = _sortPaths(getMTimeOfFile, URI.file);
const sortByPath = sortPaths('path');
const sortByAlphabetical = sortPaths('alphabet');
// const sortByLastModified = sortPaths('last-modified'); sortByLastModified is special (It will call URI.file in function).
const sortByLastModifiedRef = sortPaths('last-modified-refs');
const file: (modifiedTimeMap: Record<string, number>) => (path: string) => URI =
(map) => (path) => {
const uri = URI.file(path);
(uri as any).lastModified = map[path];
return uri;
};
const createURIWithLastModified = (lastModified: number): URI => {
const uri = URI.file(`/path/to/${lastModified}`);
(uri as any).lastModified = lastModified;
return uri;
};
describe('sortUtils', () => {
describe('sortByPath', () => {
it('should sort references by path in ascending order', async () => {
const referencesByPath = {
'b/path': [{ location: { uri: createURIWithLastModified(100) } }],
'a/path': [{ location: { uri: createURIWithLastModified(200) } }],
};
const result = await sortByPath(referencesByPath);
expect(result).toEqual(['a/path', 'b/path']);
});
it('should handle an empty referencesByPath object', async () => {
const referencesByPath: Record<string, any[]> = {};
const result = await sortByPath(referencesByPath);
expect(result).toEqual([]);
});
});
describe('sortByAlphabetical', () => {
it('should sort references by path in alphabetical order', async () => {
const referencesByPath = {
'b/path': [{ location: { uri: createURIWithLastModified(100) } }],
'a/path': [{ location: { uri: createURIWithLastModified(200) } }],
};
const result = await sortByAlphabetical(referencesByPath);
expect(result).toEqual(['a/path', 'b/path']);
});
it('should handle an empty referencesByPath object', async () => {
const referencesByPath: Record<string, any[]> = {};
const result = await sortByAlphabetical(referencesByPath);
expect(result).toEqual([]);
});
});
describe('sortByLastModified', () => {
it('should sort references by the last modified time', async () => {
const sortPaths = _sortPaths(getMTimeOfFile, file({ path1: 100, path2: 300 }));
const sortByLastModified = sortPaths('last-modified');
const referencesByPath = {
path1: [],
path2: [],
};
const result = await sortByLastModified(referencesByPath);
expect(result).toEqual(['path2', 'path1']);
});
it('should handle an empty referencesByPath object', async () => {
const sortPaths = _sortPaths(getMTimeOfFile, file({}));
const sortByLastModified = sortPaths('last-modified');
const referencesByPath: Record<string, any[]> = {};
const result = await sortByLastModified(referencesByPath);
expect(result).toEqual([]);
});
it('should correctly handle multiple file with the same modification time', async () => {
const referencesByPath = {
path1: [],
path2: [],
};
const sortPaths = _sortPaths(getMTimeOfFile, file({ path1: 0, path2: 0 }));
const sortByLastModified = sortPaths('last-modified');
// each result is accepted
expect(async () => {
await sortByLastModified(referencesByPath);
}).not.toThrow();
});
});
describe('sortByLastModifiedRef', () => {
it('should sort references by the last modified time of the reference in ascending order', async () => {
const references = {
path1: [
{ location: { uri: createURIWithLastModified(150) } },
{ location: { uri: createURIWithLastModified(1000) } },
],
path2: [
{ location: { uri: createURIWithLastModified(150) } },
{ location: { uri: createURIWithLastModified(2000) } },
],
};
const result = await sortByLastModifiedRef(references);
expect(result).toEqual(['path2', 'path1']);
});
it('should handle an empty array of references', async () => {
const references: Record<string, any> = {};
const result = await sortByLastModifiedRef(references);
expect(result).toEqual([]);
});
it('should correctly handle multiple references with the same modification time', async () => {
const references = {
path1: [{ location: { uri: createURIWithLastModified(150) } }],
path2: [{ location: { uri: createURIWithLastModified(150) } }],
};
// each result is accepted
expect(async () => {
await sortByLastModifiedRef(references);
}).not.toThrow();
});
});
});