1 import type { Label } from '@proton/shared/lib/interfaces';
2 import type { Folder } from '@proton/shared/lib/interfaces/Folder';
4 import MailImportFoldersParser from './MailImportFoldersParser/MailImportFoldersParser';
5 import { getApiFoldersTestHelper } from './MailImportFoldersParser/MailImportFoldersParser.test';
6 import { isNameAlreadyUsed, isNameEmpty, isNameReserved, isNameTooLong } from './errorsMapping';
8 const smallString = '6NLaLHynY3YPM8gGLncefo5PP7n2Db';
10 'wIfm5MY1a2j7MwYAFNzQapBIXZdBxZaqRGwun6UBFNVimgw38tmmLhn7HewkHhvuNYf5QlC8a2NmfctV42tdfrJJm10okXooWV5f';
12 describe('Activation errors mapping', () => {
13 describe('isNameTooLong', () => {
14 it('Should return false if the folder/label name is smaller than limit', () => {
15 const res = isNameTooLong(smallString);
16 expect(res).toBe(false);
18 it('Should return true if the folder/label name is longer than limit', () => {
19 const res = isNameTooLong(longString);
20 expect(res).toBe(true);
24 describe('isNameReserved', () => {
25 it('Should return false if the name is not reserved', () => {
26 const res = isNameReserved('folder');
27 expect(res).toBe(false);
29 it('Should return true if the name is reserved and capitalized', () => {
30 const res = isNameReserved('Scheduled');
31 expect(res).toBe(true);
33 it('Should return true if the name is reserved and lowercase', () => {
34 const res = isNameReserved('scheduled');
35 expect(res).toBe(true);
39 describe('isNameAlreadyUsed', () => {
40 describe('Is folder mapping', () => {
41 const isLabelMapping = false;
42 it('Should return false if the name not present in collection', () => {
43 const collection = new MailImportFoldersParser(
44 getApiFoldersTestHelper(['path1', 'path2', 'path3']),
47 const item = collection[0];
49 const res = isNameAlreadyUsed(item, collection, [], [], isLabelMapping);
50 expect(res).toBe(false);
53 it('Should return false if a parent has same name as the child', () => {
54 const collection = new MailImportFoldersParser(
55 getApiFoldersTestHelper(['marco', 'marco/marco']),
58 const item = collection[0];
60 const res = isNameAlreadyUsed(item, collection, [], [], isLabelMapping);
61 expect(res).toBe(false);
64 it('Should return false if name not present in array', () => {
65 const collection = new MailImportFoldersParser(
66 getApiFoldersTestHelper(['path1', 'path2']),
69 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path3']), isLabelMapping).folders[0];
70 const res = isNameAlreadyUsed(item, collection, [], [], isLabelMapping);
71 expect(res).toBe(false);
74 it('Should return false if name not present in array', () => {
75 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path3']), isLabelMapping).folders[0];
76 const collection = new MailImportFoldersParser(
77 getApiFoldersTestHelper(['path1', 'path2']),
80 const res = isNameAlreadyUsed(item, collection, [], [], isLabelMapping);
81 expect(res).toBe(false);
84 it('Should return true if name present in array', () => {
85 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path3']), isLabelMapping).folders[0];
86 // @ts-expect-error need to override the ID because test will think it's the same item
87 item.id = 'anothername';
88 const collection = new MailImportFoldersParser(
89 getApiFoldersTestHelper(['path1', 'path2', 'path3']),
93 const res = isNameAlreadyUsed(item, collection, [], [], isLabelMapping);
94 expect(res).toBe(true);
97 it('Should return false if name present in an empty array', () => {
98 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path1']), isLabelMapping).folders[0];
99 const collection = new MailImportFoldersParser(getApiFoldersTestHelper([]), isLabelMapping).folders;
101 const res = isNameAlreadyUsed(item, collection, [], [], isLabelMapping);
102 expect(res).toBe(false);
105 it('Should return false if label with similar name exists', () => {
106 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path1']), isLabelMapping).folders[0];
107 const collection = new MailImportFoldersParser(getApiFoldersTestHelper([]), isLabelMapping).folders;
109 const res = isNameAlreadyUsed(item, collection, [], [], isLabelMapping);
110 expect(res).toBe(false);
113 it('Should return true if label has same name', () => {
114 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path1']), isLabelMapping).folders[0];
116 const res = isNameAlreadyUsed(item, [], [{ Path: 'path1' } as Label], [], isLabelMapping);
117 expect(res).toBe(true);
120 it('Should return false if folder has same name', () => {
121 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path1']), isLabelMapping).folders[0];
123 const res = isNameAlreadyUsed(item, [], [], [{ Path: 'path1' } as Folder], isLabelMapping);
124 expect(res).toBe(false);
128 describe('Is label mapping', () => {
129 const isLabelMapping = true;
130 it('Should return false if the name not present in collection', () => {
131 const collection = new MailImportFoldersParser(
132 getApiFoldersTestHelper(['path1', 'path2', 'path3']),
135 const item = collection[0];
137 const res = isNameAlreadyUsed(item, collection, [], [], isLabelMapping);
138 expect(res).toBe(false);
141 it('Should return false if a parent has same name as the child', () => {
142 const collection = new MailImportFoldersParser(
143 getApiFoldersTestHelper(['marco', 'marco/marco']),
146 const item = collection[0];
148 const res = isNameAlreadyUsed(item, collection, [], [], isLabelMapping);
149 expect(res).toBe(false);
152 it('Should return false if name not present in array', () => {
153 const collection = new MailImportFoldersParser(
154 getApiFoldersTestHelper(['path1', 'path2']),
157 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path3']), isLabelMapping).folders[0];
158 const res = isNameAlreadyUsed(item, collection, [], [], isLabelMapping);
159 expect(res).toBe(false);
162 it('Should return false if name not present in array', () => {
163 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path3']), isLabelMapping).folders[0];
164 const collection = new MailImportFoldersParser(
165 getApiFoldersTestHelper(['path1', 'path2']),
168 const res = isNameAlreadyUsed(item, collection, [], [], isLabelMapping);
169 expect(res).toBe(false);
172 it('Should return true if name present in array', () => {
173 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path3']), isLabelMapping).folders[0];
174 // @ts-expect-error need to override the ID because test will think it's the same item
175 item.id = 'anothername';
176 const collection = new MailImportFoldersParser(
177 getApiFoldersTestHelper(['path1', 'path2', 'path3']),
181 const res = isNameAlreadyUsed(item, collection, [], [], isLabelMapping);
182 expect(res).toBe(true);
185 it('Should return false if name present in an empty array', () => {
186 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path1']), isLabelMapping).folders[0];
187 const collection = new MailImportFoldersParser(getApiFoldersTestHelper([]), isLabelMapping).folders;
189 const res = isNameAlreadyUsed(item, collection, [], [], isLabelMapping);
190 expect(res).toBe(false);
193 it('Should return false if label has same name', () => {
194 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path1']), isLabelMapping).folders[0];
196 const res = isNameAlreadyUsed(item, [], [{ Path: 'path1' } as Label], [], isLabelMapping);
197 expect(res).toBe(false);
200 it('Should return true if folder has same name', () => {
201 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path1']), isLabelMapping).folders[0];
203 const res = isNameAlreadyUsed(item, [], [], [{ Path: 'path1' } as Folder], isLabelMapping);
204 expect(res).toBe(true);
208 describe('Spaces checks', () => {
209 it('Should return true if name in collection contains a space before', () => {
210 const isLabelMapping = false;
211 const item = new MailImportFoldersParser(getApiFoldersTestHelper([' path3']), isLabelMapping)
213 // @ts-expect-error need to override the ID because test will think it's the same item
214 item.id = 'anothername';
215 const collection = new MailImportFoldersParser(
216 getApiFoldersTestHelper(['path1', 'path2', 'path3']),
220 const res = isNameAlreadyUsed(item, collection, [], [], false);
221 expect(res).toBe(true);
224 it('Should return true if name in item contains a space after', () => {
225 const isLabelMapping = false;
226 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path3 ']), isLabelMapping)
228 // @ts-expect-error need to override the ID because test will think it's the same item
229 item.id = 'anothername';
230 const collection = new MailImportFoldersParser(
231 getApiFoldersTestHelper(['path1', 'path2', 'path3']),
235 const res = isNameAlreadyUsed(item, collection, [], [], false);
236 expect(res).toBe(true);
239 it('Should return true if name in item contains a space before and after', () => {
240 const isLabelMapping = false;
241 const item = new MailImportFoldersParser(getApiFoldersTestHelper([' path3 ']), isLabelMapping)
243 // @ts-expect-error need to override the ID because test will think it's the same item
244 item.id = 'anothername';
245 const collection = new MailImportFoldersParser(
246 getApiFoldersTestHelper(['path1', 'path2', 'path3']),
250 const res = isNameAlreadyUsed(item, collection, [], [], false);
251 expect(res).toBe(true);
254 it('Should return true if the name present in item contains a space before and after and is capitalized', () => {
255 const isLabelMapping = false;
256 const item = new MailImportFoldersParser(getApiFoldersTestHelper([' Path3 ']), isLabelMapping)
258 // @ts-expect-error need to override the ID because test will think it's the same item
259 item.id = 'anothername';
260 const collection = new MailImportFoldersParser(
261 getApiFoldersTestHelper(['path1', 'path2', 'path3']),
265 const res = isNameAlreadyUsed(item, collection, [], [], false);
266 expect(res).toBe(true);
269 it('Should return true if the name present in item contains two space before', () => {
270 const isLabelMapping = false;
271 const item = new MailImportFoldersParser(getApiFoldersTestHelper([' Path3']), isLabelMapping)
273 // @ts-expect-error need to override the ID because test will think it's the same item
274 item.id = 'anothername';
275 const collection = new MailImportFoldersParser(
276 getApiFoldersTestHelper(['path1', 'path2', 'path3']),
280 const res = isNameAlreadyUsed(item, collection, [], [], false);
281 expect(res).toBe(true);
284 it('Should return true if the name in collection contains a space before and after', () => {
285 const isLabelMapping = false;
286 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path3']), isLabelMapping).folders[0];
287 // @ts-expect-error need to override the ID because test will think it's the same item
288 item.id = 'anothername';
289 const collection = new MailImportFoldersParser(
290 getApiFoldersTestHelper(['path1', 'path2', ' path3 ']),
294 const res = isNameAlreadyUsed(item, collection, [], [], false);
295 expect(res).toBe(true);
298 it('Should return true if the name passed in collection contains a space before and after and is capitalized', () => {
299 const isLabelMapping = false;
300 const item = new MailImportFoldersParser(getApiFoldersTestHelper(['path3']), isLabelMapping).folders[0];
301 // @ts-expect-error need to override the ID because test will think it's the same item
302 item.id = 'anothername';
303 const collection = new MailImportFoldersParser(
304 getApiFoldersTestHelper(['path1', 'path2', ' Path3 ']),
308 const res = isNameAlreadyUsed(item, collection, [], [], false);
309 expect(res).toBe(true);
314 describe('isNameEmpty', () => {
315 it('Should return false if the name is not empty', () => {
316 const res = isNameEmpty('folder');
317 expect(res).toBe(false);
319 it('Should return true if the name is an empty string', () => {
320 const res = isNameEmpty('');
321 expect(res).toBe(true);
323 it('Should return true if the name is undefined', () => {
324 const res = isNameEmpty(undefined);
325 expect(res).toBe(true);
329 describe('hasMergeWarning', () => {});