Skip to content

Commit

Permalink
finished unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Romuald Caplier committed Apr 24, 2024
1 parent 1f35050 commit 87d4fcf
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.
import {
DATASET_RECORDS,
SOME_USER_FEEDBACKS,
A_USER,
USER_FIXTURE,
} from '@geonetwork-ui/common/fixtures'
import { UserFeedbackViewModel } from '@geonetwork-ui/common/domain/model/record'
import { Gn4PlatformMapper } from '@geonetwork-ui/api/repository'

describe('RelatedRecordsComponent', () => {
const allUserFeedbacks = SOME_USER_FEEDBACKS
Expand All @@ -30,6 +31,9 @@ describe('RelatedRecordsComponent', () => {
isAddUserFeedbackLoading$: new BehaviorSubject(false),
loadUserFeedbacks: jest.fn(),
userFeedbacks$: of(allUserFeedbacks),
}

const gn4PlatformMapperMock: Partial<Gn4PlatformMapper> = {
createUserFeedbackViewModel: (baseUserFeedback) => {
return Promise.resolve({
...baseUserFeedback,
Expand All @@ -44,7 +48,7 @@ describe('RelatedRecordsComponent', () => {

const platformServiceInterfaceMock: Partial<PlatformServiceInterface> = {
getUserFeedbacks: jest.fn(),
getMe: jest.fn(() => new BehaviorSubject(A_USER)),
getMe: jest.fn(() => new BehaviorSubject(USER_FIXTURE())),
}

let component: RecordUserFeedbacksComponent
Expand All @@ -70,6 +74,10 @@ describe('RelatedRecordsComponent', () => {
provide: PlatformServiceInterface,
useValue: platformServiceInterfaceMock,
},
{
provide: Gn4PlatformMapper,
useValue: gn4PlatformMapperMock,
},
],
})
.overrideComponent(RecordUserFeedbacksComponent, {
Expand Down Expand Up @@ -108,7 +116,7 @@ describe('RelatedRecordsComponent', () => {
it('should set active user', fakeAsync(() => {
component.ngOnInit()
tick()
expect(component.activeUser).toEqual(A_USER)
expect(component.activeUser).toEqual(USER_FIXTURE())
}))
it('should fetch user feedbacks and sort them correctly', async () => {
component.ngOnInit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { UserModel } from '@geonetwork-ui/common/domain/model/user'
import { DropdownChoice } from '@geonetwork-ui/ui/inputs'
import { MdViewFacade } from '@geonetwork-ui/feature/record'
import { TranslateService } from '@ngx-translate/core'
import { AuthService } from '@geonetwork-ui/api/repository'
import { AuthService, Gn4PlatformMapper } from '@geonetwork-ui/api/repository'

type UserFeedbackSortingFunction = (
userFeedbackA: UserFeedback,
Expand Down Expand Up @@ -72,11 +72,12 @@ export class RecordUserFeedbacksComponent implements OnInit, OnDestroy {
isAddUserFeedbackLoading = false

constructor(
private translate: TranslateService,
private authService: AuthService,
private metadataViewFacade: MdViewFacade,
private cdr: ChangeDetectorRef,
private platformServiceInterface: PlatformServiceInterface
private readonly translate: TranslateService,
private readonly authService: AuthService,
private readonly metadataViewFacade: MdViewFacade,
private readonly cdr: ChangeDetectorRef,
private readonly mapper: Gn4PlatformMapper,
private readonly platformServiceInterface: PlatformServiceInterface
) {
this.activeUser$ = this.platformServiceInterface.getMe()
}
Expand Down Expand Up @@ -124,13 +125,13 @@ export class RecordUserFeedbacksComponent implements OnInit, OnDestroy {

const userFeedbacksParentsViewModels = await Promise.all(
userFeedbacksParents.map((feedback) =>
this.metadataViewFacade.createUserFeedbackViewModel(feedback)
this.mapper.createUserFeedbackViewModel(feedback)
)
)

const userFeedbacksAnswersViewModels = await Promise.all(
userFeedbacksAnswers.map((feedback) =>
this.metadataViewFacade.createUserFeedbackViewModel(feedback)
this.mapper.createUserFeedbackViewModel(feedback)
)
)

Expand Down
15 changes: 14 additions & 1 deletion libs/api/repository/src/lib/gn4/platform/gn4-platform.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AvatarServiceInterface } from '../auth'
import { map } from 'rxjs/operators'
import { Observable, of } from 'rxjs'
import { ThesaurusModel } from '@geonetwork-ui/common/domain/model/thesaurus/thesaurus.model'
import { UserFeedback } from '@geonetwork-ui/common/domain/model/record'
import { UserFeedback, UserFeedbackViewModel } from '@geonetwork-ui/common/domain/model/record'

@Injectable()
export class Gn4PlatformMapper {
Expand Down Expand Up @@ -99,4 +99,17 @@ export class Gn4PlatformMapper {
date: userFeedback.date.getTime().toString(),
}
}

async createUserFeedbackViewModel(
baseUserFeedback: UserFeedback
): Promise<UserFeedbackViewModel> {
const userAvatarUrl = await this.avatarService.getProfileIconUrl(
baseUserFeedback.authorUserId?.toString()
)

return {
...baseUserFeedback,
avatarUrl: userAvatarUrl,
}
}
}
4 changes: 2 additions & 2 deletions libs/feature/record/src/lib/state/mdview.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const addUserFeedbackSuccess = createAction(

export const addUserFeedbackFailure = createAction(
'[Metadata view] Add UserFeedback Failure',
props<{ error: any }>()
props<{ otherError?: string; notFound?: boolean }>()
)

export const loadUserFeedbacks = createAction(
Expand All @@ -76,5 +76,5 @@ export const loadUserFeedbacksSuccess = createAction(

export const loadUserFeedbacksFailure = createAction(
'[Metadata view] Load UserFeedbacks Failure',
props<{ error: any }>()
props<{ otherError?: string; notFound?: boolean }>()
)
134 changes: 133 additions & 1 deletion libs/feature/record/src/lib/state/mdview.effects.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { TestBed } from '@angular/core/testing'
import {
A_USER_FEEDBACK,
DATASET_RECORDS,
SAMPLE_AGGREGATIONS_RESULTS,
SAMPLE_SEARCH_RESULTS,
SOME_USER_FEEDBACKS,
} from '@geonetwork-ui/common/fixtures'

import { provideMockActions } from '@ngrx/effects/testing'
Expand All @@ -15,6 +17,7 @@ import { MdViewEffects } from './mdview.effects'
import { hot } from 'jasmine-marbles'
import { CatalogRecord } from '@geonetwork-ui/common/domain/model/record'
import { RecordsRepositoryInterface } from '@geonetwork-ui/common/domain/repository/records-repository.interface'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'

const full = {
uniqueIdentifier: '1231321321',
Expand All @@ -29,10 +32,16 @@ class RecordsRepositoryMock {
getSimilarRecords = jest.fn(() => of(DATASET_RECORDS))
}

class PlatformServiceInterfaceMock {
getUserFeedbacks = jest.fn(() => of(SOME_USER_FEEDBACKS))
postUserFeedbacks = jest.fn(() => of(undefined))
}

describe('MdViewEffects', () => {
let actions: Observable<any>
let effects: MdViewEffects
let repository: RecordsRepositoryInterface
let platform: PlatformServiceInterface

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -45,14 +54,19 @@ describe('MdViewEffects', () => {
provide: RecordsRepositoryInterface,
useClass: RecordsRepositoryMock,
},
{
provide: PlatformServiceInterface,
useClass: PlatformServiceInterfaceMock,
},
],
})

repository = TestBed.inject(RecordsRepositoryInterface)
effects = TestBed.inject(MdViewEffects)
platform = TestBed.inject(PlatformServiceInterface)
})

describe('loadFullRecord$', () => {
describe('loadFullMetadata$', () => {
describe('when api success and at least one record found', () => {
it('dispatch loadFullSuccess', () => {
actions = hot('-a-|', {
Expand Down Expand Up @@ -126,4 +140,122 @@ describe('MdViewEffects', () => {
})
})
})

describe('loadUserFeedbacks$', () => {
describe('when loadUserFeedbacks success', () => {
it('should dispatch loadUserFeedbacksSuccess when API call is successful', () => {
actions = hot('-a-', {
a: MdViewActions.loadUserFeedbacks({ datasetUuid: '12345' }),
})
const expected = hot('-a-', {
a: MdViewActions.loadUserFeedbacksSuccess({
userFeedbacks: SOME_USER_FEEDBACKS,
}),
})

expect(effects.loadUserFeedbacks$).toBeObservable(expected)
})
})

describe('when api fails', () => {
const error = 'API error'

beforeEach(() => {
platform.getUserFeedbacks = jest.fn(() =>
throwError(() => new Error(error))
)
})

it('should dispatch loadUserFeedbacksFailure when API call fails', () => {
actions = hot('-a|', {
a: MdViewActions.loadUserFeedbacks({ datasetUuid: '12345' }),
})
const expected = hot('-a|', {
a: MdViewActions.loadUserFeedbacksFailure({ otherError: error }),
})

expect(effects.loadUserFeedbacks$).toBeObservable(expected)
})
})
})

describe('reloadUserFeedbacks$', () => {
describe('when addUserFeedbackSuccess', () => {
it('should dispatch loadUserFeedbacksSuccess when API call is successful', () => {
actions = hot('-a-', {
a: MdViewActions.addUserFeedbackSuccess({ datasetUuid: '12345' }),
})
const expected = hot('-a', {
a: MdViewActions.loadUserFeedbacksSuccess({
userFeedbacks: SOME_USER_FEEDBACKS,
}),
})

expect(effects.reloadUserFeedbacks$).toBeObservable(expected)
})
})

describe('when api fails', () => {
const error = 'API error'

beforeEach(() => {
platform.getUserFeedbacks = jest.fn(() =>
throwError(() => new Error(error))
)
})

it('should dispatch loadUserFeedbacksFailure when API call fails', () => {
const error = 'API error'

actions = hot('-a-', {
a: MdViewActions.addUserFeedbackSuccess({ datasetUuid: '12345' }),
})
const expected = hot('-a', {
a: MdViewActions.loadUserFeedbacksFailure({ otherError: error }),
})

expect(effects.reloadUserFeedbacks$).toBeObservable(expected)
})
})
})

describe('addUserFeedback$', () => {
describe('when addUserFeedback success', () => {
it('should dispatch addUserFeedbackSuccess when API call is successful', () => {
actions = hot('-a-', {
a: MdViewActions.addUserFeedback({ userFeedback: A_USER_FEEDBACK }),
})
const expected = hot('-a-', {
a: MdViewActions.addUserFeedbackSuccess({
datasetUuid: A_USER_FEEDBACK.metadataUUID,
}),
})

expect(effects.addUserFeedback$).toBeObservable(expected)
})
})

describe('when api fails', () => {
const error = 'API error'

beforeEach(() => {
platform.postUserFeedbacks = jest.fn(() =>
throwError(() => new Error(error))
)
})

it('should dispatch addUserFeedbackFailure when API call fails', () => {
const error = 'API error'

actions = hot('-a-', {
a: MdViewActions.addUserFeedback({ userFeedback: A_USER_FEEDBACK }),
})
const expected = hot('-a', {
a: MdViewActions.addUserFeedbackFailure({ otherError: error }),
})

expect(effects.addUserFeedback$).toBeObservable(expected)
})
})
})
})
50 changes: 25 additions & 25 deletions libs/feature/record/src/lib/state/mdview.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,18 @@ export class MdViewEffects {
/*
UserFeedback effects
*/
addUserFeedback$ = createEffect(() =>
loadUserFeedbacks$ = createEffect(() =>
this.actions$.pipe(
ofType(MdViewActions.addUserFeedback),
mergeMap((action) =>
this.platformServiceInterface
.postUserFeedbacks(action.userFeedback)
.pipe(
map(() =>
MdViewActions.addUserFeedbackSuccess({
datasetUuid: action.userFeedback.metadataUUID,
})
),
catchError((error) => {
return of(MdViewActions.addUserFeedbackFailure({ error }))
})
ofType(MdViewActions.loadUserFeedbacks),
exhaustMap(({ datasetUuid }) =>
this.platformServiceInterface.getUserFeedbacks(datasetUuid).pipe(
map((userFeedbacks) =>
MdViewActions.loadUserFeedbacksSuccess({ userFeedbacks })
),
catchError((error) =>
of(MdViewActions.loadUserFeedbacksFailure({ otherError: error.message }))
)
)
)
)
)
Expand All @@ -81,25 +77,29 @@ export class MdViewEffects {
MdViewActions.loadUserFeedbacksSuccess({ userFeedbacks })
),
catchError((error) =>
of(MdViewActions.loadUserFeedbacksFailure({ error }))
of(MdViewActions.loadUserFeedbacksFailure({ otherError: error.message }))
)
)
)
)
)

loadUserFeedbacks$ = createEffect(() =>
addUserFeedback$ = createEffect(() =>
this.actions$.pipe(
ofType(MdViewActions.loadUserFeedbacks),
exhaustMap(({ datasetUuid }) =>
this.platformServiceInterface.getUserFeedbacks(datasetUuid).pipe(
map((userFeedbacks) =>
MdViewActions.loadUserFeedbacksSuccess({ userFeedbacks })
),
catchError((error) =>
of(MdViewActions.loadUserFeedbacksFailure({ error }))
ofType(MdViewActions.addUserFeedback),
mergeMap((action) =>
this.platformServiceInterface
.postUserFeedbacks(action.userFeedback)
.pipe(
map(() =>
MdViewActions.addUserFeedbackSuccess({
datasetUuid: action.userFeedback.metadataUUID,
})
),
catchError((error) => {
return of(MdViewActions.addUserFeedbackFailure({ otherError: error.message }))
})
)
)
)
)
)
Expand Down
Loading

0 comments on commit 87d4fcf

Please sign in to comment.