Skip to content

Commit ac0a14b

Browse files
FrancescoMolinaroAdamF42
authored andcommitted
Merged in task/dspace-cris-2024_02_x/DSC-2445 (pull request DSpace#3406)
[DSC-2445] fix loading handling for item administrative view, add tests Approved-by: Andrea Barbasso Approved-by: Fapohunda, Adamo
2 parents f92cae0 + ee5c78e commit ac0a14b

2 files changed

Lines changed: 57 additions & 4 deletions

File tree

src/app/app.component.spec.ts

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import { CommonModule } from '@angular/common';
22
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
33
import {
44
ComponentFixture,
5+
fakeAsync,
56
inject,
67
TestBed,
8+
tick,
79
waitForAsync,
810
} from '@angular/core/testing';
911
import {
1012
ActivatedRoute,
13+
NavigationStart,
1114
Router,
1215
} from '@angular/router';
1316
import {
@@ -19,7 +22,10 @@ import {
1922
TranslateLoader,
2023
TranslateModule,
2124
} from '@ngx-translate/core';
22-
import { of } from 'rxjs';
25+
import {
26+
of,
27+
Subject,
28+
} from 'rxjs';
2329

2430
import { APP_CONFIG } from '../config/app-config.interface';
2531
import { environment } from '../environments/environment';
@@ -46,7 +52,6 @@ import { MockActivatedRoute } from './shared/mocks/active-router.mock';
4652
import { AngularticsProviderMock } from './shared/mocks/angulartics-provider.service.mock';
4753
import { AuthServiceMock } from './shared/mocks/auth.service.mock';
4854
import { HeadTagServiceMock } from './shared/mocks/head-tag-service.mock';
49-
import { RouterMock } from './shared/mocks/router.mock';
5055
import { getMockThemeService } from './shared/mocks/theme-service.mock';
5156
import { TranslateLoaderMock } from './shared/mocks/translate-loader.mock';
5257
import { CSSVariableService } from './shared/sass-helper/css-variable.service';
@@ -65,6 +70,8 @@ const initialState = {
6570
core: { auth: { loading: false } },
6671
};
6772

73+
const itemPageUrl = '/entities/publication/3b6ef8e8-15a1-4607-abf8-2a6fbd572346';
74+
6875
export function getMockLocaleService(): LocaleService {
6976
return jasmine.createSpyObj('LocaleService', {
7077
setCurrentLanguageCode: jasmine.createSpy('setCurrentLanguageCode'),
@@ -77,13 +84,20 @@ describe('App component', () => {
7784
let routeServiceMock;
7885
let klaroServiceSpy: jasmine.SpyObj<KlaroService>;
7986
let datadogRumServiceSpy: jasmine.SpyObj<DatadogRumService>;
87+
let routerEventsObs: Subject<any>;
88+
let routerMock: Router;
8089

8190
const getDefaultTestBedConf = () => {
8291
breadcrumbsServiceSpy = jasmine.createSpyObj(['listenForRouteChanges']);
8392
routeServiceMock = jasmine.createSpyObj('RouterService', {
8493
getCurrentUrl: of('/home'),
8594
});
8695

96+
routerEventsObs = new Subject<any>();
97+
routerMock = jasmine.createSpyObj([], {
98+
events: routerEventsObs,
99+
});
100+
87101
klaroServiceSpy = jasmine.createSpyObj('KlaroService', {
88102
getSavedPreferences: jasmine.createSpy('getSavedPreferences'),
89103
watchConsentUpdates: jasmine.createSpy('watchConsentUpdates').and.returnValue(null),
@@ -112,7 +126,7 @@ describe('App component', () => {
112126
{ provide: HeadTagService, useValue: new HeadTagServiceMock() },
113127
{ provide: Angulartics2DSpace, useValue: new AngularticsProviderMock() },
114128
{ provide: AuthService, useValue: new AuthServiceMock() },
115-
{ provide: Router, useValue: new RouterMock() },
129+
{ provide: Router, useValue: routerMock },
116130
{ provide: ActivatedRoute, useValue: new MockActivatedRoute() },
117131
{ provide: MenuService, useValue: menuService },
118132
{ provide: CSSVariableService, useClass: CSSVariableServiceStub },
@@ -173,4 +187,32 @@ describe('App component', () => {
173187
});
174188

175189
});
190+
191+
describe('isRouteLoading$ handling', () => {
192+
193+
it('should not show loading for item page', fakeAsync(() => {
194+
routeServiceMock.getCurrentUrl.and.returnValue(of(itemPageUrl));
195+
routerEventsObs.next(new NavigationStart(1, itemPageUrl));
196+
fixture.detectChanges();
197+
tick();
198+
expect(comp.isRouteLoading$.value).toBeFalse();
199+
}));
200+
201+
it('should show loading for item page administrative edit', fakeAsync(() => {
202+
routeServiceMock.getCurrentUrl.and.returnValue(of(itemPageUrl));
203+
routerEventsObs.next(new NavigationStart(2, itemPageUrl + '/edit'));
204+
fixture.detectChanges();
205+
tick();
206+
expect(comp.isRouteLoading$.value).toBeTrue();
207+
}));
208+
209+
it('should not show loading navigating between item pages in administrative edit', fakeAsync(() => {
210+
routeServiceMock.getCurrentUrl.and.returnValue(of(itemPageUrl + '/edit'));
211+
routerEventsObs.next(new NavigationStart(2, itemPageUrl + '/edit/status'));
212+
fixture.detectChanges();
213+
tick();
214+
expect(comp.isRouteLoading$.value).toBeFalse();
215+
}));
216+
217+
});
176218
});

src/app/app.component.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ export class AppComponent implements OnInit, AfterViewInit {
174174
)),
175175
).subscribe(([currentUrl, event]: [string, any]) => {
176176
if (event instanceof NavigationStart) {
177-
if (!(currentUrl.startsWith('/entities' || getEditItemPageRoute()) || currentUrl.startsWith(getWorkspaceItemModuleRoute()) || currentUrl.startsWith(getWorkflowItemModuleRoute()))) {
177+
const nextUrl = event.url;
178+
if (!this.shouldSkipLoadingStatus(currentUrl, nextUrl)) {
178179
distinctNext(this.isRouteLoading$, true);
179180
}
180181
// distinctNext(this.isRouteLoading$, true);
@@ -217,4 +218,14 @@ export class AppComponent implements OnInit, AfterViewInit {
217218
});
218219
}
219220

221+
private shouldSkipLoadingStatus(currentUrl: string, nextUrl: string): boolean {
222+
return ((currentUrl.startsWith('/entities') || currentUrl.startsWith(getEditItemPageRoute())) && !(this.isAdministrativeEditItemPageRoute(nextUrl, currentUrl)))
223+
|| currentUrl.startsWith(getWorkspaceItemModuleRoute()) || currentUrl.startsWith(getWorkflowItemModuleRoute());
224+
}
225+
226+
private isAdministrativeEditItemPageRoute(nextUrl: string, currentUrl: string): boolean {
227+
const editPageRegEx = /\/(entities\/[^\/]+|items)\/[0-9a-f-]{36}\/edit(?:\/.*)?$/;
228+
return editPageRegEx.test(nextUrl) && !editPageRegEx.test(currentUrl);
229+
}
230+
220231
}

0 commit comments

Comments
 (0)