From 9fd3ff848e9b39b3346b401d054a2e77f136d1c8 Mon Sep 17 00:00:00 2001 From: Tobias Kohr Date: Wed, 6 Dec 2023 15:50:02 +0100 Subject: [PATCH] test(gravatar): fix unit tests --- .../search-header.component.spec.ts | 6 ++-- .../src/lib/gn4/auth/gravatar.service.spec.ts | 34 ++++++++++++------- .../gn4/platform/gn4-platform.service.spec.ts | 4 +-- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/apps/metadata-editor/src/app/dashboard/search-header/search-header.component.spec.ts b/apps/metadata-editor/src/app/dashboard/search-header/search-header.component.spec.ts index d50f0279c0..5633823269 100644 --- a/apps/metadata-editor/src/app/dashboard/search-header/search-header.component.spec.ts +++ b/apps/metadata-editor/src/app/dashboard/search-header/search-header.component.spec.ts @@ -6,7 +6,7 @@ import { import { ComponentFixture, TestBed } from '@angular/core/testing' import { SearchHeaderComponent } from './search-header.component' -import { BehaviorSubject } from 'rxjs' +import { BehaviorSubject, of } from 'rxjs' import { USER_FIXTURE } from '@geonetwork-ui/common/fixtures' import { StoreModule } from '@ngrx/store' import { EffectsModule } from '@ngrx/effects' @@ -16,8 +16,8 @@ import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform. import { AvatarServiceInterface } from '@geonetwork-ui/api/repository/gn4' class AvatarServiceInterfaceMock { - placeholder = 'http://placeholder.com' - getProfileIcon = (hash: string) => `${hash}` + getPlaceholder = () => of('http://placeholder.com') + getProfileIcon = (hash: string) => of(`${hash}`) } const me$ = new BehaviorSubject(USER_FIXTURE()) diff --git a/libs/api/repository/src/lib/gn4/auth/gravatar.service.spec.ts b/libs/api/repository/src/lib/gn4/auth/gravatar.service.spec.ts index 64c2d54b05..6f31600dd7 100644 --- a/libs/api/repository/src/lib/gn4/auth/gravatar.service.spec.ts +++ b/libs/api/repository/src/lib/gn4/auth/gravatar.service.spec.ts @@ -2,7 +2,7 @@ import { TestBed } from '@angular/core/testing' import { GravatarService } from './gravatar.service' import { HttpClientTestingModule } from '@angular/common/http/testing' import { Gn4SettingsService } from '../settings/gn4-settings.service' -import { BehaviorSubject } from 'rxjs' +import { BehaviorSubject, firstValueFrom } from 'rxjs' class Gn4SettingsServiceMock { identicon$ = new BehaviorSubject('404') @@ -10,6 +10,7 @@ class Gn4SettingsServiceMock { describe('GravatarService', () => { let service: GravatarService + let settingsService: Gn4SettingsService beforeEach(() => { TestBed.configureTestingModule({ @@ -19,22 +20,31 @@ describe('GravatarService', () => { ], }) service = TestBed.inject(GravatarService) + settingsService = TestBed.inject(Gn4SettingsService) }) it('should be created', () => { expect(service).toBeTruthy() }) - it('returns correct Url without data', () => { - expect(service.getProfileIcon('')).toEqual( - 'https://www.gravatar.com/avatar/?d=404' - ) - }) - it('returns correct Url with data but without placeholder', () => { - expect(service.getProfileIcon('abc')).toEqual( - 'https://www.gravatar.com/avatar/abc?d=404' - ) + describe('#getProfileIcon', () => { + it('returns correct Url without data', async () => { + const icon = await firstValueFrom(service.getProfileIcon('')) + expect(icon).toEqual('https://www.gravatar.com/avatar/?d=404') + }) + it('returns correct Url with data but without placeholder', async () => { + const icon = await firstValueFrom(service.getProfileIcon('abc')) + expect(icon).toEqual('https://www.gravatar.com/avatar/abc?d=404') + }) }) - it('returns plaholder to be mp', () => { - expect(service.placeholder).toEqual('https://www.gravatar.com/avatar/?d=mp') + describe('#getPlaceholder', () => { + it('returns url with identicon value, without hash', async () => { + const placeholder = await firstValueFrom(service.getPlaceholder()) + expect(placeholder).toEqual('https://www.gravatar.com/avatar/?d=404') + }) + it('returns placeholder to be mp if no identicon value', async () => { + settingsService.identicon$.next('') + const placeholder = await firstValueFrom(service.getPlaceholder()) + expect(placeholder).toEqual('https://www.gravatar.com/avatar/?d=mp') + }) }) }) diff --git a/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.spec.ts b/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.spec.ts index 4d42efc935..804b7698e5 100644 --- a/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.spec.ts +++ b/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.spec.ts @@ -35,8 +35,8 @@ class MeApiMock { } class AvatarServiceInterfaceMock { - placeholder = 'http://placeholder.com' - getProfileIcon = jest.fn((hash: string) => `http://icon_service.com/${hash}`) + getPlaceholder = () => of('http://placeholder.com') + getProfileIcon = (hash: string) => of(`http://icon_service.com/${hash}`) } class SiteApiServiceMock {