Skip to content

Commit c06e7c9

Browse files
jhonyeduardosamir-ayoub
authored andcommitted
feat(progress): adiciona evento de tentar novamente
Possibilita informar um evento para que o mesmo seja disparado quando o progresso estiver com status de erro. Fixes DTHFUI-1863
1 parent a011788 commit c06e7c9

File tree

7 files changed

+206
-60
lines changed

7 files changed

+206
-60
lines changed

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

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { expectPropertiesValues } from '../../util-test/util-expect.spec';
2-
import { Observable } from 'rxjs';
32

43
import { PoProgressBaseComponent } from './po-progress-base.component';
5-
import { PoProgressStatus } from './enums/po-progress-status.enum';
64

75
describe('PoProgressBaseComponent:', () => {
86

@@ -43,52 +41,29 @@ describe('PoProgressBaseComponent:', () => {
4341

4442
describe('Methods:', () => {
4543

46-
it(`emitCancellation: should call 'emit' with 'status'`, () => {
47-
component.status = PoProgressStatus.Success;
48-
49-
spyOn(component.cancel, 'emit');
50-
51-
component.emitCancellation();
52-
53-
expect(component.cancel.emit).toHaveBeenCalledWith(component.status);
54-
});
55-
56-
it(`isCancel: should be 'true' if 'cancel' contain 'function'`, () => {
57-
const cancelFunction = () => {};
58-
component.cancel.observers.push(<any>[new Observable(cancelFunction)]);
59-
60-
expect(component.isCancel()).toBeTruthy();
61-
});
62-
63-
it(`isCancel: should be 'false' if 'cancel' does not contain 'function'`, () => {
64-
component.cancel.observers.length = 0;
65-
66-
expect(component.isCancel()).toBeFalsy();
67-
});
68-
6944
it(`isProgressRangeValue: should be 'true' if 'value' is greater than 0`, () => {
7045
const value = 20;
71-
expect(component.isProgressRangeValue(value)).toBeTruthy();
46+
expect(component['isProgressRangeValue'](value)).toBeTruthy();
7247
});
7348

7449
it(`isProgressRangeValue: should be 'true' if 'value' equals 0`, () => {
7550
const value = 0;
76-
expect(component.isProgressRangeValue(value)).toBeTruthy();
51+
expect(component['isProgressRangeValue'](value)).toBeTruthy();
7752
});
7853

7954
it(`isProgressRangeValue: should be 'true' if 'value' equals 100`, () => {
8055
const value = 100;
81-
expect(component.isProgressRangeValue(value)).toBeTruthy();
56+
expect(component['isProgressRangeValue'](value)).toBeTruthy();
8257
});
8358

8459
it(`isProgressRangeValue: should be 'false' if 'value' is less than 0`, () => {
8560
const value = -2;
86-
expect(component.isProgressRangeValue(value)).toBeFalsy();
61+
expect(component['isProgressRangeValue'](value)).toBeFalsy();
8762
});
8863

8964
it(`isProgressRangeValue: should be 'false' if 'value' is greater than 100`, () => {
9065
const value = 120;
91-
expect(component.isProgressRangeValue(value)).toBeFalsy();
66+
expect(component['isProgressRangeValue'](value)).toBeFalsy();
9267
});
9368

9469
});

projects/ui/src/lib/components/po-progress/po-progress-base.component.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,6 @@ export class PoProgressBaseComponent {
1919
private _indeterminate?: boolean;
2020
private _value?: number = 0;
2121

22-
/**
23-
* @optional
24-
*
25-
* @description
26-
*
27-
* Evento que será disparado ao clicar no ícone de cancelamento ("x") na parte inferior da barra de progresso.
28-
*
29-
* Ao ser disparado, a função receberá como parâmetro o status atual da barra de progresso.
30-
*
31-
* > Se nenhuma função for passada para o evento, o ícone de cancelamento não será exibido.
32-
*/
33-
@Output('p-cancel') cancel: EventEmitter<any> = new EventEmitter();
34-
3522
/**
3623
* @optional
3724
*
@@ -116,15 +103,33 @@ export class PoProgressBaseComponent {
116103
return this._value;
117104
}
118105

119-
emitCancellation() {
120-
this.cancel.emit(this.status);
121-
}
106+
/**
107+
* @optional
108+
*
109+
* @description
110+
*
111+
* Evento que será disparado ao clicar no ícone de cancelamento ("x") na parte inferior da barra de progresso.
112+
*
113+
* Ao ser disparado, a função receberá como parâmetro o status atual da barra de progresso.
114+
*
115+
* > Se nenhuma função for passada para o evento ou a barra de progresso estiver com o status `PoProgressStatus.Success`,
116+
* o ícone de cancelamento não será exibido.
117+
*/
118+
@Output('p-cancel') cancel: EventEmitter<any> = new EventEmitter();
122119

123-
isCancel(): boolean {
124-
return !!this.cancel.observers.length;
125-
}
120+
/**
121+
* @optional
122+
*
123+
* @description
124+
*
125+
* Evento que será disparado ao clicar no ícone de tentar novamente na parte inferior da barra de progresso.
126+
*
127+
* > o ícone será exibido apenas se informar uma função neste evento e o status da barra de progresso for
128+
* `PoProgressStatus.Error`.
129+
*/
130+
@Output('p-retry') retry: EventEmitter<any> = new EventEmitter();
126131

127-
isProgressRangeValue(value: number): boolean {
132+
private isProgressRangeValue(value: number): boolean {
128133
return value >= poProgressMinValue && value <= poProgressMaxValue;
129134
}
130135

projects/ui/src/lib/components/po-progress/po-progress.component.html

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div class="po-progress" [ngClass]="statusClass">
22

3-
<label class="po-progress-description-mobile po-progress-description-text">
3+
<label *ngIf="text" class="po-progress-description-mobile po-progress-description-text">
44
{{ text }}
55
</label>
66

@@ -10,18 +10,23 @@
1010
[p-value]="value">
1111
</po-progress-bar>
1212

13-
<div class="po-progress-description">
13+
<div *ngIf="text" class="po-progress-description">
1414
<label class="po-progress-description-text">
1515
{{ text }}
1616
</label>
1717
</div>
1818

19-
<div class="po-progress-info">
20-
<span class="po-progress-info-icon po-icon {{ infoIcon }}"></span>
21-
<span class="po-progress-info-text">{{ info }}</span>
19+
<div *ngIf="isAllowProgressInfo" class="po-progress-info">
20+
<span *ngIf="infoIcon" class="po-progress-info-icon po-icon {{ infoIcon }}"></span>
21+
<span *ngIf="info" class="po-progress-info-text">{{ info }}</span>
2222

23-
<button *ngIf="isCancel()"
24-
class="po-progress-info-icon-close po-icon po-icon-close po-clickable"
23+
<button *ngIf="isAllowRetry"
24+
class="po-progress-info-icon-action po-icon po-icon-refresh po-clickable"
25+
(click)="emitRetry()">
26+
</button>
27+
28+
<button *ngIf="isAllowCancel"
29+
class="po-progress-info-icon-action po-icon po-icon-close po-clickable"
2530
(click)="emitCancellation()">
2631
</button>
2732

projects/ui/src/lib/components/po-progress/po-progress.component.spec.ts

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ describe('PoProgressComponent:', () => {
3333
expect(component instanceof PoProgressComponent).toBeTruthy();
3434
});
3535

36+
describe('Methods:', () => {
37+
38+
it(`emitCancellation: should call 'emit' with 'status'`, () => {
39+
component.status = PoProgressStatus.Success;
40+
41+
spyOn(component.cancel, 'emit');
42+
43+
component.emitCancellation();
44+
45+
expect(component.cancel.emit).toHaveBeenCalledWith(component.status);
46+
});
47+
48+
it(`emitRetry: should call 'emit'`, () => {
49+
component.status = PoProgressStatus.Success;
50+
51+
spyOn(component.retry, 'emit');
52+
53+
component.emitRetry();
54+
55+
expect(component.retry.emit).toHaveBeenCalled();
56+
});
57+
58+
});
59+
3660
describe('Properties:', () => {
3761

3862
it('statusClass: should return `po-progress-success` if `status` is `PoProgressStatus.Success`', () => {
@@ -65,6 +89,52 @@ describe('PoProgressComponent:', () => {
6589
expect(component.statusClass).toBe('po-progress-default');
6690
});
6791

92+
it(`isAllowCancel: should be 'true' if 'cancel' contain 'function' and status is PoProgressStatus.Default`, () => {
93+
const cancelFunction = () => {};
94+
component.cancel.observers.push(<any>[new Observable(cancelFunction)]);
95+
component.status = PoProgressStatus.Default;
96+
97+
expect(component.isAllowCancel).toBe(true);
98+
});
99+
100+
it(`isAllowCancel: should be 'false' if 'cancel' does not contain 'function' and status is PoProgressStatus.Success`, () => {
101+
component.cancel.observers.length = 0;
102+
component.status = PoProgressStatus.Success;
103+
104+
expect(component.isAllowCancel).toBe(false);
105+
});
106+
107+
it(`isAllowCancel: should be 'false' if 'cancel' contain 'function' and status is PoProgressStatus.Success`, () => {
108+
const cancelFunction = () => {};
109+
component.cancel.observers.push(<any>[new Observable(cancelFunction)]);
110+
component.status = PoProgressStatus.Success;
111+
112+
expect(component.isAllowCancel).toBe(false);
113+
});
114+
115+
it(`isAllowRetry: should be 'true' if 'retry' contain 'function' and status is PoProgressStatus.Error`, () => {
116+
const retryFunction = () => {};
117+
component.retry.observers.push(<any>[new Observable(retryFunction)]);
118+
component.status = PoProgressStatus.Error;
119+
120+
expect(component.isAllowRetry).toBe(true);
121+
});
122+
123+
it(`isAllowRetry: should be 'false' if 'retry' does not contain 'function' and status isn't PoProgressStatus.Error`, () => {
124+
component.retry.observers.length = 0;
125+
component.status = PoProgressStatus.Default;
126+
127+
expect(component.isAllowRetry).toBe(false);
128+
});
129+
130+
it(`isAllowRetry: should be 'false' if 'retry' contain 'function' and status isn't PoProgressStatus.Error`, () => {
131+
const retryFunction = () => {};
132+
component.retry.observers.push(<any>[new Observable(retryFunction)]);
133+
component.status = PoProgressStatus.Default;
134+
135+
expect(component.isAllowRetry).toBe(false);
136+
});
137+
68138
});
69139

70140
describe('Templates:', () => {
@@ -92,6 +162,16 @@ describe('PoProgressComponent:', () => {
92162
expect(infoIcon.classList).toContain('po-icon-agro-business');
93163
});
94164

165+
it('shouldn`t find `.po-progress-info-icon` if `infoIcon` is `undefined`', () => {
166+
component.infoIcon = undefined;
167+
168+
fixture.detectChanges();
169+
170+
const infoIcon = nativeElement.querySelector('.po-progress-info-icon');
171+
172+
expect(infoIcon).toBe(null);
173+
});
174+
95175
it('should contain `p-info` value', () => {
96176
component.info = 'test info';
97177

@@ -102,6 +182,16 @@ describe('PoProgressComponent:', () => {
102182
expect(info).toBe(component.info);
103183
});
104184

185+
it('shouldn`t find `.po-progress-info-text` if `info` is undefined ', () => {
186+
component.info = undefined;
187+
188+
fixture.detectChanges();
189+
190+
const info = nativeElement.querySelector('.po-progress-info-text');
191+
192+
expect(info).toBe(null);
193+
});
194+
105195
it('should contain `po-progress-default` if `status` is `default`', () => {
106196
component.status = PoProgressStatus.Default;
107197

@@ -182,6 +272,56 @@ describe('PoProgressComponent:', () => {
182272
expect(component.cancel.emit).toHaveBeenCalledWith(component.status);
183273
});
184274

275+
it('should emit retry with status if `retry` is clicked', () => {
276+
const retryFunction = () => {};
277+
278+
component.retry.observers.push(<any>[new Observable(retryFunction)]);
279+
component.status = PoProgressStatus.Error;
280+
281+
fixture.detectChanges();
282+
283+
spyOn(component.retry, 'emit');
284+
285+
nativeElement.querySelector('.po-icon-refresh').click();
286+
287+
expect(component.retry.emit).toHaveBeenCalled();
288+
});
289+
290+
it('shouldn`t find `.po-progress-description` and `.po-progress-description-mobile` if `text` is undefined', () => {
291+
component.text = undefined;
292+
293+
fixture.detectChanges();
294+
295+
const descriptionMobile = nativeElement.querySelector('.po-progress-description-mobile');
296+
const description = nativeElement.querySelector('.po-progress-description');
297+
298+
expect(descriptionMobile).toBe(null);
299+
expect(description).toBe(null);
300+
});
301+
302+
it('should find `.po-progress-info` if `info` is truthy', () => {
303+
component.info = 'filename.jpg';
304+
305+
fixture.detectChanges();
306+
307+
const progressInfo = nativeElement.querySelector('.po-progress-info');
308+
309+
expect(progressInfo).toBeTruthy();
310+
});
311+
312+
it('shouldn`t find `.po-progress-info` if `info`, `infoIcon`, `isAllowRetry` and `isAllowCancel` are falsy', () => {
313+
component.info = undefined;
314+
component.infoIcon = undefined;
315+
component.retry.observers.length = 0;
316+
component.cancel.observers.length = 0;
317+
318+
fixture.detectChanges();
319+
320+
const progressInfo = nativeElement.querySelector('.po-progress-info');
321+
322+
expect(progressInfo).toBe(null);
323+
});
324+
185325
});
186326

187327
});

projects/ui/src/lib/components/po-progress/po-progress.component.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ import { PoProgressStatus } from './enums/po-progress-status.enum';
2929
})
3030
export class PoProgressComponent extends PoProgressBaseComponent {
3131

32+
get isAllowCancel(): boolean {
33+
return !!this.cancel.observers.length && this.status !== PoProgressStatus.Success;
34+
}
35+
36+
get isAllowProgressInfo(): boolean {
37+
return !!(this.info || this.infoIcon || this.isAllowCancel || this.isAllowRetry);
38+
}
39+
40+
get isAllowRetry(): boolean {
41+
return !!this.retry.observers.length && this.status === PoProgressStatus.Error;
42+
}
43+
3244
get statusClass(): string {
3345

3446
if (this.status === PoProgressStatus.Success) {
@@ -42,4 +54,12 @@ export class PoProgressComponent extends PoProgressBaseComponent {
4254
return 'po-progress-default';
4355
}
4456

57+
emitCancellation() {
58+
this.cancel.emit(this.status);
59+
}
60+
61+
emitRetry() {
62+
this.retry.emit();
63+
}
64+
4565
}

projects/ui/src/lib/components/po-progress/samples/sample-po-progress-labs/sample-po-progress-labs.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
[p-status]="status"
66
[p-text]="text"
77
[p-value]="value"
8-
(p-cancel)="cancel()">
8+
(p-cancel)="onEvent('p-cancel')"
9+
(p-retry)="onEvent('p-retry')">
910
</po-progress>
1011

1112
<po-divider></po-divider>

projects/ui/src/lib/components/po-progress/samples/sample-po-progress-labs/sample-po-progress-labs.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export class SamplePoProgressLabsComponent implements OnInit {
3737
this.restore();
3838
}
3939

40-
cancel() {
41-
this.event = 'cancel';
40+
onEvent(event) {
41+
this.event = event;
4242
}
4343

4444
restore() {

0 commit comments

Comments
 (0)