Skip to content

Commit

Permalink
fix(me): fix impacted tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cmoinier committed Oct 2, 2023
1 parent 0dad3bf commit 2514f1b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export class MyOrgUsersComponent {
this.searchFacade.resetSearch()
this.authService.user$.subscribe((user) => {
this.orgName = user.organisation
// this.orgName = 'Géo2France'
})
this.orgService.organisations$.subscribe((orgs) => {
const org = orgs.filter((org) => org.name === this.orgName)[0]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,88 +1,46 @@
import { ComponentFixture, TestBed } from '@angular/core/testing'

import { MyOrgRecordsComponent } from './my-org-records.component'
import { SearchFacade, SearchService } from '@geonetwork-ui/feature/search'
import { Component, importProvidersFrom } from '@angular/core'
import { TranslateModule } from '@ngx-translate/core'
import { RecordsListComponent } from '../records-list.component'
import { BehaviorSubject, of } from 'rxjs'
import {
FILTERS_AGGREGATION,
ORGANISATIONS_FIXTURE,
USER_FIXTURE,
USERS_FIXTURE,
} from '@geonetwork-ui/common/fixtures'
import { BehaviorSubject, of } from 'rxjs'
import { AuthService } from '@geonetwork-ui/feature/auth'
import { OrganizationsServiceInterface } from '@geonetwork-ui/common/domain/organizations.service.interface'
import { AuthService } from '@geonetwork-ui/feature/auth'
import { SearchFacade } from '@geonetwork-ui/feature/search'

const user = USER_FIXTURE()
const filters = FILTERS_AGGREGATION

class AuthServiceMock {
user$ = new BehaviorSubject(user)
authReady = jest.fn(() => this._authSubject$)
_authSubject$ = new BehaviorSubject({})
}
class OrganisationsServiceMock {
getFiltersForOrgs = jest.fn(() => new BehaviorSubject(filters))
organisationsCount$ = of(456)
}

class searchServiceMock {
updateSearchFilters = jest.fn()
setSearch = jest.fn()
setSortBy = jest.fn()
setSortAndFilters = jest.fn()
}

class SearchFacadeMock {
resetSearch = jest.fn()
setFilters = jest.fn()
}

@Component({
// eslint-disable-next-line
selector: 'md-editor-records-list',
template: '',
standalone: true,
})
export class MockRecordsListComponent {}
const allUsers = USERS_FIXTURE()

describe('MyOrgRecordsComponent', () => {
let component: MyOrgRecordsComponent
let fixture: ComponentFixture<MyOrgRecordsComponent>
let searchFacade: SearchFacade
let orgService: OrganizationsServiceInterface
let authService: AuthService

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
importProvidersFrom(TranslateModule.forRoot()),
{
provide: SearchFacade,
useClass: SearchFacadeMock,
},
{ provide: AuthService, useClass: AuthServiceMock },
{
provide: OrganizationsServiceInterface,
useClass: OrganisationsServiceMock,
},
{
provide: SearchService,
useClass: searchServiceMock,
},
],
}).overrideComponent(MyOrgRecordsComponent, {
remove: {
imports: [RecordsListComponent],
},
add: {
imports: [MockRecordsListComponent],
},
})
searchFacade = TestBed.inject(SearchFacade)
orgService = TestBed.inject(OrganizationsServiceInterface)
fixture = TestBed.createComponent(MyOrgRecordsComponent)
component = fixture.componentInstance
fixture.detectChanges()
const authServiceMock = {
user$: new BehaviorSubject(user),
allUsers$: new BehaviorSubject(allUsers),
}

const organisationsServiceMock = {
organisations$: of(ORGANISATIONS_FIXTURE),
getFiltersForOrgs: jest.fn(() => new BehaviorSubject(filters)),
}

const searchFacadeMock = {
resetSearch: jest.fn(),
setFilters: jest.fn(),
}

authService = authServiceMock as any
orgService = organisationsServiceMock as any
searchFacade = searchFacadeMock as any

component = new MyOrgRecordsComponent(searchFacade, authService, orgService)
})

it('should create', () => {
Expand All @@ -102,4 +60,18 @@ describe('MyOrgRecordsComponent', () => {
expect(searchFacade.setFilters).toHaveBeenCalledWith(filters)
})
})

describe('Get organization users info', () => {
it('should get the org name', () => {
expect(component.orgName).toEqual('Région Hauts-de-France') // Replace with your test data
})

it('should get the org logo', () => {
expect(component.logoUrl).toEqual('https://my-geonetwork.org/logo11.png') // Replace with your test data
})

it('should get the list of users', () => {
expect(component.userCount).toEqual(3)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ export class MyOrgRecordsComponent {
) {
this.searchFacade.resetSearch()
this.authService.user$.subscribe((user) => {
// this.orgName = user.organisation
this.orgName = 'Géo2France'
this.orgName = user.organisation
this.searchByOrganisation({ name: this.orgName })
})
this.orgService.organisations$.subscribe((orgs) => {
Expand All @@ -40,12 +39,14 @@ export class MyOrgRecordsComponent {
})
this.authService.allUsers$.subscribe((users) => {
this.userCount = users.filter(
(user) => user.organisation === 'Barbie Inc.'
(user) => user.organisation === this.orgName
).length
})
}

searchByOrganisation(organisation: Organization) {
console.log(organisation)
console.log(this.orgService.getFiltersForOrgs([organisation]))
this.orgService
.getFiltersForOrgs([organisation])
.subscribe((filters) => this.searchFacade.setFilters(filters))
Expand Down
2 changes: 1 addition & 1 deletion libs/common/fixtures/src/lib/organisations.fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ export const ORGANISATIONS_FIXTURE: Organization[] = deepFreeze([
name: 'Région Hauts-de-France',
description: 'another org for testing',
logoUrl: new URL('https://my-geonetwork.org/logo11.png'),
recordCount: 15,
recordCount: 2,
},
])
2 changes: 2 additions & 0 deletions libs/feature/auth/src/lib/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TestBed } from '@angular/core/testing'
import { MeApiService } from '@geonetwork-ui/data-access/gn4'
import { TranslateService } from '@ngx-translate/core'
import { AvatarServiceInterface } from './avatar/avatar.service.interface'
import { HttpClientModule } from '@angular/common/http'

const userMock = {
id: '21737',
Expand Down Expand Up @@ -46,6 +47,7 @@ describe('AuthService', () => {
windowLocation = 'http://localhost'

TestBed.configureTestingModule({
imports: [HttpClientModule],
providers: [
{
provide: LOGIN_URL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('OrganisationsComponent', () => {
expect(
orgPreviewComponents[orgPreviewComponents.length - 1].organisation
.name
).toEqual('J Data Org')
).toEqual('Région Hauts-de-France')
})
})
})
Expand Down

0 comments on commit 2514f1b

Please sign in to comment.