@@ -11,8 +11,6 @@ import type { DatabaseItem } from '../../../src/types/database'
1111import { joinURL , withLeadingSlash } from 'ufo'
1212import { VirtualMediaCollectionName } from '../../../src/utils/media'
1313
14- const areDocumentsEqual = ( document1 : DatabaseItem , document2 : DatabaseItem ) => JSON . stringify ( document1 ) === JSON . stringify ( document2 )
15-
1614describe ( 'buildTree of documents with one level of depth' , ( ) => {
1715 // Result based on dbItemsList mock
1816 const result : TreeItem [ ] = [
@@ -48,7 +46,7 @@ describe('buildTree of documents with one level of depth', () => {
4846 ]
4947
5048 it ( 'Without draft' , ( ) => {
51- const tree = buildTree ( dbItemsList , null , areDocumentsEqual )
49+ const tree = buildTree ( dbItemsList , null )
5250 expect ( tree ) . toStrictEqual ( result as TreeItem [ ] )
5351 } )
5452
@@ -62,7 +60,7 @@ describe('buildTree of documents with one level of depth', () => {
6260 modified : createdDbItem ,
6361 } ]
6462
65- const tree = buildTree ( dbItemsList , draftList , areDocumentsEqual )
63+ const tree = buildTree ( dbItemsList , draftList )
6664
6765 expect ( tree ) . toStrictEqual ( [
6866 {
@@ -84,7 +82,7 @@ describe('buildTree of documents with one level of depth', () => {
8482
8583 const dbItemsListWithoutDeletedDbItem = dbItemsList . filter ( item => item . id !== deletedDbItem . id )
8684
87- const tree = buildTree ( dbItemsListWithoutDeletedDbItem , draftList , areDocumentsEqual )
85+ const tree = buildTree ( dbItemsListWithoutDeletedDbItem , draftList )
8886
8987 expect ( tree ) . toStrictEqual ( [
9088 { ...result [ 0 ] } ,
@@ -118,7 +116,7 @@ describe('buildTree of documents with one level of depth', () => {
118116
119117 const dbItemsListWithoutDeletedDbItem = dbItemsList . filter ( item => item . id !== deletedDbItem . id )
120118
121- const tree = buildTree ( dbItemsListWithoutDeletedDbItem , draftList , areDocumentsEqual )
119+ const tree = buildTree ( dbItemsListWithoutDeletedDbItem , draftList )
122120
123121 expect ( tree ) . toStrictEqual ( [
124122 result [ 0 ] ,
@@ -156,7 +154,7 @@ describe('buildTree of documents with one level of depth', () => {
156154 } ,
157155 } ]
158156
159- const tree = buildTree ( dbItemsList , draftList , areDocumentsEqual )
157+ const tree = buildTree ( dbItemsList , draftList )
160158
161159 const expectedTree = [
162160 result [ 0 ] ,
@@ -192,7 +190,7 @@ describe('buildTree of documents with one level of depth', () => {
192190 modified : openedDbItem ,
193191 } ]
194192
195- const tree = buildTree ( dbItemsList , draftList , areDocumentsEqual )
193+ const tree = buildTree ( dbItemsList , draftList )
196194
197195 const expectedTree = [
198196 result [ 0 ] ,
@@ -225,7 +223,7 @@ describe('buildTree of documents with one level of depth', () => {
225223 modified : openedDbItem2 ,
226224 } ]
227225
228- const tree = buildTree ( dbItemsList , draftList , areDocumentsEqual )
226+ const tree = buildTree ( dbItemsList , draftList )
229227
230228 const expectedTree = [
231229 result [ 0 ] ,
@@ -269,7 +267,7 @@ describe('buildTree of documents with one level of depth', () => {
269267 const dbItemsWithoutDeletedWithCreated = dbItemsList . filter ( item => item . id !== deletedDbItem . id )
270268 dbItemsWithoutDeletedWithCreated . push ( createdDbItem )
271269
272- const tree = buildTree ( dbItemsWithoutDeletedWithCreated , draftList , areDocumentsEqual )
270+ const tree = buildTree ( dbItemsWithoutDeletedWithCreated , draftList )
273271
274272 expect ( tree ) . toStrictEqual ( [
275273 result [ 0 ] ,
@@ -327,7 +325,7 @@ describe('buildTree of documents with two levels of depth', () => {
327325 ]
328326
329327 it ( 'Without draft' , ( ) => {
330- const tree = buildTree ( nestedDbItemsList , null , areDocumentsEqual )
328+ const tree = buildTree ( nestedDbItemsList , null )
331329 expect ( tree ) . toStrictEqual ( result )
332330 } )
333331
@@ -347,7 +345,7 @@ describe('buildTree of documents with two levels of depth', () => {
347345 } ,
348346 } ]
349347
350- const tree = buildTree ( nestedDbItemsList , draftList , areDocumentsEqual )
348+ const tree = buildTree ( nestedDbItemsList , draftList )
351349
352350 expect ( tree ) . toStrictEqual ( [ {
353351 ...result [ 0 ] ,
@@ -375,7 +373,7 @@ describe('buildTree of documents with two levels of depth', () => {
375373 } ,
376374 } ]
377375
378- const tree = buildTree ( nestedDbItemsList , draftList , areDocumentsEqual )
376+ const tree = buildTree ( nestedDbItemsList , draftList )
379377
380378 expect ( tree ) . toStrictEqual ( [ {
381379 ...result [ 0 ] ,
@@ -409,7 +407,7 @@ describe('buildTree of documents with two levels of depth', () => {
409407 // Remove the deleted item from the nestedDbItemsList
410408 const nestedDbItemsListWithoutDeletedDbItem = nestedDbItemsList . filter ( item => item . id !== deletedDbItem . id )
411409
412- const tree = buildTree ( nestedDbItemsListWithoutDeletedDbItem , draftList , areDocumentsEqual )
410+ const tree = buildTree ( nestedDbItemsListWithoutDeletedDbItem , draftList )
413411
414412 expect ( tree ) . toStrictEqual ( [ {
415413 ...result [ 0 ] ,
@@ -477,7 +475,7 @@ describe('buildTree of documents with language prefixed', () => {
477475 ]
478476
479477 it ( 'Without draft' , ( ) => {
480- const tree = buildTree ( languagePrefixedDbItemsList , null , areDocumentsEqual )
478+ const tree = buildTree ( languagePrefixedDbItemsList , null )
481479 expect ( tree ) . toStrictEqual ( result )
482480 } )
483481} )
@@ -513,7 +511,7 @@ describe('buildTree of medias', () => {
513511 modified : gitkeepDbItem ,
514512 } ]
515513
516- const tree = buildTree ( [ gitkeepDbItem , mediaDbItem ] , draftList , areDocumentsEqual )
514+ const tree = buildTree ( [ gitkeepDbItem , mediaDbItem ] , draftList )
517515
518516 expect ( tree ) . toHaveLength ( 1 )
519517 expect ( tree [ 0 ] ) . toHaveProperty ( 'fsPath' , mediaFolderName )
@@ -529,59 +527,91 @@ describe('buildTree of medias', () => {
529527} )
530528
531529describe ( 'getTreeStatus' , ( ) => {
532- it ( 'draft is CREATED if originalDatabaseItem is not defined' , ( ) => {
533- const modified : DatabaseItem = dbItemsList [ 0 ] // index.md
530+ it ( 'should return OPENED when draft status is Pristine' , ( ) => {
531+ const draftItem : DraftItem = {
532+ fsPath : 'index.md' ,
533+ status : DraftStatus . Pristine ,
534+ original : dbItemsList [ 0 ] ,
535+ modified : dbItemsList [ 0 ] ,
536+ }
534537
535- const status = getTreeStatus ( modified , undefined as never , areDocumentsEqual )
536- expect ( status ) . toBe ( TreeStatus . Created )
538+ const status = getTreeStatus ( draftItem )
539+ expect ( status ) . toBe ( TreeStatus . Opened )
537540 } )
538541
539- it ( 'draft is OPENED if originalDatabaseItem is defined and is the same as draftedDocument' , ( ) => {
540- const original : DatabaseItem = dbItemsList [ 0 ] // index.md
542+ it ( 'should return DELETED when draft status is Deleted' , ( ) => {
543+ const draftItem : DraftItem = {
544+ fsPath : 'index.md' ,
545+ status : DraftStatus . Deleted ,
546+ original : dbItemsList [ 0 ] ,
547+ modified : undefined ,
548+ }
541549
542- const status = getTreeStatus ( original , original , areDocumentsEqual )
543- expect ( status ) . toBe ( TreeStatus . Opened )
550+ const status = getTreeStatus ( draftItem )
551+ expect ( status ) . toBe ( TreeStatus . Deleted )
544552 } )
545553
546- it ( 'draft is UPDATED if originalDatabaseItem is defined and one of its data field is different from draftedDocument ' , ( ) => {
554+ it ( 'should return UPDATED when draft status is Updated ' , ( ) => {
547555 const original : DatabaseItem = dbItemsList [ 0 ]
548556 const modified : DatabaseItem = {
549557 ...original ,
550558 title : 'New title' ,
551559 }
552560
553- const status = getTreeStatus ( modified , original , areDocumentsEqual )
554- expect ( status ) . toBe ( TreeStatus . Updated )
555- } )
556-
557- it ( 'draft is UPDATED if originalDatabaseItem is defined and its body is different from draftedDocument' , ( ) => {
558- const original : DatabaseItem = dbItemsList [ 0 ]
559- const modified : DatabaseItem = {
560- ...original ,
561- body : { type : 'minimark' , value : [ 'New body' ] } ,
561+ const draftItem : DraftItem = {
562+ fsPath : 'index.md' ,
563+ status : DraftStatus . Updated ,
564+ original,
565+ modified,
562566 }
563567
564- const status = getTreeStatus ( modified , original , areDocumentsEqual )
568+ const status = getTreeStatus ( draftItem )
565569 expect ( status ) . toBe ( TreeStatus . Updated )
566570 } )
567571
568- it ( 'draft is RENAMED if originalDatabaseItem is defined and id is different from draftedDocument ' , ( ) => {
572+ it ( 'should return RENAMED when draft status is Created and original. id differs from modified.id ' , ( ) => {
569573 const original : DatabaseItem = dbItemsList [ 0 ] // index.md
570574 const modified : DatabaseItem = {
571575 ...original ,
572576 id : 'renamed.md' ,
573577 }
574578
575- const status = getTreeStatus ( modified , original , areDocumentsEqual )
579+ const draftItem : DraftItem = {
580+ fsPath : 'renamed.md' ,
581+ status : DraftStatus . Created ,
582+ original,
583+ modified,
584+ }
585+
586+ const status = getTreeStatus ( draftItem )
576587 expect ( status ) . toBe ( TreeStatus . Renamed )
577588 } )
578589
579- it ( 'draft is DELETED if modifiedDatabaseItem is not defined' , ( ) => {
580- const original : DatabaseItem = dbItemsList [ 0 ] // index.md
581- const modified : DatabaseItem = undefined as never
590+ it ( 'should return CREATED when draft status is Created without original' , ( ) => {
591+ const draftItem : DraftItem = {
592+ fsPath : 'index.md' ,
593+ status : DraftStatus . Created ,
594+ original : undefined ,
595+ modified : dbItemsList [ 0 ] ,
596+ }
582597
583- const status = getTreeStatus ( modified , original , areDocumentsEqual )
584- expect ( status ) . toBe ( TreeStatus . Deleted )
598+ const status = getTreeStatus ( draftItem )
599+ expect ( status ) . toBe ( TreeStatus . Created )
600+ } )
601+
602+ it ( 'should return CREATED when draft status is Created with original that has same id' , ( ) => {
603+ const original : DatabaseItem = dbItemsList [ 0 ]
604+ const modified : DatabaseItem = dbItemsList [ 0 ]
605+
606+ const draftItem : DraftItem = {
607+ fsPath : 'index.md' ,
608+ status : DraftStatus . Created ,
609+ original,
610+ modified,
611+ }
612+
613+ const status = getTreeStatus ( draftItem )
614+ expect ( status ) . toBe ( TreeStatus . Created )
585615 } )
586616} )
587617
0 commit comments