Skip to content

Commit

Permalink
test(gravatar): fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tkohr committed Dec 6, 2023
1 parent e2813f8 commit 9fd3ff8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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())
Expand Down
34 changes: 22 additions & 12 deletions libs/api/repository/src/lib/gn4/auth/gravatar.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ 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')
}

describe('GravatarService', () => {
let service: GravatarService
let settingsService: Gn4SettingsService

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -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')
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9fd3ff8

Please sign in to comment.