Commits
Yury Bondarenko authored c32e4ad7c79
9 9 | import { RemoteDataBuildService } from '../../cache/builders/remote-data-build.service'; |
10 10 | import { ObjectCacheService } from '../../cache/object-cache.service'; |
11 11 | import { HALEndpointService } from '../../shared/hal-endpoint.service'; |
12 12 | import { FindListOptions } from '../find-list-options.model'; |
13 13 | import { Observable, of as observableOf } from 'rxjs'; |
14 14 | import { CreateDataImpl } from './create-data'; |
15 15 | import { NotificationsService } from '../../../shared/notifications/notifications.service'; |
16 16 | import { getMockRequestService } from '../../../shared/mocks/request.service.mock'; |
17 17 | import { HALEndpointServiceStub } from '../../../shared/testing/hal-endpoint-service.stub'; |
18 18 | import { getMockRemoteDataBuildService } from '../../../shared/mocks/remote-data-build.service.mock'; |
19 - | import { followLink } from '../../../shared/utils/follow-link-config.model'; |
20 - | import { TestScheduler } from 'rxjs/testing'; |
21 19 | import { RemoteData } from '../remote-data'; |
22 20 | import { RequestEntryState } from '../request-entry-state.model'; |
21 + | import { createFailedRemoteDataObject, createSuccessfulRemoteDataObject } from '../../../shared/remote-data.utils'; |
22 + | import { RequestParam } from '../../cache/models/request-param.model'; |
23 + | import { RestRequestMethod } from '../rest-request-method'; |
23 24 | |
24 25 | const endpoint = 'https://rest.api/core'; |
25 26 | |
26 27 | class TestService extends CreateDataImpl<any> { |
27 28 | constructor( |
28 29 | protected requestService: RequestService, |
29 30 | protected rdbService: RemoteDataBuildService, |
30 31 | protected objectCache: ObjectCacheService, |
31 32 | protected halService: HALEndpointService, |
32 33 | protected notificationsService: NotificationsService, |
33 34 | ) { |
34 - | super(undefined, requestService, rdbService, objectCache, halService, notificationsService, undefined); |
35 + | super('test', requestService, rdbService, objectCache, halService, notificationsService, undefined); |
35 36 | } |
36 37 | |
37 - | public getBrowseEndpoint(options: FindListOptions = {}, linkPath: string = this.linkPath): Observable<string> { |
38 + | public getEndpoint(options: FindListOptions = {}, linkPath: string = this.linkPath): Observable<string> { |
38 39 | return observableOf(endpoint); |
39 40 | } |
40 41 | } |
41 42 | |
42 43 | describe('CreateDataImpl', () => { |
43 44 | let service: TestService; |
44 45 | let requestService; |
45 46 | let halService; |
46 47 | let rdbService; |
47 48 | let objectCache; |
48 49 | let notificationsService; |
49 - | let selfLink; |
50 - | let linksToFollow; |
51 - | let testScheduler; |
52 50 | let remoteDataMocks; |
51 + | let obj; |
52 + | |
53 + | let MOCK_SUCCEEDED_RD; |
54 + | let MOCK_FAILED_RD; |
55 + | |
56 + | let buildFromRequestUUIDSpy: jasmine.Spy; |
57 + | let createOnEndpointSpy: jasmine.Spy; |
53 58 | |
54 59 | function initTestService(): TestService { |
55 60 | requestService = getMockRequestService(); |
56 61 | halService = new HALEndpointServiceStub('url') as any; |
57 62 | rdbService = getMockRemoteDataBuildService(); |
58 63 | objectCache = { |
59 64 | |
60 65 | addPatch: () => { |
61 66 | /* empty */ |
62 67 | }, |
63 68 | getObjectBySelfLink: () => { |
64 69 | /* empty */ |
65 70 | }, |
66 71 | getByHref: () => { |
67 72 | /* empty */ |
68 73 | }, |
69 74 | } as any; |
70 - | notificationsService = {} as NotificationsService; |
71 - | selfLink = 'https://rest.api/endpoint/1698f1d3-be98-4c51-9fd8-6bfedcbd59b7'; |
72 - | linksToFollow = [ |
73 - | followLink('a'), |
74 - | followLink('b'), |
75 - | ]; |
76 - | |
77 - | testScheduler = new TestScheduler((actual, expected) => { |
78 - | // asserting the two objects are equal |
79 - | // e.g. using chai. |
80 - | expect(actual).toEqual(expected); |
75 + | notificationsService = jasmine.createSpyObj('notificationsService', { |
76 + | error: undefined, |
81 77 | }); |
82 78 | |
79 + | obj = { |
80 + | uuid: '1698f1d3-be98-4c51-9fd8-6bfedcbd59b7', |
81 + | }; |
82 + | |
83 83 | const timeStamp = new Date().getTime(); |
84 84 | const msToLive = 15 * 60 * 1000; |
85 85 | const payload = { foo: 'bar' }; |
86 86 | const statusCodeSuccess = 200; |
87 87 | const statusCodeError = 404; |
88 88 | const errorMessage = 'not found'; |
89 89 | remoteDataMocks = { |
90 90 | RequestPending: new RemoteData(undefined, msToLive, timeStamp, RequestEntryState.RequestPending, undefined, undefined, undefined), |
91 91 | ResponsePending: new RemoteData(undefined, msToLive, timeStamp, RequestEntryState.ResponsePending, undefined, undefined, undefined), |
92 92 | Success: new RemoteData(timeStamp, msToLive, timeStamp, RequestEntryState.Success, undefined, payload, statusCodeSuccess), |
99 99 | requestService, |
100 100 | rdbService, |
101 101 | objectCache, |
102 102 | halService, |
103 103 | notificationsService, |
104 104 | ); |
105 105 | } |
106 106 | |
107 107 | beforeEach(() => { |
108 108 | service = initTestService(); |
109 + | |
110 + | buildFromRequestUUIDSpy = spyOn(rdbService, 'buildFromRequestUUID').and.callThrough(); |
111 + | createOnEndpointSpy = spyOn(service, 'createOnEndpoint').and.callThrough(); |
112 + | |
113 + | MOCK_SUCCEEDED_RD = createSuccessfulRemoteDataObject({}); |
114 + | MOCK_FAILED_RD = createFailedRemoteDataObject('something went wrong'); |
109 115 | }); |
110 116 | |
111 - | // todo: add specs (there were no ceate specs in original DataService suite!) |
117 + | describe('create', () => { |
118 + | it('should POST the object to the root endpoint with the given parameters and return the remote data', (done) => { |
119 + | const params = [ |
120 + | new RequestParam('abc', 123), new RequestParam('def', 456) |
121 + | ]; |
122 + | buildFromRequestUUIDSpy.and.returnValue(observableOf(remoteDataMocks.Success)); |
123 + | |
124 + | service.create(obj, params).subscribe(out => { |
125 + | expect(createOnEndpointSpy).toHaveBeenCalledWith(obj, jasmine.anything()); |
126 + | expect(requestService.send).toHaveBeenCalledWith(jasmine.objectContaining({ |
127 + | method: RestRequestMethod.POST, |
128 + | uuid: requestService.generateRequestId(), |
129 + | href: 'https://rest.api/core?abc=123&def=456', |
130 + | body: JSON.stringify(obj), |
131 + | })); |
132 + | expect(buildFromRequestUUIDSpy).toHaveBeenCalledWith(requestService.generateRequestId()); |
133 + | expect(out).toEqual(remoteDataMocks.Success); |
134 + | done(); |
135 + | }); |
136 + | }); |
137 + | }); |
138 + | |
139 + | describe('createOnEndpoint', () => { |
140 + | beforeEach(() => { |
141 + | buildFromRequestUUIDSpy.and.returnValue(observableOf(remoteDataMocks.Success)); |
142 + | }); |
143 + | |
144 + | it('should send a POST request with the object as JSON', (done) => { |
145 + | service.createOnEndpoint(obj, observableOf('https://rest.api/core/custom?search')).subscribe(out => { |
146 + | expect(requestService.send).toHaveBeenCalledWith(jasmine.objectContaining({ |
147 + | method: RestRequestMethod.POST, |
148 + | body: JSON.stringify(obj), |
149 + | })); |
150 + | done(); |
151 + | }); |
152 + | }); |
153 + | |
154 + | it('should send the POST request to the given endpoint', (done) => { |
155 + | |
156 + | service.createOnEndpoint(obj, observableOf('https://rest.api/core/custom?search')).subscribe(out => { |
157 + | expect(requestService.send).toHaveBeenCalledWith(jasmine.objectContaining({ |
158 + | method: RestRequestMethod.POST, |
159 + | href: 'https://rest.api/core/custom?search', |
160 + | })); |
161 + | done(); |
162 + | }); |
163 + | }); |
164 + | |
165 + | it('should return the remote data for the sent request', (done) => { |
166 + | service.createOnEndpoint(obj, observableOf('https://rest.api/core/custom?search')).subscribe(out => { |
167 + | expect(requestService.send).toHaveBeenCalledWith(jasmine.objectContaining({ |
168 + | method: RestRequestMethod.POST, |
169 + | uuid: requestService.generateRequestId(), |
170 + | })); |
171 + | expect(buildFromRequestUUIDSpy).toHaveBeenCalledWith(requestService.generateRequestId()); |
172 + | expect(notificationsService.error).not.toHaveBeenCalled(); |
173 + | expect(out).toEqual(remoteDataMocks.Success); |
174 + | done(); |
175 + | }); |
176 + | }); |
177 + | |
178 + | it('should show an error notification if the request fails', (done) => { |
179 + | buildFromRequestUUIDSpy.and.returnValue(observableOf(remoteDataMocks.Error)); |
180 + | |
181 + | service.createOnEndpoint(obj, observableOf('https://rest.api/core/custom?search')).subscribe(out => { |
182 + | expect(requestService.send).toHaveBeenCalledWith(jasmine.objectContaining({ |
183 + | method: RestRequestMethod.POST, |
184 + | uuid: requestService.generateRequestId(), |
185 + | })); |
186 + | expect(buildFromRequestUUIDSpy).toHaveBeenCalledWith(requestService.generateRequestId()); |
187 + | expect(notificationsService.error).toHaveBeenCalled(); |
188 + | expect(out).toEqual(remoteDataMocks.Error); |
189 + | done(); |
190 + | }); |
191 + | }); |
192 + | }); |
112 193 | }); |