Skip to content

Commit

Permalink
refactor(platform): rework test to use inner Observables
Browse files Browse the repository at this point in the history
to avoid using global subjects which could create side effects
  • Loading branch information
fgravin committed Nov 28, 2023
1 parent 4791add commit 548263b
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -91,6 +86,7 @@ describe('HeaderComponent', () => {
let searchService: SearchService
let searchFacade: SearchFacade
let routerFacade: RouterFacade
let platform: PlatformServiceInterface

beforeEach(async () => {
_setLanguages(['fr', 'de'])
Expand Down Expand Up @@ -124,6 +120,7 @@ describe('HeaderComponent', () => {
searchService = TestBed.inject(SearchService)
searchFacade = TestBed.inject(SearchFacade)
routerFacade = TestBed.inject(RouterFacade)
platform = TestBed.inject(PlatformServiceInterface)
})

beforeEach(() => {
Expand All @@ -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(
Expand All @@ -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(
Expand Down
34 changes: 0 additions & 34 deletions libs/api/repository/src/lib/gn4/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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: '[email protected]',
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, {
Expand All @@ -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],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ const userMock = {
groupsWithUserAdmin: [],
}

const me$ = new Subject()
class MeApiMock {
getMe() {
return me$
return this._me$
}
_me$ = new Subject()
}

class AvatarServiceInterfaceMock {
Expand Down Expand Up @@ -65,6 +65,7 @@ class UsersApiServiceMock {

describe('Gn4PlatformService', () => {
let service: Gn4PlatformService
let meApiService: MeApiService

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -90,6 +91,7 @@ describe('Gn4PlatformService', () => {
],
})
service = TestBed.inject(Gn4PlatformService)
meApiService = TestBed.inject(MeApiService)
})

it('creates', () => {
Expand Down Expand Up @@ -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
Expand All @@ -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({
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class Gn4PlatformService implements PlatformServiceInterface {
)
}

getTye(): string {
getType(): string {
return this.type
}

Expand Down
11 changes: 0 additions & 11 deletions libs/common/domain/src/lib/model/user/user.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,3 @@ export interface UserModel {
organisation: string
profileIcon?: string
}

export interface UserModela {
enabled?: boolean
emailAddresses?: Set<string>
organisation?: string
kind?: string
lastLoginDate?: string
accountNonExpired?: boolean
accountNonLocked?: boolean
credentialsNonExpired?: boolean
}
2 changes: 1 addition & 1 deletion libs/common/domain/src/lib/platform.service.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>
abstract isApiCompatible(): Observable<boolean>

Expand Down
4 changes: 2 additions & 2 deletions libs/feature/search/src/lib/state/effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const stateWithSearches = {
},
}

class Gn4PlatformServiceMock {
class PlatformServiceMock {
getMe = jest.fn(() => of(true))
}
class FavoritesServiceMock {
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('Effects', () => {
SearchEffects,
{
provide: PlatformServiceInterface,
useClass: Gn4PlatformServiceMock,
useClass: PlatformServiceMock,
},
{
provide: FavoritesService,
Expand Down

0 comments on commit 548263b

Please sign in to comment.