From 548263bf793cfbd7034548fa7ec424797251b0be Mon Sep 17 00:00:00 2001 From: Florent Gravin Date: Tue, 28 Nov 2023 13:47:22 +0100 Subject: [PATCH] refactor(platform): rework test to use inner Observables to avoid using global subjects which could create side effects --- .../home-header/home-header.component.spec.ts | 15 ++++---- .../src/lib/gn4/auth/auth.service.spec.ts | 34 ------------------- .../gn4/platform/gn4-platform.service.spec.ts | 12 ++++--- .../lib/gn4/platform/gn4-platform.service.ts | 2 +- .../domain/src/lib/model/user/user.model.ts | 11 ------ .../src/lib/platform.service.interface.ts | 2 +- .../search/src/lib/state/effects.spec.ts | 4 +-- 7 files changed, 17 insertions(+), 63 deletions(-) diff --git a/apps/datahub/src/app/home/home-header/home-header.component.spec.ts b/apps/datahub/src/app/home/home-header/home-header.component.spec.ts index 75ac62a7ab..1d764bdd72 100644 --- a/apps/datahub/src/app/home/home-header/home-header.component.spec.ts +++ b/apps/datahub/src/app/home/home-header/home-header.component.spec.ts @@ -71,14 +71,9 @@ class searchServiceMock { setFilters = jest.fn() } -const isAnonymous$ = new BehaviorSubject(false) class PlatformServiceMock { - isAnonymous = jest.fn(() => isAnonymous$) -} - -class AuthServiceMock { - authReady = jest.fn(() => this._authSubject$) - _authSubject$ = new BehaviorSubject({}) + isAnonymous = jest.fn(() => this._isAnonymous$) + _isAnonymous$ = new BehaviorSubject(true) } class FieldsServiceMock { @@ -91,6 +86,7 @@ describe('HeaderComponent', () => { let searchService: SearchService let searchFacade: SearchFacade let routerFacade: RouterFacade + let platform: PlatformServiceInterface beforeEach(async () => { _setLanguages(['fr', 'de']) @@ -124,6 +120,7 @@ describe('HeaderComponent', () => { searchService = TestBed.inject(SearchService) searchFacade = TestBed.inject(SearchFacade) routerFacade = TestBed.inject(RouterFacade) + platform = TestBed.inject(PlatformServiceInterface) }) beforeEach(() => { @@ -143,7 +140,7 @@ describe('HeaderComponent', () => { describe('isAuthenticated$', () => { describe('user is authenticated', () => { beforeEach(() => { - isAnonymous$.next(false) + ;(platform as any)._isAnonymous$.next(false) }) it('displays favoriteBadge when authenticated', async () => { const isAuthenticated = await firstValueFrom( @@ -154,7 +151,7 @@ describe('HeaderComponent', () => { }) describe('user is NOT authenticated', () => { beforeEach(() => { - isAnonymous$.next(true) + ;(platform as any)._isAnonymous$.next(true) }) it('does NOT display favoriteBadge when NOT authenticated', async () => { const isAuthenticated = await firstValueFrom( diff --git a/libs/api/repository/src/lib/gn4/auth/auth.service.spec.ts b/libs/api/repository/src/lib/gn4/auth/auth.service.spec.ts index 56890beab9..f604788788 100644 --- a/libs/api/repository/src/lib/gn4/auth/auth.service.spec.ts +++ b/libs/api/repository/src/lib/gn4/auth/auth.service.spec.ts @@ -1,38 +1,12 @@ import { AuthService, LOGIN_URL } from './auth.service' -import { Subject } from 'rxjs' import { TestBed } from '@angular/core/testing' -import { MeApiService } from '@geonetwork-ui/data-access/gn4' import { TranslateService } from '@ngx-translate/core' -import { AvatarServiceInterface } from './avatar.service.interface' import { HttpClientTestingModule } from '@angular/common/http/testing' -const userMock = { - id: '21737', - profile: 'Administrator', - username: 'C2C-gravin', - name: 'Florent', - surname: 'Gravin', - email: 'florent.gravin@camptocamp.com', - hash: 'girafe', - organisation: null, - admin: true, - groupsWithRegisteredUser: [], - groupsWithEditor: [], - groupsWithReviewer: [], - groupsWithUserAdmin: [], -} let loginUrlTokenMock const translateServiceMock = { currentLang: 'fr', } -const me$ = new Subject() -const meApiMock = { - getMe: () => me$, -} -class AvatarServiceInterfaceMock { - placeholder = 'http://placeholder.com' - getProfileIcon = jest.fn((hash: string) => `http://icon_service.com/${hash}`) -} let windowLocation Object.defineProperties((global as any).window, { @@ -52,18 +26,10 @@ describe('AuthService', () => { provide: LOGIN_URL, useFactory: () => loginUrlTokenMock, }, - { - provide: MeApiService, - useValue: meApiMock, - }, { provide: TranslateService, useValue: translateServiceMock, }, - { - provide: AvatarServiceInterface, - useClass: AvatarServiceInterfaceMock, - }, ], imports: [HttpClientTestingModule], }) 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 2dbc9915ad..4d42efc935 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 @@ -27,11 +27,11 @@ const userMock = { groupsWithUserAdmin: [], } -const me$ = new Subject() class MeApiMock { getMe() { - return me$ + return this._me$ } + _me$ = new Subject() } class AvatarServiceInterfaceMock { @@ -65,6 +65,7 @@ class UsersApiServiceMock { describe('Gn4PlatformService', () => { let service: Gn4PlatformService + let meApiService: MeApiService beforeEach(() => { TestBed.configureTestingModule({ @@ -90,6 +91,7 @@ describe('Gn4PlatformService', () => { ], }) service = TestBed.inject(Gn4PlatformService) + meApiService = TestBed.inject(MeApiService) }) it('creates', () => { @@ -117,7 +119,7 @@ describe('Gn4PlatformService', () => { ]) }) it('is of type GeoNetwork', async () => { - expect(service.getTye()).toEqual('GeoNetwork') + expect(service.getType()).toEqual('GeoNetwork') }) describe('MeService', () => { let me @@ -126,7 +128,7 @@ describe('Gn4PlatformService', () => { }) describe('When user is logged in', () => { beforeEach(() => { - me$.next(userMock) + ;(meApiService as any)._me$.next(userMock) }) it('returns mapped user ', async () => { expect(me).toEqual({ @@ -147,7 +149,7 @@ describe('Gn4PlatformService', () => { }) describe('When no user is logged in', () => { beforeEach(() => { - me$.next({}) + ;(meApiService as any)._me$.next({}) }) it('returns no user ', async () => { const me = await firstValueFrom(service.getMe()) diff --git a/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts b/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts index 5f641e7865..5e4807ade3 100644 --- a/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts +++ b/libs/api/repository/src/lib/gn4/platform/gn4-platform.service.ts @@ -55,7 +55,7 @@ export class Gn4PlatformService implements PlatformServiceInterface { ) } - getTye(): string { + getType(): string { return this.type } diff --git a/libs/common/domain/src/lib/model/user/user.model.ts b/libs/common/domain/src/lib/model/user/user.model.ts index 61ed23805c..591fc7bf18 100644 --- a/libs/common/domain/src/lib/model/user/user.model.ts +++ b/libs/common/domain/src/lib/model/user/user.model.ts @@ -8,14 +8,3 @@ export interface UserModel { organisation: string profileIcon?: string } - -export interface UserModela { - enabled?: boolean - emailAddresses?: Set - organisation?: string - kind?: string - lastLoginDate?: string - accountNonExpired?: boolean - accountNonLocked?: boolean - credentialsNonExpired?: boolean -} diff --git a/libs/common/domain/src/lib/platform.service.interface.ts b/libs/common/domain/src/lib/platform.service.interface.ts index 14d464cac8..7872c2d052 100644 --- a/libs/common/domain/src/lib/platform.service.interface.ts +++ b/libs/common/domain/src/lib/platform.service.interface.ts @@ -3,7 +3,7 @@ import { UserModel } from './model/user/user.model' import { Organization } from './model/record/organization.model' export abstract class PlatformServiceInterface { - abstract getTye(): string + abstract getType(): string abstract getApiVersion(): Observable abstract isApiCompatible(): Observable diff --git a/libs/feature/search/src/lib/state/effects.spec.ts b/libs/feature/search/src/lib/state/effects.spec.ts index ac394a12df..cce5831230 100644 --- a/libs/feature/search/src/lib/state/effects.spec.ts +++ b/libs/feature/search/src/lib/state/effects.spec.ts @@ -67,7 +67,7 @@ const stateWithSearches = { }, } -class Gn4PlatformServiceMock { +class PlatformServiceMock { getMe = jest.fn(() => of(true)) } class FavoritesServiceMock { @@ -99,7 +99,7 @@ describe('Effects', () => { SearchEffects, { provide: PlatformServiceInterface, - useClass: Gn4PlatformServiceMock, + useClass: PlatformServiceMock, }, { provide: FavoritesService,