forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DSC-1337] Improve redirecting code and add unit test
- Loading branch information
Showing
5 changed files
with
202 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { take } from 'rxjs/operators'; | ||
import { ItemDataService } from '../core/data/item-data.service'; | ||
import { Item } from '../core/shared/item.model'; | ||
import { createSuccessfulRemoteDataObject, createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { Router } from '@angular/router'; | ||
import { HardRedirectService } from '../core/services/hard-redirect.service'; | ||
import { CrisItemPageTabResolver } from './cris-item-page-tab.resolver'; | ||
import { TabDataService } from '../core/layout/tab-data.service'; | ||
import { createPaginatedList } from '../shared/testing/utils.test'; | ||
import { tabDetailsTest, tabPublicationsTest } from '../shared/testing/layout-tab.mocks'; | ||
|
||
describe('CrisItemPageTabResolver', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [RouterTestingModule.withRoutes([{ | ||
path: 'entities/:entity-type/:id/:tab', | ||
component: {} as any | ||
}])] | ||
}); | ||
}); | ||
|
||
describe('when item exists', () => { | ||
let resolver: CrisItemPageTabResolver; | ||
const itemService: jasmine.SpyObj<ItemDataService> = jasmine.createSpyObj('ItemDataService', { | ||
'findById': jasmine.createSpy('findById') | ||
}); | ||
const tabService: jasmine.SpyObj<TabDataService> = jasmine.createSpyObj('TabDataService', { | ||
'findByItem': jasmine.createSpy('findByItem') | ||
}); | ||
let hardRedirectService: HardRedirectService; | ||
|
||
let router; | ||
|
||
const uuid = '1234-65487-12354-1235'; | ||
const item = Object.assign(new Item(), { | ||
id: uuid, | ||
uuid: uuid, | ||
metadata: { | ||
'dspace.entity.type': [ | ||
{ | ||
value: 'Publication' | ||
} | ||
] | ||
} | ||
}); | ||
|
||
const tabsRD = createSuccessfulRemoteDataObject(createPaginatedList([tabPublicationsTest, tabDetailsTest])); | ||
const tabsRD$ = createSuccessfulRemoteDataObject$(createPaginatedList([tabPublicationsTest, tabDetailsTest])); | ||
|
||
const noTabsRD = createSuccessfulRemoteDataObject(createPaginatedList([])); | ||
const noTabsRD$ = createSuccessfulRemoteDataObject$(createPaginatedList([])); | ||
|
||
beforeEach(() => { | ||
router = TestBed.inject(Router); | ||
|
||
itemService.findById.and.returnValue(createSuccessfulRemoteDataObject$(item)); | ||
|
||
hardRedirectService = jasmine.createSpyObj('HardRedirectService', { | ||
'redirect': jasmine.createSpy('redirect') | ||
}); | ||
}); | ||
|
||
describe('and there tabs', () => { | ||
beforeEach(() => { | ||
|
||
(tabService as any).findByItem.and.returnValue(tabsRD$); | ||
|
||
spyOn(router, 'navigateByUrl'); | ||
|
||
resolver = new CrisItemPageTabResolver(hardRedirectService, tabService, itemService, router); | ||
}); | ||
|
||
it('should redirect to root route if given tab is the first one', (done) => { | ||
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/publications' } as any) | ||
.pipe(take(1)) | ||
.subscribe( | ||
(resolved) => { | ||
expect(router.navigateByUrl).not.toHaveBeenCalled(); | ||
expect(hardRedirectService.redirect).toHaveBeenCalledWith('/entities/publication/1234-65487-12354-1235', 302); | ||
expect(resolved).toEqual(tabsRD); | ||
done(); | ||
} | ||
); | ||
}); | ||
|
||
it('should not redirect to root route if tab different than the main one is given', (done) => { | ||
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/details' } as any) | ||
.pipe(take(1)) | ||
.subscribe( | ||
(resolved) => { | ||
expect(router.navigateByUrl).not.toHaveBeenCalled(); | ||
expect(hardRedirectService.redirect).not.toHaveBeenCalled(); | ||
expect(resolved).toEqual(tabsRD); | ||
done(); | ||
} | ||
); | ||
}); | ||
|
||
it('should not redirect to root route if no tab is given', (done) => { | ||
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235' } as any) | ||
.pipe(take(1)) | ||
.subscribe( | ||
(resolved) => { | ||
expect(router.navigateByUrl).not.toHaveBeenCalled(); | ||
expect(hardRedirectService.redirect).not.toHaveBeenCalled(); | ||
expect(resolved).toEqual(tabsRD); | ||
done(); | ||
} | ||
); | ||
}); | ||
|
||
it('should navigate to 404 if a wrong tab is given', (done) => { | ||
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235/test' } as any) | ||
.pipe(take(1)) | ||
.subscribe( | ||
(resolved) => { | ||
expect(router.navigateByUrl).toHaveBeenCalled(); | ||
expect(hardRedirectService.redirect).not.toHaveBeenCalled(); | ||
expect(resolved).toEqual(tabsRD); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); | ||
|
||
describe('and there no tabs', () => { | ||
beforeEach(() => { | ||
|
||
(tabService as any).findByItem.and.returnValue(noTabsRD$); | ||
|
||
spyOn(router, 'navigateByUrl'); | ||
|
||
resolver = new CrisItemPageTabResolver(hardRedirectService, tabService, itemService, router); | ||
}); | ||
|
||
it('should not redirect nor navigate', (done) => { | ||
resolver.resolve({ params: { id: uuid } } as any, { url: '/entities/publication/1234-65487-12354-1235' } as any) | ||
.pipe(take(1)) | ||
.subscribe( | ||
(resolved) => { | ||
expect(router.navigateByUrl).not.toHaveBeenCalled(); | ||
expect(hardRedirectService.redirect).not.toHaveBeenCalled(); | ||
expect(resolved).toEqual(noTabsRD); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters