Skip to content

Commit 3f7d8ee

Browse files
FrancescoMolinaroAdamF42
authored andcommitted
Merged in task/dspace-cris-2023_02_x/DSC-2445 (pull request DSpace#3404)
Task/dspace cris 2023 02 x/DSC-2445 Approved-by: Andrea Barbasso Approved-by: Fapohunda, Adamo
2 parents c8a5356 + 4186a8f commit 3f7d8ee

2 files changed

Lines changed: 53 additions & 6 deletions

File tree

src/app/app.component.spec.ts

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Store, StoreModule } from '@ngrx/store';
2-
import { ComponentFixture, inject, TestBed, waitForAsync } from '@angular/core/testing';
2+
import { ComponentFixture, fakeAsync, inject, TestBed, tick, waitForAsync } from '@angular/core/testing';
33
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
44
import { CommonModule } from '@angular/common';
5-
import { ActivatedRoute, Router } from '@angular/router';
5+
import { ActivatedRoute, NavigationStart, Router } from '@angular/router';
66
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
77

88
// Load the implementations that should be tested
@@ -25,7 +25,6 @@ import { HostWindowService } from './shared/host-window.service';
2525
import { HostWindowServiceStub } from './shared/testing/host-window-service.stub';
2626
import { RouteService } from './core/services/route.service';
2727
import { MockActivatedRoute } from './shared/mocks/active-router.mock';
28-
import { RouterMock } from './shared/mocks/router.mock';
2928
import { Angulartics2DSpace } from './statistics/angulartics/dspace-provider';
3029
import { storeModuleConfig } from './app.reducer';
3130
import { LocaleService } from './core/locale/locale.service';
@@ -34,7 +33,7 @@ import { provideMockStore } from '@ngrx/store/testing';
3433
import { ThemeService } from './shared/theme-support/theme.service';
3534
import { getMockThemeService } from './shared/mocks/theme-service.mock';
3635
import { BreadcrumbsService } from './breadcrumbs/breadcrumbs.service';
37-
import { of } from 'rxjs';
36+
import { of, Subject } from 'rxjs';
3837
import { APP_CONFIG } from '../config/app-config.interface';
3938
import { environment } from '../environments/environment';
4039
import { KlaroService } from './shared/cookies/klaro.service';
@@ -47,6 +46,8 @@ const initialState = {
4746
core: { auth: { loading: false } }
4847
};
4948

49+
const itemPageUrl = '/entities/publication/3b6ef8e8-15a1-4607-abf8-2a6fbd572346';
50+
5051
export function getMockLocaleService(): LocaleService {
5152
return jasmine.createSpyObj('LocaleService', {
5253
setCurrentLanguageCode: jasmine.createSpy('setCurrentLanguageCode')
@@ -59,13 +60,20 @@ describe('App component', () => {
5960
let routeServiceMock;
6061
let klaroServiceSpy: jasmine.SpyObj<KlaroService>;
6162
let datadogRumServiceSpy: jasmine.SpyObj<DatadogRumService>;
63+
let routerEventsObs: Subject<any>;
64+
let routerMock: Router;
6265

6366
const getDefaultTestBedConf = () => {
6467
breadcrumbsServiceSpy = jasmine.createSpyObj(['listenForRouteChanges']);
6568
routeServiceMock = jasmine.createSpyObj('RouterService', {
6669
getCurrentUrl: of('/home')
6770
});
6871

72+
routerEventsObs = new Subject<any>();
73+
routerMock = jasmine.createSpyObj([], {
74+
events: routerEventsObs,
75+
});
76+
6977
klaroServiceSpy = jasmine.createSpyObj('KlaroService', {
7078
getSavedPreferences: jasmine.createSpy('getSavedPreferences'),
7179
watchConsentUpdates: jasmine.createSpy('watchConsentUpdates')
@@ -95,7 +103,7 @@ describe('App component', () => {
95103
{ provide: MetadataService, useValue: new MetadataServiceMock() },
96104
{ provide: Angulartics2DSpace, useValue: new AngularticsProviderMock() },
97105
{ provide: AuthService, useValue: new AuthServiceMock() },
98-
{ provide: Router, useValue: new RouterMock() },
106+
{ provide: Router, useValue: routerMock },
99107
{ provide: ActivatedRoute, useValue: new MockActivatedRoute() },
100108
{ provide: MenuService, useValue: menuService },
101109
{ provide: CSSVariableService, useClass: CSSVariableServiceStub },
@@ -151,4 +159,32 @@ describe('App component', () => {
151159
});
152160

153161
});
162+
163+
describe('isRouteLoading$ handling', () => {
164+
165+
it('should not show loading for item page', fakeAsync(() => {
166+
routeServiceMock.getCurrentUrl.and.returnValue(of(itemPageUrl));
167+
routerEventsObs.next(new NavigationStart(1, itemPageUrl));
168+
fixture.detectChanges();
169+
tick();
170+
expect(comp.isRouteLoading$.value).toBeFalse();
171+
}));
172+
173+
it('should show loading for item page administrative edit', fakeAsync(() => {
174+
routeServiceMock.getCurrentUrl.and.returnValue(of(itemPageUrl));
175+
routerEventsObs.next(new NavigationStart(2, itemPageUrl + '/edit'));
176+
fixture.detectChanges();
177+
tick();
178+
expect(comp.isRouteLoading$.value).toBeTrue();
179+
}));
180+
181+
it('should not show loading navigating between item pages in administrative edit', fakeAsync(() => {
182+
routeServiceMock.getCurrentUrl.and.returnValue(of(itemPageUrl + '/edit'));
183+
routerEventsObs.next(new NavigationStart(2, itemPageUrl + '/edit/status'));
184+
fixture.detectChanges();
185+
tick();
186+
expect(comp.isRouteLoading$.value).toBeFalse();
187+
}));
188+
189+
});
154190
});

src/app/app.component.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ export class AppComponent implements OnInit, AfterViewInit {
134134
))
135135
).subscribe(([currentUrl, event]: [string, RouterEvent]) => {
136136
if (event instanceof NavigationStart) {
137-
if (!(currentUrl.startsWith('/entities' || getEditItemPageRoute()) || currentUrl.startsWith(getWorkspaceItemModuleRoute()) || currentUrl.startsWith(getWorkflowItemModuleRoute()))) {
137+
const nextUrl = event.url;
138+
if (!this.shouldSkipLoadingStatus(currentUrl, nextUrl)) {
138139
distinctNext(this.isRouteLoading$, true);
139140
}
140141
// distinctNext(this.isRouteLoading$, true);
@@ -177,4 +178,14 @@ export class AppComponent implements OnInit, AfterViewInit {
177178
});
178179
}
179180

181+
private shouldSkipLoadingStatus(currentUrl: string, nextUrl: string): boolean {
182+
return ((currentUrl.startsWith('/entities') || currentUrl.startsWith(getEditItemPageRoute())) && !(this.isAdministrativeEditItemPageRoute(nextUrl, currentUrl)))
183+
|| currentUrl.startsWith(getWorkspaceItemModuleRoute()) || currentUrl.startsWith(getWorkflowItemModuleRoute());
184+
}
185+
186+
private isAdministrativeEditItemPageRoute(nextUrl: string, currentUrl: string): boolean {
187+
const editPageRegEx = /\/(entities\/[^\/]+|items)\/[0-9a-f-]{36}\/edit(?:\/.*)?$/;
188+
return editPageRegEx.test(nextUrl) && !editPageRegEx.test(currentUrl);
189+
}
190+
180191
}

0 commit comments

Comments
 (0)