@@ -318,6 +318,81 @@ describe('Backend', () => {
318
318
} ) ;
319
319
} ) ;
320
320
321
+ describe ( 'persistEntry' , ( ) => {
322
+ it ( 'should update the draft with the new entry returned by preSave event' , async ( ) => {
323
+ const implementation = {
324
+ init : jest . fn ( ( ) => implementation ) ,
325
+ persistEntry : jest . fn ( ( ) => implementation ) ,
326
+ } ;
327
+
328
+ const config = {
329
+ backend : {
330
+ commit_messages : 'commit-messages' ,
331
+ } ,
332
+ } ;
333
+ const collection = Map ( {
334
+ name : 'posts' ,
335
+ } ) ;
336
+ const entry = Map ( {
337
+ data : 'old_data' ,
338
+ } ) ;
339
+ const newEntry = Map ( {
340
+ data : 'new_data' ,
341
+ } ) ;
342
+ const entryDraft = Map ( {
343
+ entry,
344
+ } ) ;
345
+ const user = { login : 'login' , name : 'name' } ;
346
+ const backend = new Backend ( implementation , { config, backendName : 'github' } ) ;
347
+
348
+ backend . currentUser = jest . fn ( ) . mockResolvedValue ( user ) ;
349
+ backend . entryToRaw = jest . fn ( ) . mockReturnValue ( 'content' ) ;
350
+ backend . invokePreSaveEvent = jest . fn ( ) . mockReturnValueOnce ( newEntry ) ;
351
+
352
+ await backend . persistEntry ( { config, collection, entryDraft } ) ;
353
+
354
+ expect ( backend . entryToRaw ) . toHaveBeenCalledTimes ( 1 ) ;
355
+ expect ( backend . entryToRaw ) . toHaveBeenCalledWith ( collection , newEntry ) ;
356
+ } ) ;
357
+
358
+ it ( 'should update the draft with the new data returned by preSave event' , async ( ) => {
359
+ const implementation = {
360
+ init : jest . fn ( ( ) => implementation ) ,
361
+ persistEntry : jest . fn ( ( ) => implementation ) ,
362
+ } ;
363
+
364
+ const config = {
365
+ backend : {
366
+ commit_messages : 'commit-messages' ,
367
+ } ,
368
+ } ;
369
+ const collection = Map ( {
370
+ name : 'posts' ,
371
+ } ) ;
372
+ const entry = Map ( {
373
+ data : Map ( { } ) ,
374
+ } ) ;
375
+ const newData = Map ( { } ) ;
376
+ const newEntry = Map ( {
377
+ data : newData ,
378
+ } ) ;
379
+ const entryDraft = Map ( {
380
+ entry,
381
+ } ) ;
382
+ const user = { login : 'login' , name : 'name' } ;
383
+ const backend = new Backend ( implementation , { config, backendName : 'github' } ) ;
384
+
385
+ backend . currentUser = jest . fn ( ) . mockResolvedValue ( user ) ;
386
+ backend . entryToRaw = jest . fn ( ) . mockReturnValue ( 'content' ) ;
387
+ backend . invokePreSaveEvent = jest . fn ( ) . mockReturnValueOnce ( newData ) ;
388
+
389
+ await backend . persistEntry ( { config, collection, entryDraft } ) ;
390
+
391
+ expect ( backend . entryToRaw ) . toHaveBeenCalledTimes ( 1 ) ;
392
+ expect ( backend . entryToRaw ) . toHaveBeenCalledWith ( collection , newEntry ) ;
393
+ } ) ;
394
+ } ) ;
395
+
321
396
describe ( 'persistMedia' , ( ) => {
322
397
it ( 'should persist media' , async ( ) => {
323
398
const persistMediaResult = { } ;
0 commit comments