Skip to content

Commit 1cf696c

Browse files
committed
[UXP-124] Fix redirect on CSR when the given tab is the main one
1 parent fe577c1 commit 1cf696c

2 files changed

Lines changed: 136 additions & 60 deletions

File tree

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

Lines changed: 126 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { CrisItemPageTabResolver } from './cris-item-page-tab.resolver';
1010
import { TabDataService } from '../core/layout/tab-data.service';
1111
import { createPaginatedList } from '../shared/testing/utils.test';
1212
import { tabDetailsTest, tabPublicationsTest } from '../shared/testing/layout-tab.mocks';
13+
import { PLATFORM_ID } from '@angular/core';
1314

1415
describe('CrisItemPageTabResolver', () => {
1516
beforeEach(() => {
@@ -32,6 +33,7 @@ describe('CrisItemPageTabResolver', () => {
3233
let hardRedirectService: HardRedirectService;
3334

3435
let router;
36+
let platformId;
3537

3638
const uuid = '1234-65487-12354-1235';
3739
const item = Object.assign(new Item(), {
@@ -54,6 +56,7 @@ describe('CrisItemPageTabResolver', () => {
5456

5557
beforeEach(() => {
5658
router = TestBed.inject(Router);
59+
platformId = TestBed.inject(PLATFORM_ID);
5760

5861
itemService.findById.and.returnValue(createSuccessfulRemoteDataObject$(item));
5962

@@ -63,65 +66,131 @@ describe('CrisItemPageTabResolver', () => {
6366
});
6467

6568
describe('and there tabs', () => {
66-
beforeEach(() => {
67-
68-
(tabService as any).findByItem.and.returnValue(tabsRD$);
69-
70-
spyOn(router, 'navigateByUrl');
7169

72-
resolver = new CrisItemPageTabResolver(hardRedirectService, tabService, itemService, router);
70+
describe('when platform is browser', () => {
71+
beforeEach(() => {
72+
73+
(tabService as any).findByItem.and.returnValue(tabsRD$);
74+
75+
spyOn(router, 'navigateByUrl');
76+
77+
resolver = new CrisItemPageTabResolver(platformId, hardRedirectService, tabService, itemService, router);
78+
});
79+
80+
it('should redirect to root route if given tab is the first one', (done) => {
81+
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/publications' } as any)
82+
.pipe(take(1))
83+
.subscribe(
84+
(resolved) => {
85+
expect(router.navigateByUrl).toHaveBeenCalledWith('/entities/publication/1234-65487-12354-1235');
86+
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
87+
expect(resolved).toEqual(tabsRD);
88+
done();
89+
}
90+
);
91+
});
92+
93+
it('should not redirect to root route if tab different than the main one is given', (done) => {
94+
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/details' } as any)
95+
.pipe(take(1))
96+
.subscribe(
97+
(resolved) => {
98+
expect(router.navigateByUrl).not.toHaveBeenCalled();
99+
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
100+
expect(resolved).toEqual(tabsRD);
101+
done();
102+
}
103+
);
104+
});
105+
106+
it('should not redirect to root route if no tab is given', (done) => {
107+
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235' } as any)
108+
.pipe(take(1))
109+
.subscribe(
110+
(resolved) => {
111+
expect(router.navigateByUrl).not.toHaveBeenCalled();
112+
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
113+
expect(resolved).toEqual(tabsRD);
114+
done();
115+
}
116+
);
117+
});
118+
119+
it('should navigate to 404 if a wrong tab is given', (done) => {
120+
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/test' } as any)
121+
.pipe(take(1))
122+
.subscribe(
123+
(resolved) => {
124+
expect(router.navigateByUrl).toHaveBeenCalled();
125+
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
126+
expect(resolved).toEqual(tabsRD);
127+
done();
128+
}
129+
);
130+
});
73131
});
74132

75-
it('should redirect to root route if given tab is the first one', (done) => {
76-
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/publications' } as any)
77-
.pipe(take(1))
78-
.subscribe(
79-
(resolved) => {
80-
expect(router.navigateByUrl).not.toHaveBeenCalled();
81-
expect(hardRedirectService.redirect).toHaveBeenCalledWith('/entities/publication/1234-65487-12354-1235', 302);
82-
expect(resolved).toEqual(tabsRD);
83-
done();
84-
}
85-
);
86-
});
87-
88-
it('should not redirect to root route if tab different than the main one is given', (done) => {
89-
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/details' } as any)
90-
.pipe(take(1))
91-
.subscribe(
92-
(resolved) => {
93-
expect(router.navigateByUrl).not.toHaveBeenCalled();
94-
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
95-
expect(resolved).toEqual(tabsRD);
96-
done();
97-
}
98-
);
99-
});
100-
101-
it('should not redirect to root route if no tab is given', (done) => {
102-
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235' } as any)
103-
.pipe(take(1))
104-
.subscribe(
105-
(resolved) => {
106-
expect(router.navigateByUrl).not.toHaveBeenCalled();
107-
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
108-
expect(resolved).toEqual(tabsRD);
109-
done();
110-
}
111-
);
112-
});
113-
114-
it('should navigate to 404 if a wrong tab is given', (done) => {
115-
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/test' } as any)
116-
.pipe(take(1))
117-
.subscribe(
118-
(resolved) => {
119-
expect(router.navigateByUrl).toHaveBeenCalled();
120-
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
121-
expect(resolved).toEqual(tabsRD);
122-
done();
123-
}
124-
);
133+
describe('when platform is server', () => {
134+
beforeEach(() => {
135+
platformId = 'server';
136+
(tabService as any).findByItem.and.returnValue(tabsRD$);
137+
138+
spyOn(router, 'navigateByUrl');
139+
140+
resolver = new CrisItemPageTabResolver(platformId, hardRedirectService, tabService, itemService, router);
141+
});
142+
143+
it('should redirect to root route if given tab is the first one', (done) => {
144+
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/publications' } as any)
145+
.pipe(take(1))
146+
.subscribe(
147+
(resolved) => {
148+
expect(router.navigateByUrl).not.toHaveBeenCalled();
149+
expect(hardRedirectService.redirect).toHaveBeenCalledWith('/entities/publication/1234-65487-12354-1235', 302);
150+
expect(resolved).toEqual(tabsRD);
151+
done();
152+
}
153+
);
154+
});
155+
156+
it('should not redirect to root route if tab different than the main one is given', (done) => {
157+
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/details' } as any)
158+
.pipe(take(1))
159+
.subscribe(
160+
(resolved) => {
161+
expect(router.navigateByUrl).not.toHaveBeenCalled();
162+
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
163+
expect(resolved).toEqual(tabsRD);
164+
done();
165+
}
166+
);
167+
});
168+
169+
it('should not redirect to root route if no tab is given', (done) => {
170+
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235' } as any)
171+
.pipe(take(1))
172+
.subscribe(
173+
(resolved) => {
174+
expect(router.navigateByUrl).not.toHaveBeenCalled();
175+
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
176+
expect(resolved).toEqual(tabsRD);
177+
done();
178+
}
179+
);
180+
});
181+
182+
it('should navigate to 404 if a wrong tab is given', (done) => {
183+
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/test' } as any)
184+
.pipe(take(1))
185+
.subscribe(
186+
(resolved) => {
187+
expect(router.navigateByUrl).toHaveBeenCalled();
188+
expect(hardRedirectService.redirect).not.toHaveBeenCalled();
189+
expect(resolved).toEqual(tabsRD);
190+
done();
191+
}
192+
);
193+
});
125194
});
126195
});
127196

@@ -132,7 +201,7 @@ describe('CrisItemPageTabResolver', () => {
132201

133202
spyOn(router, 'navigateByUrl');
134203

135-
resolver = new CrisItemPageTabResolver(hardRedirectService, tabService, itemService, router);
204+
resolver = new CrisItemPageTabResolver(platformId, hardRedirectService, tabService, itemService, router);
136205
});
137206

138207
it('should not redirect nor navigate', (done) => {

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Injectable} from '@angular/core';
1+
import { Inject, Injectable, PLATFORM_ID } from '@angular/core';
22
import { ActivatedRouteSnapshot, Resolve, Router, RouterStateSnapshot } from '@angular/router';
33

44
import { Observable } from 'rxjs';
@@ -15,6 +15,7 @@ import { getItemPageRoute } from './item-page-routing-paths';
1515
import { createFailedRemoteDataObject$ } from '../shared/remote-data.utils';
1616
import { HardRedirectService } from '../core/services/hard-redirect.service';
1717
import { getPageNotFoundRoute } from '../app-routing-paths';
18+
import { isPlatformServer } from '@angular/common';
1819

1920
/**
2021
* This class represents a resolver that requests the tabs of specific
@@ -24,6 +25,7 @@ import { getPageNotFoundRoute } from '../app-routing-paths';
2425
export class CrisItemPageTabResolver implements Resolve<RemoteData<PaginatedList<CrisLayoutTab>>> {
2526

2627
constructor(
28+
@Inject(PLATFORM_ID) protected platformId: any,
2729
private hardRedirectService: HardRedirectService,
2830
private tabService: TabDataService,
2931
private itemDataService: ItemDataService,
@@ -63,8 +65,13 @@ export class CrisItemPageTabResolver implements Resolve<RemoteData<PaginatedList
6365
// If wrong tab is given redirect to 404 page
6466
this.router.navigateByUrl(getPageNotFoundRoute(), { skipLocationChange: true, replaceUrl: false });
6567
} else if (givenTab === `/${mainTab.shortname}`) {
66-
// If first tab is given redirect to root item page
67-
this.hardRedirectService.redirect(itemPageRoute, 302);
68+
if (isPlatformServer(this.platformId)) {
69+
// If first tab is given redirect to root item page
70+
this.hardRedirectService.redirect(itemPageRoute, 302);
71+
} else {
72+
this.router.navigateByUrl(itemPageRoute);
73+
}
74+
6875
}
6976
}
7077
return tabsRD;

0 commit comments

Comments
 (0)