-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathitemNames.spec.ts
More file actions
335 lines (289 loc) · 9.51 KB
/
Copy pathitemNames.spec.ts
File metadata and controls
335 lines (289 loc) · 9.51 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import { DriveItemDataProps } from '../types/drive';
import { getDisplayName } from './itemNames';
describe('getDisplayName', () => {
describe('Folders', () => {
it('should return the folder name as-is', () => {
const folder = {
id: 1,
name: 'Documents',
isFolder: true,
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(folder)).toBe('Documents');
});
it('should handle folder names with special characters', () => {
const folder: DriveItemDataProps = {
id: 1,
name: 'My Folder (2024)',
isFolder: true,
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(folder)).toBe('My Folder (2024)');
}) as unknown as DriveItemDataProps;
it('should handle folder names with dots', () => {
const folder: DriveItemDataProps = {
id: 1,
name: 'backup.2024',
isFolder: true,
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(folder)).toBe('backup.2024');
});
});
describe('Files without extension in name', () => {
it('should append the extension to the file name', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'report',
type: 'pdf',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('report.pdf');
});
it('should handle different file extensions', () => {
const cases = [
{ name: 'image', type: 'jpg', expected: 'image.jpg' },
{ name: 'document', type: 'docx', expected: 'document.docx' },
{ name: 'video', type: 'mp4', expected: 'video.mp4' },
{ name: 'data', type: 'json', expected: 'data.json' },
];
cases.forEach(({ name, type, expected }) => {
const file: DriveItemDataProps = {
id: 1,
name,
type,
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe(expected);
});
});
});
describe('Files with extension already in name', () => {
it('should always append the type extension even if present in name', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'report.pdf',
type: 'pdf',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('report.pdf.pdf');
});
it('should append extension regardless of case matching', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'Image.JPG',
type: 'jpg',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('Image.JPG.jpg');
});
it('should always append type to files with multiple dots in name', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'backup.2024.01.15.tar',
type: 'tar',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('backup.2024.01.15.tar.tar');
});
it('should append type even when name ends with same extension', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'document.PDF',
type: 'pdf',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('document.PDF.pdf');
});
});
describe('Files without type', () => {
it('should return the name as-is when type is undefined', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'unknown',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('unknown');
});
it('should return the name as-is when type is null', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'noextension',
type: undefined,
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('noextension');
});
it('should preserve existing extension in name when type is undefined', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'file.txt',
type: undefined,
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('file.txt');
});
});
describe('Edge cases', () => {
it('should handle empty type string', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'file',
type: '',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('file');
});
it('should handle type with leading/trailing whitespace', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'document',
type: ' pdf ',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('document. pdf ');
});
it('should handle type with only whitespace', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'document',
type: ' ',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('document');
});
it('should handle files with dots but different extension', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'archive.tar',
type: 'gz',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('archive.tar.gz');
});
it('should handle numeric extensions', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'file',
type: '001',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('file.001');
});
it('should handle special characters in extension', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'document',
type: 'pdf~',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('document.pdf~');
});
it('should handle very long extensions', () => {
const longExtension = 'verylongextensionname';
const file: DriveItemDataProps = {
id: 1,
name: 'file',
type: longExtension,
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe(`file.${longExtension}`);
});
});
describe('Real-world scenarios', () => {
it('should handle common document files', () => {
const scenarios = [
{ name: 'Meeting Notes', type: 'docx', expected: 'Meeting Notes.docx' },
{ name: 'Budget 2024', type: 'xlsx', expected: 'Budget 2024.xlsx' },
{ name: 'Presentation', type: 'pptx', expected: 'Presentation.pptx' },
];
scenarios.forEach(({ name, type, expected }) => {
const file: DriveItemDataProps = {
id: 1,
name,
type,
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe(expected);
});
});
it('should handle compressed archives with double extensions', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'backup.tar',
type: 'gz',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('backup.tar.gz');
});
it('should handle versioned files', () => {
const file: DriveItemDataProps = {
id: 1,
name: 'document.v2',
type: 'pdf',
isFolder: false,
fileId: 'file-1',
updatedAt: '2025-01-01',
createdAt: '2025-01-01',
} as unknown as DriveItemDataProps;
expect(getDisplayName(file)).toBe('document.v2.pdf');
});
});
});