From 2e1983c4af63506059aa35159076363f02bca85b Mon Sep 17 00:00:00 2001 From: Olivia Guyot Date: Tue, 7 Jan 2025 17:53:38 +0100 Subject: [PATCH] chore: fix various test failures part III Also fix warnings --- apps/datafeeder/src/test-setup.ts | 15 +------ apps/datahub/src/test-setup.ts | 4 +- apps/demo/src/test-setup.ts | 15 +------ apps/map-viewer/src/test-setup.ts | 4 +- apps/metadata-converter/src/test-setup.ts | 4 +- .../src/app/auth-guard.service.spec.ts | 13 +++--- .../src/app/auth-guard.service.ts | 6 ++- apps/metadata-editor/src/test-setup.ts | 14 +----- apps/search/src/test-setup.ts | 15 +------ apps/webcomponents/src/test-setup.ts | 15 +------ .../src/lib/gn4/gn4.converter.spec.ts | 4 +- .../src/lib/gn4/gn4.field.mapper.spec.ts | 4 +- .../src/lib/gn4/metadata-url.service.spec.ts | 4 +- .../src/lib/gn4/auth/auth.service.spec.ts | 10 ++--- .../src/lib/gn4/auth/auth.service.ts | 8 ++-- libs/api/repository/src/test-setup.ts | 4 +- libs/feature/auth/src/test-setup.ts | 15 +------ libs/feature/catalog/src/test-setup.ts | 15 +------ .../src/lib/service/data.service.spec.ts | 5 +++ libs/feature/dataviz/src/test-setup.ts | 15 +------ libs/feature/editor/src/test-setup.ts | 15 +------ libs/feature/map/src/test-setup.ts | 15 +------ libs/feature/notifications/src/test-setup.ts | 15 +------ .../external-viewer-button.component.spec.ts | 44 ++++++------------- libs/feature/record/src/test-setup.ts | 15 +------ libs/feature/router/src/test-setup.ts | 15 +------ .../favorite-star.component.spec.ts | 5 +++ libs/feature/search/src/test-setup.ts | 14 +----- libs/ui/catalog/src/test-setup.ts | 15 +------ libs/ui/dataviz/src/test-setup.ts | 4 +- libs/ui/elements/src/test-setup.ts | 15 +------ libs/ui/inputs/src/test-setup.ts | 15 +------ libs/ui/layout/src/test-setup.ts | 15 +------ libs/ui/map/src/test-setup.ts | 15 +------ libs/ui/search/src/test-setup.ts | 15 +------ libs/ui/widgets/src/test-setup.ts | 15 +------ libs/util/app-config/src/test-setup.ts | 17 +------ libs/util/i18n/src/test-setup.ts | 15 +------ .../src/lib/services/proxy.service.spec.ts | 12 +++-- .../shared/src/lib/services/proxy.service.ts | 10 +++-- libs/util/shared/src/test-setup.ts | 15 +------ 41 files changed, 128 insertions(+), 377 deletions(-) diff --git a/apps/datafeeder/src/test-setup.ts b/apps/datafeeder/src/test-setup.ts index 1261469e19..c4e518a552 100644 --- a/apps/datafeeder/src/test-setup.ts +++ b/apps/datafeeder/src/test-setup.ts @@ -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 } }) diff --git a/apps/datahub/src/test-setup.ts b/apps/datahub/src/test-setup.ts index 5626c49f29..337400835b 100644 --- a/apps/datahub/src/test-setup.ts +++ b/apps/datahub/src/test-setup.ts @@ -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() diff --git a/apps/demo/src/test-setup.ts b/apps/demo/src/test-setup.ts index 1261469e19..c4e518a552 100644 --- a/apps/demo/src/test-setup.ts +++ b/apps/demo/src/test-setup.ts @@ -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 } }) diff --git a/apps/map-viewer/src/test-setup.ts b/apps/map-viewer/src/test-setup.ts index 2de8264de0..c807f6f012 100644 --- a/apps/map-viewer/src/test-setup.ts +++ b/apps/map-viewer/src/test-setup.ts @@ -1,2 +1,4 @@ -import 'jest-preset-angular/setup-jest' +import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone' import '../../../jest.setup' + +setupZoneTestEnv() diff --git a/apps/metadata-converter/src/test-setup.ts b/apps/metadata-converter/src/test-setup.ts index 2de8264de0..c807f6f012 100644 --- a/apps/metadata-converter/src/test-setup.ts +++ b/apps/metadata-converter/src/test-setup.ts @@ -1,2 +1,4 @@ -import 'jest-preset-angular/setup-jest' +import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone' import '../../../jest.setup' + +setupZoneTestEnv() diff --git a/apps/metadata-editor/src/app/auth-guard.service.spec.ts b/apps/metadata-editor/src/app/auth-guard.service.spec.ts index 8966947d11..5f5f0d8304 100644 --- a/apps/metadata-editor/src/app/auth-guard.service.spec.ts +++ b/apps/metadata-editor/src/app/auth-guard.service.spec.ts @@ -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) }) @@ -26,8 +25,8 @@ 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 () => { @@ -35,10 +34,10 @@ describe('AuthGuardService', () => { .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?') }) }) diff --git a/apps/metadata-editor/src/app/auth-guard.service.ts b/apps/metadata-editor/src/app/auth-guard.service.ts index fa7087f610..a81e39fa21 100644 --- a/apps/metadata-editor/src/app/auth-guard.service.ts +++ b/apps/metadata-editor/src/app/auth-guard.service.ts @@ -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' @@ -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 { 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 diff --git a/apps/metadata-editor/src/test-setup.ts b/apps/metadata-editor/src/test-setup.ts index c84439f7f4..76f451e8bc 100644 --- a/apps/metadata-editor/src/test-setup.ts +++ b/apps/metadata-editor/src/test-setup.ts @@ -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' @@ -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') diff --git a/apps/search/src/test-setup.ts b/apps/search/src/test-setup.ts index 1261469e19..c4e518a552 100644 --- a/apps/search/src/test-setup.ts +++ b/apps/search/src/test-setup.ts @@ -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 } }) diff --git a/apps/webcomponents/src/test-setup.ts b/apps/webcomponents/src/test-setup.ts index 1261469e19..c4e518a552 100644 --- a/apps/webcomponents/src/test-setup.ts +++ b/apps/webcomponents/src/test-setup.ts @@ -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 } }) diff --git a/libs/api/metadata-converter/src/lib/gn4/gn4.converter.spec.ts b/libs/api/metadata-converter/src/lib/gn4/gn4.converter.spec.ts index b40e35ce13..08e0e7a888 100644 --- a/libs/api/metadata-converter/src/lib/gn4/gn4.converter.spec.ts +++ b/libs/api/metadata-converter/src/lib/gn4/gn4.converter.spec.ts @@ -1,4 +1,4 @@ -import 'jest-preset-angular/setup-jest' +import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone' import { elasticFullResponseFixture, elasticHitsOnlyFixture, @@ -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}` diff --git a/libs/api/metadata-converter/src/lib/gn4/gn4.field.mapper.spec.ts b/libs/api/metadata-converter/src/lib/gn4/gn4.field.mapper.spec.ts index 1c10aced37..659a48ff74 100644 --- a/libs/api/metadata-converter/src/lib/gn4/gn4.field.mapper.spec.ts +++ b/libs/api/metadata-converter/src/lib/gn4/gn4.field.mapper.spec.ts @@ -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' diff --git a/libs/api/metadata-converter/src/lib/gn4/metadata-url.service.spec.ts b/libs/api/metadata-converter/src/lib/gn4/metadata-url.service.spec.ts index 9499a4c12c..6ffe28a0ae 100644 --- a/libs/api/metadata-converter/src/lib/gn4/metadata-url.service.spec.ts +++ b/libs/api/metadata-converter/src/lib/gn4/metadata-url.service.spec.ts @@ -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', } diff --git a/libs/api/repository/src/lib/gn4/auth/auth.service.spec.ts b/libs/api/repository/src/lib/gn4/auth/auth.service.spec.ts index cc3fc4d381..052197f000 100644 --- a/libs/api/repository/src/lib/gn4/auth/auth.service.spec.ts +++ b/libs/api/repository/src/lib/gn4/auth/auth.service.spec.ts @@ -2,6 +2,8 @@ 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 = { @@ -9,11 +11,6 @@ const translateServiceMock = { } let windowLocation -Object.defineProperties((global as any).window, { - location: { - get: () => new URL(windowLocation), - }, -}) describe('AuthService', () => { let service: AuthService @@ -30,6 +27,9 @@ describe('AuthService', () => { provide: TranslateService, useValue: translateServiceMock, }, + MockProvider(Location, { + path: () => windowLocation, + }), ], imports: [HttpClientTestingModule], }) diff --git a/libs/api/repository/src/lib/gn4/auth/auth.service.ts b/libs/api/repository/src/lib/gn4/auth/auth.service.ts index da6d7fae3b..79a09f1a7d 100644 --- a/libs/api/repository/src/lib/gn4/auth/auth.service.ts +++ b/libs/api/repository/src/lib/gn4/auth/auth.service.ts @@ -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('loginUrl') @@ -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}', @@ -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 ) {} } diff --git a/libs/api/repository/src/test-setup.ts b/libs/api/repository/src/test-setup.ts index 4c58ab7724..146980f35b 100644 --- a/libs/api/repository/src/test-setup.ts +++ b/libs/api/repository/src/test-setup.ts @@ -1,2 +1,4 @@ -import 'jest-preset-angular/setup-jest' +import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone' import '../../../../jest.setup' + +setupZoneTestEnv() diff --git a/libs/feature/auth/src/test-setup.ts b/libs/feature/auth/src/test-setup.ts index 70e41af1c8..88f061f3da 100644 --- a/libs/feature/auth/src/test-setup.ts +++ b/libs/feature/auth/src/test-setup.ts @@ -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 } }) diff --git a/libs/feature/catalog/src/test-setup.ts b/libs/feature/catalog/src/test-setup.ts index 70e41af1c8..88f061f3da 100644 --- a/libs/feature/catalog/src/test-setup.ts +++ b/libs/feature/catalog/src/test-setup.ts @@ -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 } }) diff --git a/libs/feature/dataviz/src/lib/service/data.service.spec.ts b/libs/feature/dataviz/src/lib/service/data.service.spec.ts index a26e84a814..36552e5df6 100644 --- a/libs/feature/dataviz/src/lib/service/data.service.spec.ts +++ b/libs/feature/dataviz/src/lib/service/data.service.spec.ts @@ -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() @@ -637,6 +639,9 @@ describe('DataService', () => { provide: PROXY_PATH, useValue: 'http://proxy.local/?url=', }, + MockProvider(Location, { + path: () => 'http://localhost', + }), ], }) service = TestBed.inject(DataService) diff --git a/libs/feature/dataviz/src/test-setup.ts b/libs/feature/dataviz/src/test-setup.ts index 19b403b6c7..beca8bef5b 100644 --- a/libs/feature/dataviz/src/test-setup.ts +++ b/libs/feature/dataviz/src/test-setup.ts @@ -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() diff --git a/libs/feature/editor/src/test-setup.ts b/libs/feature/editor/src/test-setup.ts index 8c7932e609..dfe2bfffc9 100644 --- a/libs/feature/editor/src/test-setup.ts +++ b/libs/feature/editor/src/test-setup.ts @@ -1,11 +1,5 @@ -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' import { ngMocks } from 'ng-mocks' import { CommonModule } from '@angular/common' import { BrowserModule } from '@angular/platform-browser' @@ -13,12 +7,7 @@ import { TranslateModule } from '@ngx-translate/core' import { EditorFacade } from './lib/+state/editor.facade' import { EMPTY } from 'rxjs' -getTestBed().resetTestEnvironment() -getTestBed().initTestEnvironment( - BrowserDynamicTestingModule, - platformBrowserDynamicTesting(), - { teardown: { destroyAfterEach: false } } -) +setupZoneTestEnv({ teardown: { destroyAfterEach: false } }) // ng-mocks global configuration ngMocks.autoSpy('jest') diff --git a/libs/feature/map/src/test-setup.ts b/libs/feature/map/src/test-setup.ts index 19b403b6c7..beca8bef5b 100644 --- a/libs/feature/map/src/test-setup.ts +++ b/libs/feature/map/src/test-setup.ts @@ -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() diff --git a/libs/feature/notifications/src/test-setup.ts b/libs/feature/notifications/src/test-setup.ts index 19b403b6c7..beca8bef5b 100644 --- a/libs/feature/notifications/src/test-setup.ts +++ b/libs/feature/notifications/src/test-setup.ts @@ -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() diff --git a/libs/feature/record/src/lib/external-viewer-button/external-viewer-button.component.spec.ts b/libs/feature/record/src/lib/external-viewer-button/external-viewer-button.component.spec.ts index efa4b13be1..c1e8b1544b 100644 --- a/libs/feature/record/src/lib/external-viewer-button/external-viewer-button.component.spec.ts +++ b/libs/feature/record/src/lib/external-viewer-button/external-viewer-button.component.spec.ts @@ -10,10 +10,17 @@ import { ButtonComponent } from '@geonetwork-ui/ui/inputs' import { importProvidersFrom } from '@angular/core' import { TranslateModule } from '@ngx-translate/core' +window.open = jest.fn().mockImplementation(() => window) +window.focus = jest.fn().mockImplementation(() => window) + describe('ExternalViewerButtonComponent', () => { let component: ExternalViewerButtonComponent let fixture: ComponentFixture + beforeEach(() => { + jest.clearAllMocks() + }) + beforeEach(() => MockBuilder(ExternalViewerButtonComponent)) beforeEach(async () => { @@ -53,9 +60,7 @@ describe('ExternalViewerButtonComponent', () => { describe('with mapConfig and valid external links', () => { let buttonComponent: ButtonComponent let componentSpy - let windowSpy - const openMock = jest.fn().mockReturnThis() - const focusMock = jest.fn().mockReturnThis() + describe('with mapConfig and WMS link', () => { beforeEach(() => { component.link = { @@ -77,30 +82,23 @@ describe('ExternalViewerButtonComponent', () => { By.directive(ButtonComponent) ).componentInstance componentSpy = jest.spyOn(component, 'openInExternalViewer') - windowSpy = jest - .spyOn(global, 'window', 'get') - .mockImplementation(() => ({ - open: openMock, - focus: focusMock, - })) buttonComponent.buttonClick.emit() }) afterEach(() => { componentSpy.mockRestore() - windowSpy.mockRestore() }) it('calls openInExternalViewer', () => { expect(component.openInExternalViewer).toHaveBeenCalled() }) it('opens window in new tab with URL including WMS link params', () => { - expect(openMock).toHaveBeenCalledWith( + expect(window.open).toHaveBeenCalledWith( 'https://example.com/myviewer/#/?actions=[{"type":"CATALOG:ADD_LAYERS_FROM_CATALOGS","layers":["layername"],"sources":[{"url":"http%3A%2F%2Fexample.com%2Fows%3Fservice%3Dwms%26request%3Dgetcapabilities","type":"wms"}]}]', '_blank' ) }) it('focuses window', () => { - expect(focusMock).toHaveBeenCalled() + expect(window.focus).toHaveBeenCalled() }) }) }) @@ -125,30 +123,23 @@ describe('ExternalViewerButtonComponent', () => { By.directive(ButtonComponent) ).componentInstance componentSpy = jest.spyOn(component, 'openInExternalViewer') - windowSpy = jest - .spyOn(global, 'window', 'get') - .mockImplementation(() => ({ - open: openMock, - focus: focusMock, - })) buttonComponent.buttonClick.emit() }) afterEach(() => { componentSpy.mockRestore() - windowSpy.mockRestore() }) it('calls openInExternalViewer', () => { expect(component.openInExternalViewer).toHaveBeenCalled() }) it('opens window in new tab with URL including WFS link params', () => { - expect(openMock).toHaveBeenCalledWith( + expect(window.open).toHaveBeenCalledWith( 'https://example.com/myviewer/#/?actions=[{"type":"CATALOG:ADD_LAYERS_FROM_CATALOGS","layers":["layername"],"sources":[{"url":"http%3A%2F%2Fexample.com%2Fows%3Fservice%3Dwfs%26request%3Dgetcapabilities","type":"wfs"}]}]', '_blank' ) }) it('focuses window', () => { - expect(focusMock).toHaveBeenCalled() + expect(window.focus).toHaveBeenCalled() }) }) }) @@ -170,30 +161,23 @@ describe('ExternalViewerButtonComponent', () => { By.directive(ButtonComponent) ).componentInstance componentSpy = jest.spyOn(component, 'openInExternalViewer') - windowSpy = jest - .spyOn(global, 'window', 'get') - .mockImplementation(() => ({ - open: openMock, - focus: focusMock, - })) buttonComponent.buttonClick.emit() }) afterEach(() => { componentSpy.mockRestore() - windowSpy.mockRestore() }) it('calls openInExternalViewer', () => { expect(component.openInExternalViewer).toHaveBeenCalled() }) it('opens window in new tab with URL including link params', () => { - expect(openMock).toHaveBeenCalledWith( + expect(window.open).toHaveBeenCalledWith( 'https://example.com/myviewer/#/?actions=[{"type":"CATALOG:ADD_LAYERS_FROM_CATALOGS","layers":["externalviewer.dataset.unnamed"],"sources":[{"url":"http%3A%2F%2Fexample.com%2Fsomespatialdata.geojson","type":"geojson"}]}]', '_blank' ) }) it('focuses window', () => { - expect(focusMock).toHaveBeenCalled() + expect(window.focus).toHaveBeenCalled() }) }) }) diff --git a/libs/feature/record/src/test-setup.ts b/libs/feature/record/src/test-setup.ts index 70e41af1c8..88f061f3da 100644 --- a/libs/feature/record/src/test-setup.ts +++ b/libs/feature/record/src/test-setup.ts @@ -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 } }) diff --git a/libs/feature/router/src/test-setup.ts b/libs/feature/router/src/test-setup.ts index 70e41af1c8..88f061f3da 100644 --- a/libs/feature/router/src/test-setup.ts +++ b/libs/feature/router/src/test-setup.ts @@ -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 } }) diff --git a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts index d58ce87248..21217487eb 100644 --- a/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts +++ b/libs/feature/search/src/lib/favorites/favorite-star/favorite-star.component.spec.ts @@ -9,6 +9,8 @@ import tippy from 'tippy.js' import { datasetRecordsFixture } from '@geonetwork-ui/common/fixtures' import { FavoritesService } from '@geonetwork-ui/api/repository' import { PlatformServiceInterface } from '@geonetwork-ui/common/domain/platform.service.interface' +import { MockProvider } from 'ng-mocks' +import { Location } from '@angular/common' tippy = jest.fn() const isAnonymous$ = new BehaviorSubject(false) @@ -55,6 +57,9 @@ describe('FavoriteStarComponent', () => { provide: TranslateService, useClass: TranslateServiceMock, }, + MockProvider(Location, { + path: () => 'http://localhost', + }), ], schemas: [NO_ERRORS_SCHEMA], }) diff --git a/libs/feature/search/src/test-setup.ts b/libs/feature/search/src/test-setup.ts index 36ba8c5ee4..28aef25ec9 100644 --- a/libs/feature/search/src/test-setup.ts +++ b/libs/feature/search/src/test-setup.ts @@ -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 { RecordsRepositoryInterface } from '@geonetwork-ui/common/domain/repository/records-repository.interface' import { TranslateModule } from '@ngx-translate/core' import { ngMocks } from 'ng-mocks' @@ -15,12 +10,7 @@ import { BehaviorSubject } from 'rxjs' import { SearchFacade } from './lib/state/search.facade' import { SearchService } from './lib/utils/service/search.service' -getTestBed().resetTestEnvironment() -getTestBed().initTestEnvironment( - BrowserDynamicTestingModule, - platformBrowserDynamicTesting(), - { teardown: { destroyAfterEach: false } } -) +setupZoneTestEnv({ teardown: { destroyAfterEach: false } }) // ng-mocks global configuration ngMocks.autoSpy('jest') diff --git a/libs/ui/catalog/src/test-setup.ts b/libs/ui/catalog/src/test-setup.ts index 70e41af1c8..88f061f3da 100644 --- a/libs/ui/catalog/src/test-setup.ts +++ b/libs/ui/catalog/src/test-setup.ts @@ -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 } }) diff --git a/libs/ui/dataviz/src/test-setup.ts b/libs/ui/dataviz/src/test-setup.ts index 4c58ab7724..146980f35b 100644 --- a/libs/ui/dataviz/src/test-setup.ts +++ b/libs/ui/dataviz/src/test-setup.ts @@ -1,2 +1,4 @@ -import 'jest-preset-angular/setup-jest' +import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone' import '../../../../jest.setup' + +setupZoneTestEnv() diff --git a/libs/ui/elements/src/test-setup.ts b/libs/ui/elements/src/test-setup.ts index f83b97ee8e..095be2e565 100644 --- a/libs/ui/elements/src/test-setup.ts +++ b/libs/ui/elements/src/test-setup.ts @@ -1,23 +1,12 @@ -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' import { ngMocks } from 'ng-mocks' import { CommonModule } from '@angular/common' import { BrowserModule } from '@angular/platform-browser' import { TranslateModule } from '@ngx-translate/core' import { MatTooltipModule } from '@angular/material/tooltip' -getTestBed().resetTestEnvironment() -getTestBed().initTestEnvironment( - BrowserDynamicTestingModule, - platformBrowserDynamicTesting(), - { teardown: { destroyAfterEach: false } } -) +setupZoneTestEnv({ teardown: { destroyAfterEach: false } }) class ResizeObserverMock { observe = jest.fn() diff --git a/libs/ui/inputs/src/test-setup.ts b/libs/ui/inputs/src/test-setup.ts index 70e41af1c8..88f061f3da 100644 --- a/libs/ui/inputs/src/test-setup.ts +++ b/libs/ui/inputs/src/test-setup.ts @@ -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 } }) diff --git a/libs/ui/layout/src/test-setup.ts b/libs/ui/layout/src/test-setup.ts index 70e41af1c8..88f061f3da 100644 --- a/libs/ui/layout/src/test-setup.ts +++ b/libs/ui/layout/src/test-setup.ts @@ -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 } }) diff --git a/libs/ui/map/src/test-setup.ts b/libs/ui/map/src/test-setup.ts index a397e1cfe3..8e232dbd81 100644 --- a/libs/ui/map/src/test-setup.ts +++ b/libs/ui/map/src/test-setup.ts @@ -1,11 +1,5 @@ -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' import { ngMocks } from 'ng-mocks' import { BASEMAP_LAYERS, @@ -13,12 +7,7 @@ import { MAP_VIEW_CONSTRAINTS, } from './lib/components/map-container/map-settings.token' -getTestBed().resetTestEnvironment() -getTestBed().initTestEnvironment( - BrowserDynamicTestingModule, - platformBrowserDynamicTesting(), - { teardown: { destroyAfterEach: false } } -) +setupZoneTestEnv({ teardown: { destroyAfterEach: false } }) ngMocks.globalKeep(DO_NOT_USE_DEFAULT_BASEMAP) ngMocks.globalKeep(BASEMAP_LAYERS) diff --git a/libs/ui/search/src/test-setup.ts b/libs/ui/search/src/test-setup.ts index 70e41af1c8..88f061f3da 100644 --- a/libs/ui/search/src/test-setup.ts +++ b/libs/ui/search/src/test-setup.ts @@ -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 } }) diff --git a/libs/ui/widgets/src/test-setup.ts b/libs/ui/widgets/src/test-setup.ts index 70e41af1c8..88f061f3da 100644 --- a/libs/ui/widgets/src/test-setup.ts +++ b/libs/ui/widgets/src/test-setup.ts @@ -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 } }) diff --git a/libs/util/app-config/src/test-setup.ts b/libs/util/app-config/src/test-setup.ts index 5366337161..88f061f3da 100644 --- a/libs/util/app-config/src/test-setup.ts +++ b/libs/util/app-config/src/test-setup.ts @@ -1,17 +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' -import fetch from 'fetch-mock-jest' -global.fetch = fetch as never - -getTestBed().resetTestEnvironment() -getTestBed().initTestEnvironment( - BrowserDynamicTestingModule, - platformBrowserDynamicTesting(), - { teardown: { destroyAfterEach: false } } -) +setupZoneTestEnv({ teardown: { destroyAfterEach: false } }) diff --git a/libs/util/i18n/src/test-setup.ts b/libs/util/i18n/src/test-setup.ts index 70e41af1c8..88f061f3da 100644 --- a/libs/util/i18n/src/test-setup.ts +++ b/libs/util/i18n/src/test-setup.ts @@ -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 } }) diff --git a/libs/util/shared/src/lib/services/proxy.service.spec.ts b/libs/util/shared/src/lib/services/proxy.service.spec.ts index b263df45b8..a4a7fd673d 100644 --- a/libs/util/shared/src/lib/services/proxy.service.spec.ts +++ b/libs/util/shared/src/lib/services/proxy.service.spec.ts @@ -1,12 +1,7 @@ import { PROXY_PATH, ProxyService } from './proxy.service' import { TestBed } from '@angular/core/testing' - -// mock window.location -// workaround for https://github.com/nodejs/node/issues/47563 -;(global as any).window ??= Object.create(window) -Object.defineProperty(window, 'location', { - value: new URL('http://myhost:1234/my/path?abc'), -}) +import { MockProvider } from 'ng-mocks' +import { Location } from '@angular/common' let proxyPath @@ -19,6 +14,9 @@ describe('ProxyService', () => { provide: PROXY_PATH, useFactory: () => proxyPath, }, + MockProvider(Location, { + path: () => 'http://myhost:1234/wms', + }), ], }) }) diff --git a/libs/util/shared/src/lib/services/proxy.service.ts b/libs/util/shared/src/lib/services/proxy.service.ts index 63e7e096f5..2b9fa162cb 100644 --- a/libs/util/shared/src/lib/services/proxy.service.ts +++ b/libs/util/shared/src/lib/services/proxy.service.ts @@ -1,4 +1,5 @@ import { Inject, Injectable, InjectionToken, Optional } from '@angular/core' +import { Location } from '@angular/common' export const PROXY_PATH = new InjectionToken('proxyPath') @@ -6,7 +7,10 @@ export const PROXY_PATH = new InjectionToken('proxyPath') providedIn: 'root', }) export class ProxyService { - constructor(@Optional() @Inject(PROXY_PATH) private proxyPath: string) {} + constructor( + @Optional() @Inject(PROXY_PATH) private proxyPath: string, + private location: Location + ) {} /** * Transforms the URL to go through a proxy specified with the PROXY_PATH @@ -16,7 +20,7 @@ export class ProxyService { getProxiedUrl(url: string): string { if (!this.proxyPath) return url const urlObj = new URL(url) - const current = window.location + const current = new URL(this.location.path()) const proxyUrl = new URL(this.proxyPath, current.toString()).toString() if ( current.hostname === urlObj.hostname && @@ -28,7 +32,7 @@ export class ProxyService { if (url.indexOf(proxyUrl) === 0) return url return new URL( `${this.proxyPath}${encodeURIComponent(url)}`, - window.location.toString() + this.location.path() ).toString() } } diff --git a/libs/util/shared/src/test-setup.ts b/libs/util/shared/src/test-setup.ts index 70e41af1c8..88f061f3da 100644 --- a/libs/util/shared/src/test-setup.ts +++ b/libs/util/shared/src/test-setup.ts @@ -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 } })