Skip to content

Commit

Permalink
test(platform): adapt new imports from tests
Browse files Browse the repository at this point in the history
move from AuthService to PlatformeService
  • Loading branch information
fgravin committed Nov 27, 2023
1 parent 980864f commit 0833d60
Show file tree
Hide file tree
Showing 23 changed files with 300 additions and 197 deletions.
24 changes: 12 additions & 12 deletions apps/datahub/src/app/home/home-header/home-header.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { NO_ERRORS_SCHEMA } from '@angular/core'
import { ComponentFixture, TestBed } from '@angular/core/testing'
import { By } from '@angular/platform-browser'
import {
RouterFacade,
ROUTER_ROUTE_SEARCH,
RouterFacade,
} from '@geonetwork-ui/feature/router'
import {
FieldsService,
Expand All @@ -15,10 +15,10 @@ import { BehaviorSubject, firstValueFrom, of } from 'rxjs'
import { ROUTER_ROUTE_NEWS } from '../../router/constants'
import { HeaderBadgeButtonComponent } from '../header-badge-button/header-badge-button.component'
import { HomeHeaderComponent } from './home-header.component'
import resetAllMocks = jest.resetAllMocks
import { SortByEnum } from '@geonetwork-ui/common/domain/model/search'
import { AuthService } from '@geonetwork-ui/api/repository/gn4'
import { _setLanguages } from '@geonetwork-ui/util/app-config'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
import resetAllMocks = jest.resetAllMocks

jest.mock('@geonetwork-ui/util/app-config', () => {
let _languages = ['pt', 'de']
Expand Down Expand Up @@ -71,6 +71,11 @@ 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({})
Expand All @@ -83,7 +88,6 @@ class FieldsServiceMock {
describe('HeaderComponent', () => {
let component: HomeHeaderComponent
let fixture: ComponentFixture<HomeHeaderComponent>
let authService: AuthService
let searchService: SearchService
let searchFacade: SearchFacade
let routerFacade: RouterFacade
Expand All @@ -108,16 +112,15 @@ describe('HeaderComponent', () => {
useClass: searchServiceMock,
},
{
provide: AuthService,
useClass: AuthServiceMock,
provide: PlatformServiceInterface,
useClass: PlatformServiceMock,
},
{
provide: FieldsService,
useClass: FieldsServiceMock,
},
],
}).compileComponents()
authService = TestBed.inject(AuthService)
searchService = TestBed.inject(SearchService)
searchFacade = TestBed.inject(SearchFacade)
routerFacade = TestBed.inject(RouterFacade)
Expand All @@ -140,10 +143,7 @@ describe('HeaderComponent', () => {
describe('isAuthenticated$', () => {
describe('user is authenticated', () => {
beforeEach(() => {
;(authService as any)._authSubject$.next({
id: 'user-id',
name: 'testuser',
})
isAnonymous$.next(false)
})
it('displays favoriteBadge when authenticated', async () => {
const isAuthenticated = await firstValueFrom(
Expand All @@ -154,7 +154,7 @@ describe('HeaderComponent', () => {
})
describe('user is NOT authenticated', () => {
beforeEach(() => {
;(authService as any)._authSubject$.next(null)
isAnonymous$.next(true)
})
it('does NOT display favoriteBadge when NOT authenticated', async () => {
const isAuthenticated = await firstValueFrom(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export class HomeHeaderComponent {
)

isAuthenticated$ = this.platformService
.getMe()
.pipe(map((user) => !!user?.id))
.isAnonymous()
.pipe(map((isAnonymous) => !isAnonymous))

onFuzzySearchSelection(record: CatalogRecord) {
this.routerFacade.goToMetadata(record)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { FormsModule } from '@angular/forms'
import { FieldFilters } from '@geonetwork-ui/common/domain/model/search'
import { USER_FIXTURE } from '@geonetwork-ui/common/fixtures'
import { AuthService } from '@geonetwork-ui/api/repository/gn4'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'

jest.mock('@geonetwork-ui/util/app-config', () => ({
getOptionalSearchConfig: () => ({
Expand Down Expand Up @@ -103,10 +104,8 @@ class FieldsServiceMock {
}
}

class AuthServiceMock {
user$ = new BehaviorSubject(user)
authReady = jest.fn(() => this._authSubject$)
_authSubject$ = new BehaviorSubject({})
class PlatformServiceMock {
getMe = jest.fn(() => new BehaviorSubject(user))
}

describe('SearchFiltersComponent', () => {
Expand Down Expand Up @@ -138,8 +137,8 @@ describe('SearchFiltersComponent', () => {
useClass: FieldsServiceMock,
},
{
provide: AuthService,
useClass: AuthServiceMock,
provide: PlatformServiceInterface,
useClass: PlatformServiceMock,
},
],
})
Expand Down
2 changes: 2 additions & 0 deletions apps/metadata-editor/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { extModules } from './build-specifics'
import { DashboardPageComponent } from './dashboard/dashboard-page.component'
import { EditorRouterService } from './router.service'
import { provideRepositoryUrl } from '@geonetwork-ui/api/repository'
import { provideGn4 } from '@geonetwork-ui/api/repository/gn4'

@NgModule({
declarations: [AppComponent],
Expand Down Expand Up @@ -58,6 +59,7 @@ import { provideRepositoryUrl } from '@geonetwork-ui/api/repository'
importProvidersFrom(TranslateModule.forRoot(TRANSLATE_DEFAULT_CONFIG)),
provideRepositoryUrl(() => getGlobalConfig().GN4_API_URL),
importProvidersFrom(EffectsModule.forRoot()),
provideGn4(),
],
bootstrap: [AppComponent],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<button>
<mat-icon class="icon-btn material-symbols-outlined">settings</mat-icon>
</button>
<ng-container *ngrxLet="authService.user$ as user">
<ng-container *ngrxLet="platformService.getMe() as user">
<gn-ui-user-preview
*ngIf="user && user.name"
[user]="user"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,28 @@
import { ChangeDetectionStrategy, NO_ERRORS_SCHEMA } from '@angular/core'
import {
ChangeDetectionStrategy,
CUSTOM_ELEMENTS_SCHEMA,
NO_ERRORS_SCHEMA,
} from '@angular/core'
import { ComponentFixture, TestBed } from '@angular/core/testing'

import { SearchHeaderComponent } from './search-header.component'
import { BehaviorSubject, of } from 'rxjs'
import { summaryHits, USER_FIXTURE } from '@geonetwork-ui/common/fixtures'
import { BehaviorSubject } from 'rxjs'
import { USER_FIXTURE } from '@geonetwork-ui/common/fixtures'
import { StoreModule } from '@ngrx/store'
import { EffectsModule } from '@ngrx/effects'
import { TranslateModule } from '@ngx-translate/core'
import { TRANSLATE_DEFAULT_CONFIG } from '@geonetwork-ui/util/i18n'
import { OrganizationsServiceInterface } from '@geonetwork-ui/common/domain/organizations.service.interface'
import { SearchFacade, SearchService } from '@geonetwork-ui/feature/search'
import {
AuthService,
AvatarServiceInterface,
} from '@geonetwork-ui/api/repository/gn4'
import { provideRepositoryUrl } from '@geonetwork-ui/api/repository'

const user = USER_FIXTURE()
class AuthServiceMock {
user$ = new BehaviorSubject(user)
authReady = jest.fn(() => this._authSubject$)
_authSubject$ = new BehaviorSubject({})
}
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
import { AvatarServiceInterface } from '@geonetwork-ui/api/repository/gn4'

class AvatarServiceInterfaceMock {
placeholder = 'http://placeholder.com'
getProfileIcon = (hash: string) => `${hash}`
}

class OrganisationsServiceMock {
organisationsCount$ = of(456)
}
class SearchFacadeMock {
init = jest.fn()
results$ = of(summaryHits)
setPagination = jest.fn(() => this)
setSortBy = jest.fn(() => this)
setConfigRequestFields = jest.fn(() => this)
setResultsLayout = jest.fn(() => this)
searchFilters$ = new BehaviorSubject(user)
authReady = jest.fn(() => this.searchFilters$)
}

class searchServiceMock {
updateSearchFilters = jest.fn()
setSearch = jest.fn()
setSortBy = jest.fn()
setSortAndFilters = jest.fn()
const me$ = new BehaviorSubject(USER_FIXTURE())
class PlatformServiceMock {
getMe = jest.fn(() => me$)
}

describe('SearchHeaderComponent', () => {
Expand All @@ -63,28 +39,22 @@ describe('SearchHeaderComponent', () => {
],
schemas: [NO_ERRORS_SCHEMA],
providers: [
provideRepositoryUrl('/geonetwork/srv/api'),
{ provide: AuthService, useClass: AuthServiceMock },
{
provide: AvatarServiceInterface,
useClass: AvatarServiceInterfaceMock,
},
{
provide: OrganizationsServiceInterface,
useClass: OrganisationsServiceMock,
},
{
provide: SearchFacade,
useClass: SearchFacadeMock,
},
{
provide: SearchService,
useClass: searchServiceMock,
provide: PlatformServiceInterface,
useClass: PlatformServiceMock,
},
],
})
.overrideComponent(SearchHeaderComponent, {
set: { changeDetection: ChangeDetectionStrategy.Default },
set: {
changeDetection: ChangeDetectionStrategy.Default,
imports: [],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
},
})
.compileComponents()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import { MatIconModule } from '@angular/material/icon'
import { LetDirective } from '@ngrx/component'
import { FeatureSearchModule } from '@geonetwork-ui/feature/search'
import { UiElementsModule } from '@geonetwork-ui/ui/elements'
import {
AuthService,
AvatarServiceInterface,
} from '@geonetwork-ui/api/repository/gn4'
import { AvatarServiceInterface } from '@geonetwork-ui/api/repository/gn4'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'

@Component({
selector: 'md-editor-search-header',
Expand All @@ -27,7 +25,7 @@ export class SearchHeaderComponent {
public placeholder = this.avatarService.placeholder

constructor(
public authService: AuthService,
public platformService: PlatformServiceInterface,
private avatarService: AvatarServiceInterface
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TranslateModule } from '@ngx-translate/core'
import { CommonModule } from '@angular/common'
import { MyOrgService } from '@geonetwork-ui/feature/catalog'
import { SearchFacade } from '@geonetwork-ui/feature/search'
import { UserApiModel } from '@geonetwork-ui/data-access/gn4'
import { UserModel } from '@geonetwork-ui/common/domain/model/user/user.model'

@Component({
selector: 'md-editor-my-org-users',
Expand All @@ -25,7 +25,7 @@ export class MyOrgUsersComponent implements OnDestroy {
logoUrl: string
recordCount: number
userCount: number
userList: UserApiModel[]
userList: UserModel[]
}

private myOrgDataSubscription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { MyOrgService } from '@geonetwork-ui/feature/catalog'
import { SearchFacade } from '@geonetwork-ui/feature/search'
import { Organization } from '@geonetwork-ui/common/domain/model/record'
import { OrganizationsServiceInterface } from '@geonetwork-ui/common/domain/organizations.service.interface'
import { UserApiModel } from '@geonetwork-ui/data-access/gn4'
import { EditorRouterService } from '../../router.service'
import { UserModel } from '@geonetwork-ui/common/domain/model/user/user.model'

@Component({
selector: 'md-editor-my-org-records',
Expand All @@ -22,7 +22,7 @@ export class MyOrgRecordsComponent implements OnDestroy {
logoUrl: string
recordCount: number
userCount: number
userList: UserApiModel[]
userList: UserModel[]
}

public myOrgDataSubscription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { BehaviorSubject, of } from 'rxjs'
import { USER_FIXTURE } from '@geonetwork-ui/common/fixtures'
import { AuthService } from '@geonetwork-ui/api/repository/gn4'
import { EditorRouterService } from '../../router.service'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'

@Component({
// eslint-disable-next-line
Expand Down Expand Up @@ -38,6 +39,11 @@ class FieldsServiceMock {
buildFiltersFromFieldValues = jest.fn((val) => of(val))
}

const me$ = new BehaviorSubject(USER_FIXTURE())
class PlatformServiceMock {
getMe = jest.fn(() => me$)
}

describe('MyRecordsComponent', () => {
let component: MyRecordsComponent
let fixture: ComponentFixture<MyRecordsComponent>
Expand All @@ -56,8 +62,8 @@ describe('MyRecordsComponent', () => {
useClass: SearchFacadeMock,
},
{
provide: AuthService,
useClass: AuthServiceMock,
provide: PlatformServiceInterface,
useClass: PlatformServiceMock,
},
{
provide: EditorRouterService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { FieldsService, SearchFacade } from '@geonetwork-ui/feature/search'
import { AuthService } from '@geonetwork-ui/api/repository/gn4'
import { EditorRouterService } from '../../router.service'
import { Subscription } from 'rxjs'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'

@Component({
selector: 'md-editor-my-records',
Expand All @@ -21,13 +22,13 @@ export class MyRecordsComponent implements OnInit, OnDestroy {
constructor(
public fieldsService: FieldsService,
public searchFacade: SearchFacade,
private authService: AuthService,
private platformService: PlatformServiceInterface,
private router: EditorRouterService
) {}

ngOnInit() {
this.searchFacade.resetSearch()
this.sub = this.authService.user$.subscribe((user) => {
this.sub = this.platformService.getMe().subscribe((user) => {
this.ownerId = user.id
this.fieldsService
.buildFiltersFromFieldValues({ owner: user.id })
Expand Down
Loading

0 comments on commit 0833d60

Please sign in to comment.