Commits

Tim Donohue authored d4a5308d0c7
Refactor to use HardRedirectService.

Update its redirect method to support optional statusCode
No tags

src/app/core/data/dso-redirect.service.spec.ts

Modified
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 - const mockResponse = jasmine.createSpyObj(['redirect']);
31 - const mockPlatformBrowser = 'browser';
32 - const mockPlatformServer = 'server';
33 31
34 32 beforeEach(() => {
35 33 scheduler = getTestScheduler();
36 34
37 35 halService = jasmine.createSpyObj('halService', {
38 36 getEndpoint: cold('a', { a: pidLink })
39 37 });
40 38 requestService = jasmine.createSpyObj('requestService', {
41 39 generateRequestId: requestUUID,
42 40 send: true
43 41 });
44 - router = {
45 - navigate: jasmine.createSpy('navigate')
46 - };
47 42
48 43 remoteData = createSuccessfulRemoteDataObject(Object.assign(new Item(), {
49 44 type: 'item',
50 45 uuid: '123456789'
51 46 }));
52 47
53 48 rdbService = jasmine.createSpyObj('rdbService', {
54 49 buildSingle: cold('a', {
55 50 a: remoteData
56 51 })
57 52 });
53 +
54 + redirectService = jasmine.createSpyObj('redirectService', {
55 + redirect: {}
56 + });
57 +
58 58 service = new DsoRedirectService(
59 59 requestService,
60 60 rdbService,
61 61 objectCache,
62 62 halService,
63 - router,
64 - mockResponse,
65 - mockPlatformBrowser // default to CSR except where explicitly SSR below
63 + redirectService
66 64 );
67 65 });
68 66
69 67 describe('findByIdAndIDType', () => {
70 68 it('should call HALEndpointService with the path to the pid endpoint', () => {
71 69 scheduler.schedule(() => service.findByIdAndIDType(dsoHandle, IdentifierType.HANDLE));
72 70 scheduler.flush();
73 71
74 72 expect(halService.getEndpoint).toHaveBeenCalledWith('pid');
75 73 });
102 100 expect(requestService.send).toHaveBeenCalledWith(new GetRequest(requestUUID, requestHandleURL), true);
103 101 });
104 102
105 103 it('should navigate to item route', () => {
106 104 remoteData.payload.type = 'item';
107 105 const redir = service.findByIdAndIDType(dsoHandle, IdentifierType.HANDLE);
108 106 // The framework would normally subscribe but do it here so we can test navigation.
109 107 redir.subscribe();
110 108 scheduler.schedule(() => redir);
111 109 scheduler.flush();
112 - expect(router.navigate).toHaveBeenCalledWith(['/items/' + remoteData.payload.uuid]);
110 + expect(redirectService.redirect).toHaveBeenCalledWith('/items/' + remoteData.payload.uuid, 301);
113 111 });
114 112 it('should navigate to entities route with the corresponding entity type', () => {
115 113 remoteData.payload.type = 'item';
116 114 remoteData.payload.metadata = {
117 115 'dspace.entity.type': [
118 116 {
119 117 language: 'en_US',
120 118 value: 'Publication'
121 119 }
122 120 ],
123 121 };
124 122 const redir = service.findByIdAndIDType(dsoHandle, IdentifierType.HANDLE);
125 123 // The framework would normally subscribe but do it here so we can test navigation.
126 124 redir.subscribe();
127 125 scheduler.schedule(() => redir);
128 126 scheduler.flush();
129 - expect(router.navigate).toHaveBeenCalledWith(['/entities/publication/' + remoteData.payload.uuid]);
127 + expect(redirectService.redirect).toHaveBeenCalledWith('/entities/publication/' + remoteData.payload.uuid, 301);
130 128 });
131 129
132 130 it('should navigate to collections route', () => {
133 131 remoteData.payload.type = 'collection';
134 132 const redir = service.findByIdAndIDType(dsoHandle, IdentifierType.HANDLE);
135 133 redir.subscribe();
136 134 scheduler.schedule(() => redir);
137 135 scheduler.flush();
138 - expect(router.navigate).toHaveBeenCalledWith(['/collections/' + remoteData.payload.uuid]);
136 + expect(redirectService.redirect).toHaveBeenCalledWith('/collections/' + remoteData.payload.uuid, 301);
139 137 });
140 138
141 139 it('should navigate to communities route', () => {
142 140 remoteData.payload.type = 'community';
143 141 const redir = service.findByIdAndIDType(dsoHandle, IdentifierType.HANDLE);
144 142 redir.subscribe();
145 143 scheduler.schedule(() => redir);
146 144 scheduler.flush();
147 - expect(router.navigate).toHaveBeenCalledWith(['/communities/' + remoteData.payload.uuid]);
148 - });
149 -
150 - it('should return 301 redirect when SSR is used', () => {
151 - service = new DsoRedirectService(
152 - requestService,
153 - rdbService,
154 - objectCache,
155 - halService,
156 - router,
157 - mockResponse,
158 - mockPlatformServer // explicitly SSR mode
159 - );
160 - remoteData.payload.type = 'item';
161 - const redir = service.findByIdAndIDType(dsoHandle, IdentifierType.HANDLE);
162 - // The framework would normally subscribe but do it here so we can test navigation.
163 - redir.subscribe();
164 - scheduler.schedule(() => redir);
165 - scheduler.flush();
166 - expect(mockResponse.redirect).toHaveBeenCalledWith(301, '/items/' + remoteData.payload.uuid);
145 + expect(redirectService.redirect).toHaveBeenCalledWith('/communities/' + remoteData.payload.uuid, 301);
167 146 });
168 147 });
169 148
170 149 describe('DataService', () => { // todo: should only test the id/uuid interpolation thingy
171 150 describe('getIDHref', () => { // todo: should be able to move this up to IdentifiableDataService?
172 151 it('should return endpoint', () => {
173 152 const result = (service as any).dataService.getIDHref(pidLink, dsoUUID);
174 153 expect(result).toEqual(requestUUIDURL);
175 154 });
176 155

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut