Skip to content

Commit fccbb10

Browse files
Aline Cristina Bastos Godoi Lariguetjhonyeduardo
authored andcommitted
feat(upload): exibir restrições de arquivos
Novo subcomponente interno que exibe informações referente às restrições dos arquivos. Fixes DTHFUI-788
1 parent 30dbcf1 commit fccbb10

17 files changed

+822
-35
lines changed

projects/ui/src/lib/components/po-field/po-field.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { PoPasswordComponent } from './po-password/po-password.component';
3636
import { PoRadioGroupComponent } from './po-radio-group/po-radio-group.component';
3737
import { PoSelectComponent } from './po-select/po-select.component';
3838
import { PoSelectOptionTemplateDirective } from './po-select/po-select-option-template/po-select-option-template.directive';
39+
import { PoServicesModule } from '../../services/services.module';
3940
import { PoSwitchComponent } from './po-switch/po-switch.component';
4041
import { PoTextareaComponent } from './po-textarea/po-textarea.component';
4142
import { PoUploadComponent } from './po-upload/po-upload.component';
@@ -45,6 +46,7 @@ import {
4546
PoUploadDragDropAreaOverlayComponent
4647
} from './po-upload/po-upload-drag-drop/po-upload-drag-drop-area-overlay/po-upload-drag-drop-area-overlay.component';
4748
import { PoUploadDragDropAreaComponent } from './po-upload/po-upload-drag-drop/po-upload-drag-drop-area/po-upload-drag-drop-area.component';
49+
import { PoUploadFileRestrictionsComponent } from './po-upload/po-upload-file-restrictions/po-upload-file-restrictions.component';
4850
import { PoUrlComponent } from './po-url/po-url.component';
4951

5052
/**
@@ -58,12 +60,13 @@ import { PoUrlComponent } from './po-url/po-url.component';
5860
CommonModule,
5961
FormsModule,
6062
HttpClientModule,
63+
PoButtonGroupModule,
6164
PoButtonModule,
6265
PoDisclaimerModule,
6366
PoLoadingModule,
6467
PoModalModule,
68+
PoServicesModule,
6569
PoTableModule,
66-
PoButtonGroupModule
6770
],
6871
exports: [
6972
PoCheckboxGroupComponent,
@@ -123,6 +126,7 @@ import { PoUrlComponent } from './po-url/po-url.component';
123126
PoUploadDragDropDirective,
124127
PoUploadDragDropAreaOverlayComponent,
125128
PoUploadDragDropAreaComponent,
129+
PoUploadFileRestrictionsComponent,
126130
PoUrlComponent
127131
],
128132
providers: [],

projects/ui/src/lib/components/po-field/po-upload/interfaces/po-upload-file-restriction.interface.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,40 @@
33
*
44
* @description
55
*
6-
* Os arquivos a serem selecionados podem ser restritos com base em regras pré definidas
7-
* para o seu tamanho e / ou extensão.
6+
* Interface que define as restrições dos arquivos a serem selecionados com base em regras predefinidas
7+
* para o seu tamanho, extensão e quantidade.
88
*/
99
export interface PoUploadFileRestrictions {
1010

1111
/**
12-
* Tamanho minimo do arquivo a ser enviado ao servidor. Deve ser informado o valor em bytes.
13-
* Por padrão o valor é 0.
12+
* Extensões permitidas de arquivos que serão enviados ao servidor, devendo ser informada uma coleção de extensões, por exemplo:
13+
* ```
14+
* allowedExtensions = ['.png', '.jpg', '.pdf'];
15+
* ```
1416
*/
15-
minFileSize?: number;
17+
allowedExtensions?: Array<string>;
1618

1719
/**
1820
* Quantidade máxima de arquivos para o *upload*.
19-
* Este valor deve ser maior que zero, caso contrário será desconsiderado e a funcionalidade não será habilitada.
2021
*
21-
* > Esta propriedade somente será válida se a propriedade `p-multiple` estiver habilitada.
22+
* > Esta propriedade será válida somente se a propriedade `p-multiple` estiver habilitada e seu valor for maior do que zero.
2223
*/
2324
maxFiles?: number;
2425

2526
/**
26-
* Tamanho máximo do arquivo a ser enviado ao servidor. Deve ser informado o valor em bytes,
27-
* Por exemplo: 31457280 (30MB).
27+
* Tamanho máximo do arquivo a ser enviado ao servidor.
28+
*
29+
* Deve ser informado um valor em *bytes*, por exemplo: `31457280` (30MB).
30+
*
31+
* > Por padrão o valor é `30 MB`.
2832
*/
2933
maxFileSize?: number;
3034

3135
/**
32-
* Extensões permitidas a serem enviadas ao servidor, deve ser informado uma coleção de extensões.
33-
* Exemplo de valores válidos: ['.png', '.jpg', '.pdf'].
36+
* Tamanho mínimo em *bytes* do arquivo que será enviado ao servidor.
37+
*
38+
* > Por padrão o valor é `0`.
3439
*/
35-
allowedExtensions?: Array<string>;
40+
minFileSize?: number;
41+
3642
}

projects/ui/src/lib/components/po-field/po-upload/po-upload-base.component.spec.ts

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class PoUploadComponent extends PoUploadBaseComponent {
2626
constructor(uploadService: PoUploadService) {
2727
super(uploadService);
2828
}
29+
30+
sendFeedback() {}
2931
}
3032

3133
describe('PoUploadBaseComponent:', () => {
@@ -291,6 +293,32 @@ describe('PoUploadBaseComponent:', () => {
291293
expect(component['parseFiles'](<any>files)).toEqual(<any>files);
292294
});
293295

296+
it('should set `quantityNotAllowed` with `filesLength - fileRestrictions.maxFiles` if `isExceededFileLimit` returns `true`', () => {
297+
298+
const files = [
299+
{ name: 'file1' },
300+
{ name: 'file2' },
301+
{ name: 'file3' }
302+
];
303+
304+
component.fileRestrictions = { maxFiles: 2 };
305+
306+
spyOn(component, <any>'isExceededFileLimit').and.returnValue(true);
307+
308+
component['parseFiles'](<any>files);
309+
310+
expect(component['quantityNotAllowed']).toBe(1);
311+
});
312+
313+
it('should call `sendFeedback`', () => {
314+
315+
spyOn(component, 'sendFeedback');
316+
317+
component['parseFiles'](<any>[{ name: 'file1' }]);
318+
319+
expect(component.sendFeedback).toHaveBeenCalled();
320+
});
321+
294322
});
295323

296324
it('checkRestrictions: should be check restrictions', () => {
@@ -323,6 +351,48 @@ describe('PoUploadBaseComponent:', () => {
323351
expect(restrictionsOk).toBeTruthy();
324352
});
325353

354+
it('checkRestrictions: should sum `sizeNotAllowed` if `file.size` is less than `minFileSize`', () => {
355+
356+
const mockFile = {
357+
size: 2
358+
};
359+
360+
component['sizeNotAllowed'] = 2;
361+
component['fileRestrictions'] = { minFileSize: 3 };
362+
363+
component['checkRestrictions'](<any>mockFile);
364+
365+
expect(component['sizeNotAllowed']).toBe(3);
366+
});
367+
368+
it('checkRestrictions: should sum `sizeNotAllowed` if `file.size` is greater than `maxFileSize`', () => {
369+
370+
const mockFile = {
371+
size: 3
372+
};
373+
374+
component['sizeNotAllowed'] = 2;
375+
component['fileRestrictions'] = { maxFileSize: 2 };
376+
377+
component['checkRestrictions'](<any>mockFile);
378+
379+
expect(component['sizeNotAllowed']).toBe(3);
380+
});
381+
382+
it('checkRestrictions: shouldn`t sum `sizeNotAllowed` if `isAcceptSize` is `true`', () => {
383+
384+
const mockFile = {
385+
size: 2
386+
};
387+
388+
component['sizeNotAllowed'] = 2;
389+
component['fileRestrictions'] = { maxFileSize: 2 };
390+
391+
component['checkRestrictions'](<any>mockFile);
392+
393+
expect(component['sizeNotAllowed']).toBe(2);
394+
});
395+
326396
it('existsFileSameName: ', () => {
327397
expect(component['existsFileSameName'](file, [file])).toBeTruthy();
328398
});
@@ -377,6 +447,36 @@ describe('PoUploadBaseComponent:', () => {
377447
expect(files.splice).not.toHaveBeenCalled();
378448
});
379449

450+
describe('initRestrictions:', () => {
451+
452+
it('should return falsy if restrictions is undefined', () => {
453+
const restrictions = undefined;
454+
455+
expect(component['initRestrictions'](restrictions)).toBeFalsy();
456+
});
457+
458+
it('should return default `minFileSize`', () => {
459+
const restrictions = { maxFileSize: 1 };
460+
const expectedResult = { maxFileSize: 1, minFileSize: 0 };
461+
462+
expect(component['initRestrictions'](restrictions)).toEqual(expectedResult);
463+
});
464+
465+
it('should return default `maxFileSize`', () => {
466+
const restrictions = { minFileSize: 2 };
467+
const expectedResult = { minFileSize: 2, maxFileSize: 31457280 };
468+
469+
expect(component['initRestrictions'](restrictions)).toEqual(expectedResult);
470+
});
471+
472+
it('should concat default values with file restrictions', () => {
473+
const restrictions = { allowedExtensions: ['pdf'], maxFiles: 2 };
474+
const expectedResult = { ...restrictions, minFileSize: 0, maxFileSize: 31457280 };
475+
476+
expect(component['initRestrictions'](restrictions)).toEqual(expectedResult);
477+
});
478+
});
479+
380480
it('isAllowedExtension: should allowed extensions', () => {
381481
const isAllowed = component['isAllowedExtension']('.pdf', ['.txt', '.PDF']);
382482
expect(isAllowed).toBeTruthy();
@@ -448,6 +548,33 @@ describe('PoUploadBaseComponent:', () => {
448548
expectPropertiesValues(component, 'disabled', invalidValues, false);
449549
});
450550

551+
it('fileRestrictions: should set `fileRestrictions` calling `initRestrictions` and call `setAllowedExtensions`', () => {
552+
553+
const restrictions = { minFileSize: 2 };
554+
const expectedResult = { minFileSize: 2, maxFileSize: 31457280 };
555+
556+
spyOn(component, <any>'initRestrictions').and.callThrough();
557+
spyOn(component, <any>'setAllowedExtensions');
558+
559+
component.fileRestrictions = restrictions;
560+
561+
expect(component.fileRestrictions).toEqual(expectedResult);
562+
expect(component['initRestrictions']).toHaveBeenCalled();
563+
expect(component['setAllowedExtensions']).toHaveBeenCalled();
564+
});
565+
566+
it('hideRestrictionsInfo: should set `hideRestrictionsInfo` with valid values', () => {
567+
const validValues = ['', true, 1, [], {}, 'true'];
568+
569+
expectPropertiesValues(component, 'hideRestrictionsInfo', validValues, true);
570+
});
571+
572+
it('hideRestrictionsInfo: should set `hideRestrictionsInfo` to false with invalid values', () => {
573+
const invalidValues = [null, undefined, NaN, false, 0, 'false', 'teste'];
574+
575+
expectPropertiesValues(component, 'hideRestrictionsInfo', invalidValues, false);
576+
});
577+
451578
it('p-literals: should be in portuguese if browser is setted with an unsupported language', () => {
452579
spyOn(utilsFunctions, <any>'browserLanguage').and.returnValue('ru');
453580

0 commit comments

Comments
 (0)