Skip to content

Commit 2eb1d5c

Browse files
jhonyeduardojhosefmarks
authored andcommitted
fix(combo): adiciona tratamento para retorno de erro HTTP na pesquisa
Ao ocorrer um erro na pequisa, utilizando serviço, o componente ficava no estado de carregando e apresentava notificações de erro. Fixes DTHFUI-3653
1 parent c3fe5f7 commit 2eb1d5c

File tree

4 files changed

+114
-16
lines changed

4 files changed

+114
-16
lines changed

projects/ui/src/lib/components/po-field/po-combo/po-combo-filter.service.spec.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ describe('PoComboFilterService ', () => {
9797
});
9898

9999
describe('Methods:', () => {
100+
it('getFilteredData: should contains X-PO-NO-MESSAGE in headers request', done => {
101+
const urlWithParams = 'http://mockurl.com?filter=test';
102+
const parsedParams: Array<PoComboOption> = [{ label: 'value1', value: 'value1' }];
103+
104+
spyOn(comboService, <any>'parseToArrayComboOption').and.returnValue(parsedParams);
105+
spyOnProperty(comboService, 'url', 'get').and.returnValue('http://mockurl.com');
106+
107+
comboService.getFilteredData({ value: 'test' }).subscribe(response => {
108+
expect(response).toEqual(parsedParams);
109+
done();
110+
});
111+
112+
const req = httpMock.expectOne((request: HttpRequest<any>) => request.urlWithParams === urlWithParams);
113+
expect(req.request.headers.get('X-PO-No-Message')).toBe('true');
114+
115+
req.flush({});
116+
});
117+
100118
it('getFilteredData: should concatenate url with filter params', done => {
101119
const urlWithParams = 'http://mockurl.com?param1=value1&param2=value2&filter=test';
102120
const parsedParams: Array<PoComboOption> = [{ label: 'value1', value: 'value1' }];
@@ -183,6 +201,25 @@ describe('PoComboFilterService ', () => {
183201
httpMock.expectOne(`http://mockurl.com?filter=`).flush({ items });
184202
});
185203

204+
it('getObjectByValue: should contains X-PO-NO-MESSAGE in headers request', done => {
205+
const filteredObject: PoComboOption = { label: 'value1', value: 'value1' };
206+
const param = 'angular';
207+
const urlWithParams = 'http://mockurl.com/angular';
208+
209+
spyOn(comboService, <any>'parseToComboOption').and.returnValue(filteredObject);
210+
spyOnProperty(comboService, 'url', 'get').and.returnValue('http://mockurl.com');
211+
212+
comboService.getObjectByValue(param).subscribe(response => {
213+
expect(response).toEqual(filteredObject);
214+
done();
215+
});
216+
217+
const req = httpMock.expectOne((request: HttpRequest<any>) => request.urlWithParams === urlWithParams);
218+
expect(req.request.headers.get('X-PO-No-Message')).toBe('true');
219+
220+
req.flush({});
221+
});
222+
186223
it('getObjectByValue: should add filter params', done => {
187224
const filteredObject: PoComboOption = { label: 'value1', value: 'value1' };
188225
const param = 'angular';

projects/ui/src/lib/components/po-field/po-combo/po-combo-filter.service.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpClient } from '@angular/common/http';
1+
import { HttpClient, HttpHeaders } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
33

44
import { Observable } from 'rxjs';
@@ -25,6 +25,10 @@ export class PoComboFilterService implements PoComboFilter {
2525

2626
private messages = [];
2727

28+
readonly headers: HttpHeaders = new HttpHeaders({
29+
'X-PO-No-Message': 'true'
30+
});
31+
2832
get url(): string {
2933
return this._url;
3034
}
@@ -38,15 +42,15 @@ export class PoComboFilterService implements PoComboFilter {
3842
const params = { ...filterParamsValidated, filter: value };
3943

4044
return this.http
41-
.get(`${this.url}`, { responseType: 'json', params: params })
45+
.get(`${this.url}`, { responseType: 'json', params, headers: this.headers })
4246
.pipe(map((response: PoResponse) => this.parseToArrayComboOption(response.items)));
4347
}
4448

4549
getObjectByValue(value: string | number, filterParams?: any): Observable<PoComboOption> {
4650
const filterParamsValidated = validateObjectType(filterParams);
4751

4852
return this.http
49-
.get(`${this.url}/${value}`, { params: filterParamsValidated })
53+
.get(`${this.url}/${value}`, { params: filterParamsValidated, headers: this.headers })
5054
.pipe(map(item => this.parseToComboOption(item)));
5155
}
5256

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

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testin
33
import { HttpClient, HttpHandler } from '@angular/common/http';
44
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
55

6-
import { Observable } from 'rxjs';
6+
import { Observable, throwError } from 'rxjs';
77

88
import { changeBrowserInnerWidth, configureTestSuite } from './../../../util-test/util-expect.spec';
99

@@ -1222,12 +1222,9 @@ describe('PoComboComponent:', () => {
12221222
expect(component.isServerSearching).toBe(true);
12231223
});
12241224

1225-
it('isServerSearching: should call `removeListeners` if isServerSearching is false', () => {
1226-
const spyRemoveListeners = spyOn(component, <any>'removeListeners');
1227-
1225+
it('isServerSearching: should set isServerSearching with false', () => {
12281226
component.isServerSearching = false;
12291227

1230-
expect(spyRemoveListeners).toHaveBeenCalled();
12311228
expect(component.isServerSearching).toBe(false);
12321229
});
12331230

@@ -1718,6 +1715,35 @@ describe('PoComboComponent - with service:', () => {
17181715
expect(fakeThis.service.getFilteredData).toHaveBeenCalledWith(param, filterParams);
17191716
});
17201717

1718+
it('applyFilter: should set isServerSearching and call controlComboVisibility with false if getFilteredData throw error', () => {
1719+
const value = 'test';
1720+
const error = { 'error': { 'message': 'message' } };
1721+
1722+
spyOn(component, 'controlComboVisibility');
1723+
spyOn(component.service, 'getFilteredData').and.returnValue(throwError(error));
1724+
1725+
component.applyFilter(value);
1726+
fixture.detectChanges();
1727+
1728+
expect(component.service.getFilteredData).toHaveBeenCalled();
1729+
expect(component.isServerSearching).toBe(false);
1730+
expect(component.visibleOptions).toEqual([]);
1731+
expect(component.controlComboVisibility).toHaveBeenCalledWith(true);
1732+
});
1733+
1734+
it('getObjectByValue: should call updateSelectedValue with null if getObjectByValue throw error', () => {
1735+
const value = 'XPTO';
1736+
const error = { 'error': { 'message': 'message' } };
1737+
1738+
spyOn(component.service, 'getObjectByValue').and.returnValue(throwError(error));
1739+
spyOn(component, 'updateSelectedValue');
1740+
1741+
component.getObjectByValue(value);
1742+
1743+
expect(component.updateSelectedValue).toHaveBeenCalledWith(null);
1744+
expect(component.service.getObjectByValue).toHaveBeenCalled();
1745+
});
1746+
17211747
it(`getObjectByValue: should call PoComboFilterService.getObjectByValue() with param and filterParam if
17221748
selectedValue is invalid`, () => {
17231749
const filterParams = 'filter';
@@ -1921,6 +1947,25 @@ describe('PoComboComponent - with service:', () => {
19211947
expect(spyUpdateComboList).toHaveBeenCalledWith([...component.cacheOptions]);
19221948
});
19231949
});
1950+
1951+
describe('Templates:', () => {
1952+
it('should display `.po-combo-container-no-data` if error in filtered data', () => {
1953+
const value = 'test';
1954+
const error = { 'error': { 'message': 'message' } };
1955+
1956+
component.options = [
1957+
{ value: 1, label: 'John Doe' },
1958+
{ value: 2, label: 'Jane Doe' }
1959+
];
1960+
1961+
spyOn(component.service, 'getFilteredData').and.returnValue(throwError(error));
1962+
1963+
component.applyFilter(value);
1964+
fixture.detectChanges();
1965+
1966+
expect(fixture.debugElement.query(By.css('.po-combo-container-no-data'))).toBeTruthy();
1967+
});
1968+
});
19241969
});
19251970

19261971
function getFakeService(item): any {

projects/ui/src/lib/components/po-field/po-combo/po-combo.component.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ export class PoComboComponent extends PoComboBaseComponent implements AfterViewI
146146
this.initializeListeners();
147147
} else {
148148
this._isServerSearching = value;
149-
150-
this.removeListeners();
151149
}
152150
}
153151

@@ -346,9 +344,10 @@ export class PoComboComponent extends PoComboBaseComponent implements AfterViewI
346344

347345
const param = { property: this.fieldLabel, value };
348346

349-
this.filterSubscription = this.service
350-
.getFilteredData(param, this.filterParams)
351-
.subscribe(items => this.setOptionsByApplyFilter(value, items));
347+
this.filterSubscription = this.service.getFilteredData(param, this.filterParams).subscribe(
348+
items => this.setOptionsByApplyFilter(value, items),
349+
error => this.onErrorFilteredData()
350+
);
352351
}
353352

354353
setOptionsByApplyFilter(value, items) {
@@ -373,9 +372,10 @@ export class PoComboComponent extends PoComboBaseComponent implements AfterViewI
373372
if (!this.selectedValue) {
374373
this.isProcessingGetObjectByValue = true;
375374

376-
this.getSubscription = this.service
377-
.getObjectByValue(value, this.filterParams)
378-
.subscribe(item => this.updateOptionByFilteredValue(item));
375+
this.getSubscription = this.service.getObjectByValue(value, this.filterParams).subscribe(
376+
item => this.updateOptionByFilteredValue(item),
377+
error => this.onErrorGetObjectByValue()
378+
);
379379
}
380380
}
381381

@@ -608,6 +608,18 @@ export class PoComboComponent extends PoComboBaseComponent implements AfterViewI
608608
window.addEventListener('scroll', this.onScroll, true);
609609
}
610610

611+
private onErrorGetObjectByValue() {
612+
this.updateOptionByFilteredValue(null);
613+
}
614+
615+
private onErrorFilteredData() {
616+
this.isServerSearching = false;
617+
618+
this.updateComboList([]);
619+
620+
this.controlComboVisibility(true);
621+
}
622+
611623
private onScroll = (): void => {
612624
this.adjustContainerPosition();
613625
};

0 commit comments

Comments
 (0)