Skip to content

Commit

Permalink
refactor: recordsCount$ should not catch the error
Browse files Browse the repository at this point in the history
  • Loading branch information
fgravin committed Jun 26, 2024
1 parent 1f253f9 commit 4f1441a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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'
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 {
Expand Down Expand Up @@ -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$', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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 {
Expand Down
14 changes: 1 addition & 13 deletions libs/feature/catalog/src/lib/records/records.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
})
})
})
})
7 changes: 3 additions & 4 deletions libs/feature/catalog/src/lib/records/records.service.ts
Original file line number Diff line number Diff line change
@@ -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<number> = of(0).pipe(
recordsCount$: Observable<number> = of(true).pipe(

This comment has been minimized.

Copy link
@rcaplier

rcaplier Jun 26, 2024

Collaborator

Seems like EMPTY.pipe(....... is what you're looking for

This comment has been minimized.

Copy link
@fgravin

fgravin Jun 26, 2024

Author Member

Actually it does not work cause EMPTY completes the observable.
I guess that it's already complete when the template subscribes to it.

switchMap(() => this.recordsRepository.getMatchesCount({})),
shareReplay(1),
catchError(() => of(0))
shareReplay(1)
)

constructor(private recordsRepository: RecordsRepositoryInterface) {}
Expand Down

0 comments on commit 4f1441a

Please sign in to comment.