Skip to content

Commit 3eb32e5

Browse files
committed
Treat /home as previous route and add tests
Include "/home" in ItemComponent's previousRoute regex so the back button is shown for home. Add unit tests to verify the back button appears for a home previous URL and that a home previous URL is prioritized over a session-stored URL (ensuring the session is updated and navigation uses the home URL).
1 parent 85dfa4c commit 3eb32e5

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/app/item-page/simple/item-types/shared/item.component.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ describe('ItemComponent', () => {
431431

432432
const searchUrl = '/search?query=test&spc.page=2';
433433
const browseUrl = '/browse/title?scope=0cc&bbm.page=3';
434+
const homeUrl = '/home';
434435
const recentSubmissionsUrl = '/collections/be7b8430-77a5-4016-91c9-90863e50583a?cp.page=3';
435436

436437
beforeEach(waitForAsync(() => {
@@ -517,6 +518,32 @@ describe('ItemComponent', () => {
517518
expect(val).toBeTrue();
518519
});
519520
});
521+
522+
it('should show back button for home', () => {
523+
spyOn(mockRouteService, 'getPreviousUrl').and.returnValue(observableOf(homeUrl));
524+
comp.ngOnInit();
525+
comp.showBackButton.subscribe((val) => {
526+
expect(val).toBeTrue();
527+
});
528+
});
529+
530+
it('should prioritize home previous url over session fallback', () => {
531+
const staleSessionUrl = searchUrl;
532+
const getPreviousUrlSpy = spyOn(mockRouteService, 'getPreviousUrl').and.returnValue(observableOf(homeUrl));
533+
const getUrlFromSessionSpy = spyOn(mockRouteService, 'getUrlFromSession').and.returnValue(staleSessionUrl);
534+
const storeUrlInSessionSpy = spyOn(mockRouteService, 'storeUrlInSession');
535+
536+
comp.ngOnInit();
537+
comp.showBackButton.subscribe((val) => {
538+
expect(val).toBeTrue();
539+
expect(getPreviousUrlSpy).toHaveBeenCalled();
540+
expect(getUrlFromSessionSpy).not.toHaveBeenCalled();
541+
expect(storeUrlInSessionSpy).toHaveBeenCalledWith('item-previous-url', homeUrl);
542+
543+
comp.back();
544+
expect(router.navigateByUrl).toHaveBeenCalledWith(homeUrl);
545+
});
546+
});
520547
});
521548

522549
});

src/app/item-page/simple/item-types/shared/item.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class ItemComponent implements OnInit {
3131
* This regex matches previous routes. The button is shown
3232
* for matching paths and hidden in other cases.
3333
*/
34-
previousRoute = /^(\/search|\/browse|\/collections|\/admin\/search|\/mydspace)/;
34+
previousRoute = /^(\/home|\/search|\/browse|\/collections|\/admin\/search|\/mydspace)/;
3535

3636
/**
3737
* Used to show or hide the back to results button in the view.

0 commit comments

Comments
 (0)