Commits
Tim Donohue authored and GitHub committed db526a0c258 Merge
3 3 | import { followLink } from '../../shared/utils/follow-link-config.model'; |
4 4 | import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service'; |
5 5 | import { ObjectCacheService } from '../cache/object-cache.service'; |
6 6 | import { HALEndpointService } from '../shared/hal-endpoint.service'; |
7 7 | import { DsoRedirectService } from './dso-redirect.service'; |
8 8 | import { GetRequest, IdentifierType } from './request.models'; |
9 9 | import { RequestService } from './request.service'; |
10 10 | import { createSuccessfulRemoteDataObject } from '../../shared/remote-data.utils'; |
11 11 | import { Item } from '../shared/item.model'; |
12 12 | import { EMBED_SEPARATOR } from './base/base-data.service'; |
13 + | import { HardRedirectService } from '../services/hard-redirect.service'; |
13 14 | |
14 15 | describe('DsoRedirectService', () => { |
15 16 | let scheduler: TestScheduler; |
16 17 | let service: DsoRedirectService; |
17 18 | let halService: HALEndpointService; |
18 19 | let requestService: RequestService; |
19 20 | let rdbService: RemoteDataBuildService; |
20 - | let router; |
21 + | let redirectService: HardRedirectService; |
21 22 | let remoteData; |
22 23 | const dsoUUID = '9b4f22f4-164a-49db-8817-3316b6ee5746'; |
23 24 | const dsoHandle = '1234567789/22'; |
24 25 | const encodedHandle = encodeURIComponent(dsoHandle); |
25 26 | const pidLink = 'https://rest.api/rest/api/pid/find{?id}'; |
26 27 | const requestHandleURL = `https://rest.api/rest/api/pid/find?id=${encodedHandle}`; |
27 28 | const requestUUIDURL = `https://rest.api/rest/api/pid/find?id=${dsoUUID}`; |
28 29 | const requestUUID = '34cfed7c-f597-49ef-9cbe-ea351f0023c2'; |
29 30 | const objectCache = {} as ObjectCacheService; |
30 31 | |
31 32 | beforeEach(() => { |
32 33 | scheduler = getTestScheduler(); |
33 34 | |
34 35 | halService = jasmine.createSpyObj('halService', { |
35 36 | getEndpoint: cold('a', { a: pidLink }) |
36 37 | }); |
37 38 | requestService = jasmine.createSpyObj('requestService', { |
38 39 | generateRequestId: requestUUID, |
39 40 | send: true |
40 41 | }); |
41 - | router = { |
42 - | navigate: jasmine.createSpy('navigate') |
43 - | }; |
44 42 | |
45 43 | remoteData = createSuccessfulRemoteDataObject(Object.assign(new Item(), { |
46 44 | type: 'item', |
47 45 | uuid: '123456789' |
48 46 | })); |
49 47 | |
50 48 | rdbService = jasmine.createSpyObj('rdbService', { |
51 49 | buildSingle: cold('a', { |
52 50 | a: remoteData |
53 51 | }) |
54 52 | }); |
53 + | |
54 + | redirectService = jasmine.createSpyObj('redirectService', { |
55 + | redirect: {} |
56 + | }); |
57 + | |
55 58 | service = new DsoRedirectService( |
56 59 | requestService, |
57 60 | rdbService, |
58 61 | objectCache, |
59 62 | halService, |
60 - | router, |
63 + | redirectService |
61 64 | ); |
62 65 | }); |
63 66 | |
64 67 | describe('findByIdAndIDType', () => { |
65 68 | it('should call HALEndpointService with the path to the pid endpoint', () => { |
66 69 | scheduler.schedule(() => service.findByIdAndIDType(dsoHandle, IdentifierType.HANDLE)); |
67 70 | scheduler.flush(); |
68 71 | |
69 72 | expect(halService.getEndpoint).toHaveBeenCalledWith('pid'); |
70 73 | }); |
97 100 | expect(requestService.send).toHaveBeenCalledWith(new GetRequest(requestUUID, requestHandleURL), true); |
98 101 | }); |
99 102 | |
100 103 | it('should navigate to item route', () => { |
101 104 | remoteData.payload.type = 'item'; |
102 105 | const redir = service.findByIdAndIDType(dsoHandle, IdentifierType.HANDLE); |
103 106 | // The framework would normally subscribe but do it here so we can test navigation. |
104 107 | redir.subscribe(); |
105 108 | scheduler.schedule(() => redir); |
106 109 | scheduler.flush(); |
107 - | expect(router.navigate).toHaveBeenCalledWith(['/items/' + remoteData.payload.uuid]); |
110 + | expect(redirectService.redirect).toHaveBeenCalledWith('/items/' + remoteData.payload.uuid, 301); |
108 111 | }); |
109 112 | it('should navigate to entities route with the corresponding entity type', () => { |
110 113 | remoteData.payload.type = 'item'; |
111 114 | remoteData.payload.metadata = { |
112 115 | 'dspace.entity.type': [ |
113 116 | { |
114 117 | language: 'en_US', |
115 118 | value: 'Publication' |
116 119 | } |
117 120 | ], |
118 121 | }; |
119 122 | const redir = service.findByIdAndIDType(dsoHandle, IdentifierType.HANDLE); |
120 123 | // The framework would normally subscribe but do it here so we can test navigation. |
121 124 | redir.subscribe(); |
122 125 | scheduler.schedule(() => redir); |
123 126 | scheduler.flush(); |
124 - | expect(router.navigate).toHaveBeenCalledWith(['/entities/publication/' + remoteData.payload.uuid]); |
127 + | expect(redirectService.redirect).toHaveBeenCalledWith('/entities/publication/' + remoteData.payload.uuid, 301); |
125 128 | }); |
126 129 | |
127 130 | it('should navigate to collections route', () => { |
128 131 | remoteData.payload.type = 'collection'; |
129 132 | const redir = service.findByIdAndIDType(dsoHandle, IdentifierType.HANDLE); |
130 133 | redir.subscribe(); |
131 134 | scheduler.schedule(() => redir); |
132 135 | scheduler.flush(); |
133 - | expect(router.navigate).toHaveBeenCalledWith(['/collections/' + remoteData.payload.uuid]); |
136 + | expect(redirectService.redirect).toHaveBeenCalledWith('/collections/' + remoteData.payload.uuid, 301); |
134 137 | }); |
135 138 | |
136 139 | it('should navigate to communities route', () => { |
137 140 | remoteData.payload.type = 'community'; |
138 141 | const redir = service.findByIdAndIDType(dsoHandle, IdentifierType.HANDLE); |
139 142 | redir.subscribe(); |
140 143 | scheduler.schedule(() => redir); |
141 144 | scheduler.flush(); |
142 - | expect(router.navigate).toHaveBeenCalledWith(['/communities/' + remoteData.payload.uuid]); |
145 + | expect(redirectService.redirect).toHaveBeenCalledWith('/communities/' + remoteData.payload.uuid, 301); |
143 146 | }); |
144 147 | }); |
145 148 | |
146 149 | describe('DataService', () => { // todo: should only test the id/uuid interpolation thingy |
147 150 | describe('getIDHref', () => { // todo: should be able to move this up to IdentifiableDataService? |
148 151 | it('should return endpoint', () => { |
149 152 | const result = (service as any).dataService.getIDHref(pidLink, dsoUUID); |
150 153 | expect(result).toEqual(requestUUIDURL); |
151 154 | }); |
152 155 | |