diff --git a/projects/ion/src/core/bn-table/bn-table.spec.ts b/projects/ion/src/core/bn-table/bn-table.spec.ts index de82c64fb..16309fd3f 100644 --- a/projects/ion/src/core/bn-table/bn-table.spec.ts +++ b/projects/ion/src/core/bn-table/bn-table.spec.ts @@ -1,4 +1,4 @@ -import { Observable, of } from 'rxjs'; +import { Observable, of, throwError } from 'rxjs'; import BnTable, { IBnTable } from './bn-table'; import { BnService, IResponse } from '../api/http.interfaces'; import { SmartTableEvent } from '../../public-api'; @@ -26,6 +26,14 @@ class MockEmptyService implements BnService { } } +// Defina um erro de exemplo +const mockError = new Error('Request Error'); +class MockServiceError implements BnService { + list(): Observable> { + return throwError(mockError); + } +} + describe('BnTable', () => { let bnTable: BnTable; const mockService: MockService = new MockService(); @@ -281,3 +289,20 @@ describe('BnTable', () => { }); }); }); + +describe('BnTable Error', () => { + let bnTable: BnTable; + const mockServiceWithError: MockServiceError = new MockServiceError(); + + it('should stop loading when receive an error', () => { + const config: IBnTable = { + service: mockServiceWithError, + tableConfig: { + columns: [{ label: 'Name', key: 'name' }], + actions: [{ label: 'Remove', icon: 'trash' }], + }, + }; + bnTable = new BnTable({ ...config }); + expect(bnTable.configTable.loading).toBeFalsy(); + }); +}); diff --git a/projects/ion/src/core/bn-table/bn-table.ts b/projects/ion/src/core/bn-table/bn-table.ts index 26042e72c..b7a019d47 100644 --- a/projects/ion/src/core/bn-table/bn-table.ts +++ b/projects/ion/src/core/bn-table/bn-table.ts @@ -1,5 +1,5 @@ import { forkJoin, of } from 'rxjs'; -import { take } from 'rxjs/operators'; +import { finalize, take } from 'rxjs/operators'; import { LIST_OF_PAGE_OPTIONS } from '../../lib/pagination/pagination.component'; import { ConfigTable, EventTable } from '../../lib/table/utilsTable'; import { @@ -95,34 +95,34 @@ export default class BnTable { .list({ ...this.payload, total: false }) .pipe(take(1)); - forkJoin([totalRequest$, dataRequest$]).subscribe( - (response) => { - this.configTable.loading = false; - - const [totalResponse, dataResponse]: [ - IResponse, - IResponse - ] = response; - - if (totalResponse && totalResponse.total !== null) { - this.configTable.pagination = { - ...this.configTable.pagination, - total: totalResponse.total || 0, - }; - } - - this.configTable.data = dataResponse.dados || dataResponse.data || []; - - if (this.formatData) { - this.configTable.data = this.formatData(this.configTable.data); + forkJoin([totalRequest$, dataRequest$]) + .pipe(finalize(() => (this.configTable.loading = false))) + .subscribe( + (response) => { + const [totalResponse, dataResponse]: [ + IResponse, + IResponse + ] = response; + + if (totalResponse && totalResponse.total !== null) { + this.configTable.pagination = { + ...this.configTable.pagination, + total: totalResponse.total || 0, + }; + } + + this.configTable.data = dataResponse.dados || dataResponse.data || []; + + if (this.formatData) { + this.configTable.data = this.formatData(this.configTable.data); + } + }, + () => { + // TODO: add notification service + // const msg: string = error.msg || error.error.msg; + // this.notify.error('Erro', msg); } - }, - (error) => { - // TODO: add notification service - // const msg: string = error.msg || error.error.msg; - // this.notify.error('Erro', msg); - } - ); + ); } events(event: SmartTableEvent): void {