diff --git a/apps/datahub/src/app/home/news-page/key-figures/key-figures.component.spec.ts b/apps/datahub/src/app/home/news-page/key-figures/key-figures.component.spec.ts index a91879ff46..bf900a05ad 100644 --- a/apps/datahub/src/app/home/news-page/key-figures/key-figures.component.spec.ts +++ b/apps/datahub/src/app/home/news-page/key-figures/key-figures.component.spec.ts @@ -1,6 +1,6 @@ import { ComponentFixture, TestBed } from '@angular/core/testing' import { KeyFiguresComponent } from './key-figures.component' -import { of } from 'rxjs' +import { BehaviorSubject, of } from 'rxjs' import { RecordsService } from '@geonetwork-ui/feature/catalog' import { TranslateModule } from '@ngx-translate/core' import { NO_ERRORS_SCHEMA } from '@angular/core' @@ -8,8 +8,9 @@ import { RouterTestingModule } from '@angular/router/testing' import { By } from '@angular/platform-browser' import { OrganizationsServiceInterface } from '@geonetwork-ui/common/domain/organizations.service.interface' +const recordsCount$ = new BehaviorSubject(1234) class RecordsServiceMock { - recordsCount$ = of(1234) + recordsCount$ = recordsCount$ } class OrganisationsServiceMock { @@ -58,6 +59,16 @@ describe('KeyFiguresComponent', () => { it('emits the records count', () => { expect(values[1]).toBe(1234) }) + describe('when the request does not behave as expected', () => { + beforeEach(() => { + recordsCount$.error('blargz') + }) + it('emits -', () => { + let count + component.recordsCount$.subscribe((v) => (count = v)) + expect(count).toBe('-') + }) + }) }) describe('orgsCount$', () => { diff --git a/apps/datahub/src/app/home/news-page/key-figures/key-figures.component.ts b/apps/datahub/src/app/home/news-page/key-figures/key-figures.component.ts index bee90f8cfe..c4643d73a4 100644 --- a/apps/datahub/src/app/home/news-page/key-figures/key-figures.component.ts +++ b/apps/datahub/src/app/home/news-page/key-figures/key-figures.component.ts @@ -1,10 +1,11 @@ import { ChangeDetectionStrategy, Component } from '@angular/core' -import { startWith } from 'rxjs/operators' +import { catchError, startWith } from 'rxjs/operators' import { RecordsService } from '@geonetwork-ui/feature/catalog' import { ROUTER_ROUTE_SEARCH } from '@geonetwork-ui/feature/router' import { ROUTER_ROUTE_ORGANISATIONS } from '../../../router/constants' import { OrganizationsServiceInterface } from '@geonetwork-ui/common/domain/organizations.service.interface' import { marker } from '@biesbjerg/ngx-translate-extract-marker' +import { of } from 'rxjs' marker('catalog.figures.datasets') marker('catalog.figures.organisations') @@ -16,7 +17,10 @@ marker('catalog.figures.organisations') changeDetection: ChangeDetectionStrategy.OnPush, }) export class KeyFiguresComponent { - recordsCount$ = this.catalogRecords.recordsCount$.pipe(startWith('-')) + recordsCount$ = this.catalogRecords.recordsCount$.pipe( + startWith('-'), + catchError(() => of('-')) + ) orgsCount$ = this.catalogOrgs.organisationsCount$.pipe(startWith('-')) ROUTE_SEARCH = `/${ROUTER_ROUTE_SEARCH}` ROUTE_ORGANISATIONS = `/${ROUTER_ROUTE_ORGANISATIONS}` diff --git a/apps/webcomponents/src/app/components/gn-figure-datasets/gn-figure-datasets.component.ts b/apps/webcomponents/src/app/components/gn-figure-datasets/gn-figure-datasets.component.ts index 046aaeae89..aaeaf65cb7 100644 --- a/apps/webcomponents/src/app/components/gn-figure-datasets/gn-figure-datasets.component.ts +++ b/apps/webcomponents/src/app/components/gn-figure-datasets/gn-figure-datasets.component.ts @@ -7,8 +7,8 @@ import { } from '@angular/core' import { BaseComponent } from '../base.component' import { RecordsService } from '@geonetwork-ui/feature/catalog' -import { startWith } from 'rxjs/operators' -import { Observable } from 'rxjs' +import { catchError, startWith } from 'rxjs/operators' +import { Observable, of } from 'rxjs' import { SearchFacade } from '@geonetwork-ui/feature/search' @Component({ @@ -26,7 +26,10 @@ export class GnFigureDatasetsComponent extends BaseComponent { constructor(injector: Injector, private changeDetector: ChangeDetectorRef) { super(injector) this.catalogRecords = injector.get(RecordsService) - this.recordsCount$ = this.catalogRecords.recordsCount$.pipe(startWith('-')) + this.recordsCount$ = this.catalogRecords.recordsCount$.pipe( + startWith('-'), + catchError(() => of('-')) + ) } init(): void { diff --git a/libs/feature/catalog/src/lib/records/records.service.spec.ts b/libs/feature/catalog/src/lib/records/records.service.spec.ts index be91cb1cb2..fff21c5376 100644 --- a/libs/feature/catalog/src/lib/records/records.service.spec.ts +++ b/libs/feature/catalog/src/lib/records/records.service.spec.ts @@ -1,5 +1,5 @@ import { RecordsService } from './records.service' -import { of, throwError } from 'rxjs' +import { of } from 'rxjs' import { RecordsRepositoryInterface } from '@geonetwork-ui/common/domain/repository/records-repository.interface' class RecordsRepositoryMock { @@ -33,17 +33,5 @@ describe('RecordsService', () => { expect(repository.getMatchesCount).toHaveBeenCalledTimes(1) }) }) - - describe('when the request does not behave as expected', () => { - beforeEach(() => { - repository.getMatchesCount = () => throwError(() => 'blargz') - service = new RecordsService(repository) // create a new service to enable the changed repository behaviour - }) - it('emits 0', () => { - let count - service.recordsCount$.subscribe((v) => (count = v)) - expect(count).toBe(0) - }) - }) }) }) diff --git a/libs/feature/catalog/src/lib/records/records.service.ts b/libs/feature/catalog/src/lib/records/records.service.ts index 8c1fa8cd22..2d332f1155 100644 --- a/libs/feature/catalog/src/lib/records/records.service.ts +++ b/libs/feature/catalog/src/lib/records/records.service.ts @@ -1,16 +1,15 @@ import { Injectable } from '@angular/core' import { Observable, of, switchMap } from 'rxjs' -import { catchError, shareReplay } from 'rxjs/operators' +import { shareReplay } from 'rxjs/operators' import { RecordsRepositoryInterface } from '@geonetwork-ui/common/domain/repository/records-repository.interface' @Injectable({ providedIn: 'root', }) export class RecordsService { - recordsCount$: Observable = of(0).pipe( + recordsCount$: Observable = of(true).pipe( switchMap(() => this.recordsRepository.getMatchesCount({})), - shareReplay(1), - catchError(() => of(0)) + shareReplay(1) ) constructor(private recordsRepository: RecordsRepositoryInterface) {}