@@ -22,7 +22,7 @@ import { RequestEntryState } from '../request-entry-state.model';
2222import { DeleteData , DeleteDataImpl } from './delete-data' ;
2323import { NotificationsService } from '../../../shared/notifications/notifications.service' ;
2424import { createFailedRemoteDataObject , createSuccessfulRemoteDataObject , createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils' ;
25- import { fakeAsync , tick } from '@angular/core/testing ' ;
25+ import { RestRequestMethod } from '../rest-request-method ' ;
2626
2727/**
2828 * Tests whether calls to `DeleteData` methods are correctly patched through in a concrete data service that implements it
@@ -63,8 +63,6 @@ export function testDeleteDataImplementation(serviceFactory: () => DeleteData<an
6363
6464const endpoint = 'https://rest.api/core' ;
6565
66- const BOOLEAN = { f : false , t : true } ;
67-
6866class TestService extends DeleteDataImpl < any > {
6967 constructor (
7068 protected requestService : RequestService ,
@@ -155,13 +153,13 @@ describe('DeleteDataImpl', () => {
155153 let MOCK_FAILED_RD ;
156154
157155 let invalidateByHrefSpy : jasmine . Spy ;
158- let buildFromRequestUUIDSpy : jasmine . Spy ;
156+ let buildFromRequestUUIDAndAwaitSpy : jasmine . Spy ;
159157 let getIDHrefObsSpy : jasmine . Spy ;
160158 let deleteByHrefSpy : jasmine . Spy ;
161159
162160 beforeEach ( ( ) => {
163161 invalidateByHrefSpy = spyOn ( service , 'invalidateByHref' ) . and . returnValue ( observableOf ( true ) ) ;
164- buildFromRequestUUIDSpy = spyOn ( rdbService , 'buildFromRequestUUID ' ) . and . callThrough ( ) ;
162+ buildFromRequestUUIDAndAwaitSpy = spyOn ( rdbService , 'buildFromRequestUUIDAndAwait ' ) . and . callThrough ( ) ;
165163 getIDHrefObsSpy = spyOn ( service , 'getIDHrefObs' ) . and . callThrough ( ) ;
166164 deleteByHrefSpy = spyOn ( service , 'deleteByHref' ) . and . callThrough ( ) ;
167165
@@ -171,7 +169,7 @@ describe('DeleteDataImpl', () => {
171169
172170 it ( 'should retrieve href by ID and call deleteByHref' , ( ) => {
173171 getIDHrefObsSpy . and . returnValue ( observableOf ( 'some-href' ) ) ;
174- buildFromRequestUUIDSpy . and . returnValue ( createSuccessfulRemoteDataObject$ ( { } ) ) ;
172+ buildFromRequestUUIDAndAwaitSpy . and . returnValue ( createSuccessfulRemoteDataObject$ ( { } ) ) ;
175173
176174 service . delete ( 'some-id' , [ 'a' , 'b' , 'c' ] ) . subscribe ( rd => {
177175 expect ( getIDHrefObsSpy ) . toHaveBeenCalledWith ( 'some-id' ) ;
@@ -180,64 +178,51 @@ describe('DeleteDataImpl', () => {
180178 } ) ;
181179
182180 describe ( 'deleteByHref' , ( ) => {
183- it ( 'should call invalidateByHref if the DELETE request succeeds' , ( done ) => {
184- buildFromRequestUUIDSpy . and . returnValue ( observableOf ( MOCK_SUCCEEDED_RD ) ) ;
185-
186- service . deleteByHref ( 'some-href' ) . subscribe ( rd => {
187- expect ( rd ) . toBe ( MOCK_SUCCEEDED_RD ) ;
188- expect ( invalidateByHrefSpy ) . toHaveBeenCalled ( ) ;
181+ it ( 'should send a DELETE request' , ( done ) => {
182+ buildFromRequestUUIDAndAwaitSpy . and . returnValue ( observableOf ( MOCK_SUCCEEDED_RD ) ) ;
183+
184+ service . deleteByHref ( 'some-href' ) . subscribe ( ( ) => {
185+ expect ( requestService . send ) . toHaveBeenCalledWith ( jasmine . objectContaining ( {
186+ method : RestRequestMethod . DELETE ,
187+ href : 'some-href' ,
188+ } ) ) ;
189189 done ( ) ;
190190 } ) ;
191191 } ) ;
192192
193- it ( 'should call invalidateByHref even if not subscribing to returned Observable' , fakeAsync ( ( ) => {
194- buildFromRequestUUIDSpy . and . returnValue ( observableOf ( MOCK_SUCCEEDED_RD ) ) ;
195-
196- service . deleteByHref ( 'some-href' ) ;
197- tick ( ) ;
193+ it ( 'should include the virtual metadata to be copied in the DELETE request' , ( done ) => {
194+ buildFromRequestUUIDAndAwaitSpy . and . returnValue ( observableOf ( MOCK_SUCCEEDED_RD ) ) ;
198195
199- expect ( invalidateByHrefSpy ) . toHaveBeenCalled ( ) ;
200- } ) ) ;
201-
202- it ( 'should not call invalidateByHref if the DELETE request fails' , ( done ) => {
203- buildFromRequestUUIDSpy . and . returnValue ( observableOf ( MOCK_FAILED_RD ) ) ;
204-
205- service . deleteByHref ( 'some-href' ) . subscribe ( rd => {
206- expect ( rd ) . toBe ( MOCK_FAILED_RD ) ;
207- expect ( invalidateByHrefSpy ) . not . toHaveBeenCalled ( ) ;
196+ service . deleteByHref ( 'some-href' , [ 'a' , 'b' , 'c' ] ) . subscribe ( ( ) => {
197+ expect ( requestService . send ) . toHaveBeenCalledWith ( jasmine . objectContaining ( {
198+ method : RestRequestMethod . DELETE ,
199+ href : 'some-href?copyVirtualMetadata=a©VirtualMetadata=b©VirtualMetadata=c' ,
200+ } ) ) ;
208201 done ( ) ;
209202 } ) ;
210203 } ) ;
211204
212- it ( 'should wait for invalidateByHref before emitting' , ( ) => {
213- testScheduler . run ( ( { cold, expectObservable } ) => {
214- buildFromRequestUUIDSpy . and . returnValue (
215- cold ( '(r|)' , { r : MOCK_SUCCEEDED_RD } ) // RD emits right away
216- ) ;
217- invalidateByHrefSpy . and . returnValue (
218- cold ( '----(t|)' , BOOLEAN ) // but we pretend that setting requests to stale takes longer
205+ it ( 'should invalidate the currently cached object' , ( done ) => {
206+ service . deleteByHref ( 'some-href' ) . subscribe ( ( ) => {
207+ expect ( buildFromRequestUUIDAndAwaitSpy ) . toHaveBeenCalledWith (
208+ requestService . generateRequestId ( ) ,
209+ jasmine . anything ( ) ,
219210 ) ;
220211
221- const done$ = service . deleteByHref ( 'some-href' ) ;
222- expectObservable ( done$ ) . toBe (
223- '----(r|)' , { r : MOCK_SUCCEEDED_RD } // ...and expect the returned Observable to wait until that's done
224- ) ;
212+ const callback = ( rdbService . buildFromRequestUUIDAndAwait as jasmine . Spy ) . calls . argsFor ( 0 ) [ 1 ] ;
213+ callback ( ) ;
214+ expect ( service . invalidateByHref ) . toHaveBeenCalledWith ( 'some-href' ) ;
215+
216+ done ( ) ;
225217 } ) ;
226218 } ) ;
227219
228- it ( 'should wait for the DELETE request to resolve before emitting' , ( ) => {
229- testScheduler . run ( ( { cold, expectObservable } ) => {
230- buildFromRequestUUIDSpy . and . returnValue (
231- cold ( '----(r|)' , { r : MOCK_SUCCEEDED_RD } ) // the request takes a while
232- ) ;
233- invalidateByHrefSpy . and . returnValue (
234- cold ( '(t|)' , BOOLEAN ) // but we pretend that setting to stale happens sooner
235- ) ; // e.g.: maybe already stale before this call?
220+ it ( 'should return the RemoteData of the response' , ( done ) => {
221+ buildFromRequestUUIDAndAwaitSpy . and . returnValue ( observableOf ( MOCK_SUCCEEDED_RD ) ) ;
236222
237- const done$ = service . deleteByHref ( 'some-href' ) ;
238- expectObservable ( done$ ) . toBe (
239- '----(r|)' , { r : MOCK_SUCCEEDED_RD } // ...and expect the returned Observable to wait for the request
240- ) ;
223+ service . deleteByHref ( 'some-href' ) . subscribe ( rd => {
224+ expect ( rd ) . toBe ( MOCK_SUCCEEDED_RD ) ;
225+ done ( ) ;
241226 } ) ;
242227 } ) ;
243228 } ) ;
0 commit comments