Skip to content

Commit 1b958a3

Browse files
author
FrancescoMauto
committed
[DSC-2887] fix: ok load page with query params, the resolver mis-validate the tab
1 parent b14967f commit 1b958a3

2 files changed

Lines changed: 52 additions & 19 deletions

File tree

src/app/item-page/cris-item-page-tab.resolver.spec.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,50 @@ describe('CrisItemPageTabResolver', () => {
136136
});
137137
});
138138

139-
it('Should handle tab shortnames with "::" correctly', () => {
140-
const tabRD = createSuccessfulRemoteDataObject(createPaginatedList([{
139+
it('Should handle tab shortnames with "::" correctly', (done) => {
140+
const tabPageList = createPaginatedList([{
141141
...tabPublicationsTest,
142142
shortname: 'publication::details',
143-
}]));
144-
tabService.findByItem.and.returnValue(createSuccessfulRemoteDataObject$(tabRD) as any);
143+
}]);
144+
const tabRD = createSuccessfulRemoteDataObject(tabPageList);
145+
tabService.findByItem.and.returnValue(createSuccessfulRemoteDataObject$(tabPageList) as any);
145146

146-
TestBed.runInInjectionContext(() => {
147-
return crisItemPageTabResolver({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/publication::details' } as any);
147+
const obs = TestBed.runInInjectionContext(() => {
148+
return crisItemPageTabResolver({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/details' } as any);
149+
}) as Observable<RemoteData<PaginatedList<CrisLayoutTab>>>;
150+
151+
obs.pipe(take(1)).subscribe((resolved) => {
152+
expect(router.navigateByUrl).not.toHaveBeenCalled();
153+
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
154+
expect(resolved).toEqual(tabRD);
155+
done();
148156
});
157+
});
158+
159+
it('should NOT redirect to 404 when query params are present in the URL', (done) => {
160+
const obs = TestBed.runInInjectionContext(() => {
161+
return crisItemPageTabResolver({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235?f.subject=value&f.date=2024' } as any);
162+
}) as Observable<RemoteData<PaginatedList<CrisLayoutTab>>>;
149163

150-
expect(router.navigateByUrl).not.toHaveBeenCalled();
151-
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
164+
obs.pipe(take(1)).subscribe((resolved) => {
165+
expect(router.navigateByUrl).not.toHaveBeenCalled();
166+
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
167+
expect(resolved).toEqual(tabsRD);
168+
done();
169+
});
170+
});
171+
172+
it('should NOT redirect to 404 when query params are present with a valid tab', (done) => {
173+
const obs = TestBed.runInInjectionContext(() => {
174+
return crisItemPageTabResolver({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/details?f.subject=value' } as any);
175+
}) as Observable<RemoteData<PaginatedList<CrisLayoutTab>>>;
176+
177+
obs.pipe(take(1)).subscribe((resolved) => {
178+
expect(router.navigateByUrl).not.toHaveBeenCalled();
179+
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
180+
expect(resolved).toEqual(tabsRD);
181+
done();
182+
});
152183
});
153184
});
154185

src/app/item-page/cris-item-page-tab.resolver.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,19 @@ export const crisItemPageTabResolver: ResolveFn<RemoteData<PaginatedList<CrisLay
5555
).pipe(
5656
getFirstCompletedRemoteData(),
5757
map((tabsRD: RemoteData<PaginatedList<CrisLayoutTab>>) => {
58-
if (tabsRD.hasSucceeded && tabsRD?.payload?.page?.length > 0) {
59-
// By splitting the url with uuid we can understand if the item is primary item page or a tab
60-
const urlSplit = state.url.split(route.params.id);
61-
const givenTab = urlSplit[1];
62-
const itemPageRoute = getItemPageRoute(itemRD.payload);
58+
if (tabsRD.hasSucceeded && tabsRD?.payload?.page?.length > 0) {
59+
// By splitting the url with uuid we can understand if the item is primary item page or a tab
60+
const urlWithoutQuery = state.url.split('?')[0];
61+
const urlSplit = urlWithoutQuery.split(route.params.id);
62+
const tabArguments = urlSplit[1]?.split('/');
63+
const givenTab = tabArguments?.[1];
64+
const itemPageRoute = getItemPageRoute(itemRD.payload);
6365

64-
const isValidTab = !givenTab || tabsRD.payload.page.some((tab) => {
65-
const shortnameSplit = tab.shortname.split('::');
66-
const shortname = shortnameSplit[shortnameSplit.length - 1];
67-
return `/${shortname}` === givenTab;
68-
});
66+
const isValidTab = !givenTab || tabsRD.payload.page.some((tab) => {
67+
const shortnameSplit = tab.shortname.split('::');
68+
const shortname = shortnameSplit[shortnameSplit.length - 1];
69+
return shortname === givenTab;
70+
});
6971

7072
const mainTab = tabsRD.payload.page.length === 1
7173
? tabsRD.payload.page[0]
@@ -74,7 +76,7 @@ export const crisItemPageTabResolver: ResolveFn<RemoteData<PaginatedList<CrisLay
7476
if (!isValidTab) {
7577
// If wrong tab is given redirect to 404 page
7678
router.navigateByUrl(getPageNotFoundRoute(), { skipLocationChange: true, replaceUrl: false });
77-
} else if (givenTab === `/${mainTab.shortname}`) {
79+
} else if (givenTab === mainTab.shortname) {
7880
if (isPlatformServer(platformId)) {
7981
// If first tab is given redirect to root item page
8082
hardRedirectService.redirect(itemPageRoute, 302);

0 commit comments

Comments
 (0)