Skip to content

Commit

Permalink
chore: fix various test failures part III
Browse files Browse the repository at this point in the history
Also fix warnings
  • Loading branch information
jahow committed Jan 14, 2025
1 parent 69374c8 commit 2e1983c
Show file tree
Hide file tree
Showing 41 changed files with 128 additions and 377 deletions.
15 changes: 2 additions & 13 deletions apps/datafeeder/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import '../../../jest.setup'

import { getTestBed } from '@angular/core/testing'
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing'

getTestBed().resetTestEnvironment()
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting(),
{ teardown: { destroyAfterEach: false } }
)
setupZoneTestEnv({ teardown: { destroyAfterEach: false } })
4 changes: 3 additions & 1 deletion apps/datahub/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import '../../../jest.setup'

setupZoneTestEnv()

class ResizeObserverMock {
observe = jest.fn()
unobserve = jest.fn()
Expand Down
15 changes: 2 additions & 13 deletions apps/demo/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import '../../../jest.setup'

import { getTestBed } from '@angular/core/testing'
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing'

getTestBed().resetTestEnvironment()
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting(),
{ teardown: { destroyAfterEach: false } }
)
setupZoneTestEnv({ teardown: { destroyAfterEach: false } })
4 changes: 3 additions & 1 deletion apps/map-viewer/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import '../../../jest.setup'

setupZoneTestEnv()
4 changes: 3 additions & 1 deletion apps/metadata-converter/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import '../../../jest.setup'

setupZoneTestEnv()
13 changes: 6 additions & 7 deletions apps/metadata-editor/src/app/auth-guard.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { AuthService } from '@geonetwork-ui/api/repository'
import { TestBed } from '@angular/core/testing'
import { of } from 'rxjs'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'

Object.defineProperty(window, 'location', {
value: new URL('http://localhost'),
})
import { Location } from '@angular/common'

describe('AuthGuardService', () => {
let service: AuthGuardService
let location: Location

beforeEach(() => {
return MockBuilder(AuthGuardService)
})
Expand All @@ -26,19 +25,19 @@ describe('AuthGuardService', () => {
}),
],
})
window.location.href = 'http://original.path'
service = TestBed.inject(AuthGuardService)
location = TestBed.inject(Location)
})

it('returns true if the user is logged in', async () => {
jest
.spyOn(TestBed.inject(PlatformServiceInterface), 'isAnonymous')
.mockReturnValue(of(false))
expect(await service.canActivate()).toBe(true)
expect(window.location.href).toBe('http://original.path/')
expect(location.go).not.toHaveBeenCalled()
})
it('redirects the user to the login page if the user is not logged in', async () => {
expect(await service.canActivate()).toBe(false)
expect(window.location.href).toBe('http://login.com/bla?')
expect(location.go).toHaveBeenCalledWith('http://login.com/bla?')
})
})
6 changes: 4 additions & 2 deletions apps/metadata-editor/src/app/auth-guard.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Injectable } from '@angular/core'
import { Location } from '@angular/common'
import { AuthService } from '@geonetwork-ui/api/repository'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
import { firstValueFrom } from 'rxjs'
Expand All @@ -7,14 +8,15 @@ import { firstValueFrom } from 'rxjs'
export class AuthGuardService {
constructor(
private platformService: PlatformServiceInterface,
private authService: AuthService
private authService: AuthService,
private locationService: Location
) {}

// this will redirect the user to the authentication form if required
async canActivate(): Promise<boolean> {
const notLoggedIn = await firstValueFrom(this.platformService.isAnonymous())
if (notLoggedIn) {
window.location.href = this.authService.loginUrl
this.locationService.go(this.authService.loginUrl)
return false
}
return true
Expand Down
14 changes: 2 additions & 12 deletions apps/metadata-editor/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import '../../../jest.setup'

import { CommonModule } from '@angular/common'
import { getTestBed } from '@angular/core/testing'
import { BrowserModule } from '@angular/platform-browser'
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing'
import { RouterModule } from '@angular/router'
import { AvatarServiceInterface } from '@geonetwork-ui/api/repository'
import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface'
Expand All @@ -17,12 +12,7 @@ import { ngMocks } from 'ng-mocks'
import { BehaviorSubject, EMPTY } from 'rxjs'
import { TranslateModule } from '@ngx-translate/core'

getTestBed().resetTestEnvironment()
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting(),
{ teardown: { destroyAfterEach: false } }
)
setupZoneTestEnv()

// ng-mocks global configuration
ngMocks.autoSpy('jest')
Expand Down
15 changes: 2 additions & 13 deletions apps/search/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import '../../../jest.setup'

import { getTestBed } from '@angular/core/testing'
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing'

getTestBed().resetTestEnvironment()
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting(),
{ teardown: { destroyAfterEach: false } }
)
setupZoneTestEnv({ teardown: { destroyAfterEach: false } })
15 changes: 2 additions & 13 deletions apps/webcomponents/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import '../../../jest.setup'

import { getTestBed } from '@angular/core/testing'
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing'

getTestBed().resetTestEnvironment()
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting(),
{ teardown: { destroyAfterEach: false } }
)
setupZoneTestEnv({ teardown: { destroyAfterEach: false } })
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import {
elasticFullResponseFixture,
elasticHitsOnlyFixture,
Expand All @@ -15,6 +15,8 @@ import {
import { TranslateService } from '@ngx-translate/core'
import { Gn4Record } from '../gn4/types/metadata.model'

setupZoneTestEnv()

class MetadataUrlServiceMock {
translate = undefined
getUrl = (uuid) => `http://my.catalog.org/metadata/${uuid}`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import { TestBed } from '@angular/core/testing'
import { elasticLinkFixture } from '@geonetwork-ui/common/fixtures'
import { Gn4FieldMapper } from './gn4.field.mapper'
import { MetadataUrlService } from './metadata-url.service'
import { TranslateService } from '@ngx-translate/core'

setupZoneTestEnv()

class MetadataUrlServiceMock {
translate = undefined
getUrl = () => 'url'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import { TestBed } from '@angular/core/testing'
import { TranslateService } from '@ngx-translate/core'
import { MetadataUrlService } from './metadata-url.service'
import { Configuration } from '@geonetwork-ui/data-access/gn4'

setupZoneTestEnv()

const translateServiceMock = {
currentLang: 'en',
}
Expand Down
10 changes: 5 additions & 5 deletions libs/api/repository/src/lib/gn4/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import { AuthService, LOGIN_URL } from './auth.service'
import { TestBed } from '@angular/core/testing'
import { TranslateService } from '@ngx-translate/core'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { MockProvider } from 'ng-mocks'
import { Location } from '@angular/common'

let loginUrlTokenMock
const translateServiceMock = {
currentLang: 'fr',
}

let windowLocation
Object.defineProperties((global as any).window, {
location: {
get: () => new URL(windowLocation),
},
})

describe('AuthService', () => {
let service: AuthService
Expand All @@ -30,6 +27,9 @@ describe('AuthService', () => {
provide: TranslateService,
useValue: translateServiceMock,
},
MockProvider(Location, {
path: () => windowLocation,
}),
],
imports: [HttpClientTestingModule],
})
Expand Down
8 changes: 5 additions & 3 deletions libs/api/repository/src/lib/gn4/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Inject, Injectable, InjectionToken, Optional } from '@angular/core'
import { LANG_2_TO_3_MAPPER } from '@geonetwork-ui/util/i18n'
import { TranslateService } from '@ngx-translate/core'
import { Location } from '@angular/common'

export const DEFAULT_GN4_LOGIN_URL = `/geonetwork/srv/\${lang3}/catalog.signin?redirect=\${current_url}`
export const LOGIN_URL = new InjectionToken<string>('loginUrl')
Expand All @@ -21,13 +22,13 @@ export class AuthService {

get loginUrl() {
let baseUrl = this.baseLoginUrl
const locationHasQueryParams = !!window.location.search
const locationHasQueryParams = !!new URL(this.location.path()).search
// this is specific to georchestra login URL based on a ?login query param
if (baseUrl.startsWith('${current_url}?') && locationHasQueryParams) {
baseUrl = baseUrl.replace('?', '&')
}
return baseUrl
.replace('${current_url}', window.location.toString())
.replace('${current_url}', new URL(this.location.path()).toString())
.replace('${lang2}', this.translateService.currentLang)
.replace(
'${lang3}',
Expand All @@ -50,6 +51,7 @@ export class AuthService {
@Optional() @Inject(LOGIN_URL) private baseLoginUrlToken: string,
@Optional() @Inject(LOGOUT_URL) private baseLogoutUrlToken: string,
@Optional() @Inject(SETTINGS_URL) private baseSettingsUrlToken: string,
private translateService: TranslateService
private translateService: TranslateService,
private location: Location
) {}
}
4 changes: 3 additions & 1 deletion libs/api/repository/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import '../../../../jest.setup'

setupZoneTestEnv()
15 changes: 2 additions & 13 deletions libs/feature/auth/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import '../../../../jest.setup'

import { getTestBed } from '@angular/core/testing'
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing'

getTestBed().resetTestEnvironment()
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting(),
{ teardown: { destroyAfterEach: false } }
)
setupZoneTestEnv({ teardown: { destroyAfterEach: false } })
15 changes: 2 additions & 13 deletions libs/feature/catalog/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import '../../../../jest.setup'

import { getTestBed } from '@angular/core/testing'
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing'

getTestBed().resetTestEnvironment()
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting(),
{ teardown: { destroyAfterEach: false } }
)
setupZoneTestEnv({ teardown: { destroyAfterEach: false } })
5 changes: 5 additions & 0 deletions libs/feature/dataviz/src/lib/service/data.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { DataService } from './data.service'
import { openDataset } from '@geonetwork-ui/data-fetcher'
import { PROXY_PATH } from '@geonetwork-ui/util/shared'
import { lastValueFrom } from 'rxjs'
import { MockProvider } from 'ng-mocks'
import { Location } from '@angular/common'

const newEndpointCall = jest.fn()

Expand Down Expand Up @@ -637,6 +639,9 @@ describe('DataService', () => {
provide: PROXY_PATH,
useValue: 'http://proxy.local/?url=',
},
MockProvider(Location, {
path: () => 'http://localhost',
}),
],
})
service = TestBed.inject(DataService)
Expand Down
15 changes: 2 additions & 13 deletions libs/feature/dataviz/src/test-setup.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import 'jest-preset-angular/setup-jest'
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'
import '../../../../jest.setup'

import { getTestBed } from '@angular/core/testing'
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting,
} from '@angular/platform-browser-dynamic/testing'

getTestBed().resetTestEnvironment()
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting(),
{ teardown: { destroyAfterEach: false } }
)
setupZoneTestEnv({ teardown: { destroyAfterEach: false } })

class ResizeObserverMock {
observe = jest.fn()
Expand Down
Loading

0 comments on commit 2e1983c

Please sign in to comment.