@@ -8,6 +8,7 @@ jest.mock('unique-slug')
8
8
const uniqueSlug = require ( 'unique-slug' )
9
9
const mdurl = require ( 'mdurl' )
10
10
const fse = require ( 'fs-extra' )
11
+ jest . mock ( 'sander' )
11
12
const sander = require ( 'sander' )
12
13
13
14
const systemUnderTest = require ( 'browser/main/lib/dataApi/attachmentManagement' )
@@ -50,11 +51,13 @@ it('should test that copyAttachment works correctly assuming correct working of
50
51
const noteKey = 'noteKey'
51
52
const dummyUniquePath = 'dummyPath'
52
53
const dummyStorage = { path : 'dummyStoragePath' }
54
+ const dummyReadStream = { }
53
55
56
+ dummyReadStream . pipe = jest . fn ( )
57
+ dummyReadStream . on = jest . fn ( ( event , callback ) => { callback ( ) } )
54
58
fs . existsSync = jest . fn ( )
55
59
fs . existsSync . mockReturnValue ( true )
56
- fs . createReadStream = jest . fn ( )
57
- fs . createReadStream . mockReturnValue ( { pipe : jest . fn ( ) } )
60
+ fs . createReadStream = jest . fn ( ( ) => dummyReadStream )
58
61
fs . createWriteStream = jest . fn ( )
59
62
60
63
findStorage . findStorage = jest . fn ( )
@@ -77,7 +80,11 @@ it('should test that copyAttachment creates a new folder if the attachment folde
77
80
const noteKey = 'noteKey'
78
81
const attachmentFolderPath = path . join ( dummyStorage . path , systemUnderTest . DESTINATION_FOLDER )
79
82
const attachmentFolderNoteKyPath = path . join ( dummyStorage . path , systemUnderTest . DESTINATION_FOLDER , noteKey )
83
+ const dummyReadStream = { }
80
84
85
+ dummyReadStream . pipe = jest . fn ( )
86
+ dummyReadStream . on = jest . fn ( )
87
+ fs . createReadStream = jest . fn ( ( ) => dummyReadStream )
81
88
fs . existsSync = jest . fn ( )
82
89
fs . existsSync . mockReturnValueOnce ( true )
83
90
fs . existsSync . mockReturnValueOnce ( false )
@@ -99,7 +106,11 @@ it('should test that copyAttachment creates a new folder if the attachment folde
99
106
100
107
it ( 'should test that copyAttachment don\'t uses a random file name if not intended ' , function ( ) {
101
108
const dummyStorage = { path : 'dummyStoragePath' }
109
+ const dummyReadStream = { }
102
110
111
+ dummyReadStream . pipe = jest . fn ( )
112
+ dummyReadStream . on = jest . fn ( )
113
+ fs . createReadStream = jest . fn ( ( ) => dummyReadStream )
103
114
fs . existsSync = jest . fn ( )
104
115
fs . existsSync . mockReturnValueOnce ( true )
105
116
fs . existsSync . mockReturnValueOnce ( false )
@@ -383,3 +394,76 @@ it('should test that moveAttachments returns a correct modified content version'
383
394
const actualContent = systemUnderTest . moveAttachments ( oldPath , newPath , oldNoteKey , newNoteKey , testInput )
384
395
expect ( actualContent ) . toBe ( expectedOutput )
385
396
} )
397
+
398
+ it ( 'should test that cloneAttachments modifies the content of the new note correctly' , function ( ) {
399
+ const oldNote = { key : 'oldNoteKey' , content : 'oldNoteContent' , storage : 'storageKey' , type : 'MARKDOWN_NOTE' }
400
+ const newNote = { key : 'newNoteKey' , content : 'oldNoteContent' , storage : 'storageKey' , type : 'MARKDOWN_NOTE' }
401
+ const testInput =
402
+ 'Test input' +
403
+ ' \n' +
404
+ '[' + systemUnderTest . STORAGE_FOLDER_PLACEHOLDER + path . sep + oldNote . key + path . sep + 'pdf.pdf](pdf})'
405
+ newNote . content = testInput
406
+
407
+ const expectedOutput =
408
+ 'Test input' +
409
+ ' \n' +
410
+ '[' + systemUnderTest . STORAGE_FOLDER_PLACEHOLDER + path . sep + newNote . key + path . sep + 'pdf.pdf](pdf})'
411
+ systemUnderTest . cloneAttachments ( oldNote , newNote )
412
+
413
+ expect ( newNote . content ) . toBe ( expectedOutput )
414
+ } )
415
+
416
+ it ( 'should test that cloneAttachments finds all attachments and copies them to the new location' , function ( ) {
417
+ const storagePathOld = 'storagePathOld'
418
+ const storagePathNew = 'storagePathNew'
419
+ const dummyStorageOld = { path : storagePathOld }
420
+ const dummyStorageNew = { path : storagePathNew }
421
+ const oldNote = { key : 'oldNoteKey' , content : 'oldNoteContent' , storage : 'storageKeyOldNote' , type : 'MARKDOWN_NOTE' }
422
+ const newNote = { key : 'newNoteKey' , content : 'oldNoteContent' , storage : 'storageKeyNewNote' , type : 'MARKDOWN_NOTE' }
423
+ const testInput =
424
+ 'Test input' +
425
+ ' \n' +
426
+ '[' + systemUnderTest . STORAGE_FOLDER_PLACEHOLDER + path . sep + oldNote . key + path . sep + 'pdf.pdf](pdf})'
427
+ oldNote . content = testInput
428
+ newNote . content = testInput
429
+
430
+ const copyFileSyncResp = { to : jest . fn ( ) }
431
+ sander . copyFileSync = jest . fn ( )
432
+ sander . copyFileSync . mockReturnValue ( copyFileSyncResp )
433
+ findStorage . findStorage = jest . fn ( )
434
+ findStorage . findStorage . mockReturnValueOnce ( dummyStorageOld )
435
+ findStorage . findStorage . mockReturnValue ( dummyStorageNew )
436
+
437
+ const pathAttachmentOneFrom = path . join ( storagePathOld , systemUnderTest . DESTINATION_FOLDER , oldNote . key , 'image.jpg' )
438
+ const pathAttachmentOneTo = path . join ( storagePathNew , systemUnderTest . DESTINATION_FOLDER , newNote . key , 'image.jpg' )
439
+
440
+ const pathAttachmentTwoFrom = path . join ( storagePathOld , systemUnderTest . DESTINATION_FOLDER , oldNote . key , 'pdf.pdf' )
441
+ const pathAttachmentTwoTo = path . join ( storagePathNew , systemUnderTest . DESTINATION_FOLDER , newNote . key , 'pdf.pdf' )
442
+
443
+ systemUnderTest . cloneAttachments ( oldNote , newNote )
444
+
445
+ expect ( findStorage . findStorage ) . toHaveBeenCalledWith ( oldNote . storage )
446
+ expect ( findStorage . findStorage ) . toHaveBeenCalledWith ( newNote . storage )
447
+ expect ( sander . copyFileSync ) . toHaveBeenCalledTimes ( 2 )
448
+ expect ( copyFileSyncResp . to ) . toHaveBeenCalledTimes ( 2 )
449
+ expect ( sander . copyFileSync . mock . calls [ 0 ] [ 0 ] ) . toBe ( pathAttachmentOneFrom )
450
+ expect ( copyFileSyncResp . to . mock . calls [ 0 ] [ 0 ] ) . toBe ( pathAttachmentOneTo )
451
+ expect ( sander . copyFileSync . mock . calls [ 1 ] [ 0 ] ) . toBe ( pathAttachmentTwoFrom )
452
+ expect ( copyFileSyncResp . to . mock . calls [ 1 ] [ 0 ] ) . toBe ( pathAttachmentTwoTo )
453
+ } )
454
+
455
+ it ( 'should test that cloneAttachments finds all attachments and copies them to the new location' , function ( ) {
456
+ const oldNote = { key : 'oldNoteKey' , content : 'oldNoteContent' , storage : 'storageKeyOldNote' , type : 'SOMETHING_ELSE' }
457
+ const newNote = { key : 'newNoteKey' , content : 'oldNoteContent' , storage : 'storageKeyNewNote' , type : 'SOMETHING_ELSE' }
458
+ const testInput = 'Test input'
459
+ oldNote . content = testInput
460
+ newNote . content = testInput
461
+
462
+ sander . copyFileSync = jest . fn ( )
463
+ findStorage . findStorage = jest . fn ( )
464
+
465
+ systemUnderTest . cloneAttachments ( oldNote , newNote )
466
+
467
+ expect ( findStorage . findStorage ) . not . toHaveBeenCalled ( )
468
+ expect ( sander . copyFileSync ) . not . toHaveBeenCalled ( )
469
+ } )
0 commit comments