Skip to content

Commit 59d087b

Browse files
committed
refactor getDraftStatus
1 parent 965131f commit 59d087b

File tree

2 files changed

+85
-68
lines changed

2 files changed

+85
-68
lines changed

src/app/src/utils/tree.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import {
2-
ContentFileExtension,
32
DraftStatus,
43
TreeStatus,
5-
type DatabasePageItem,
64
type DraftItem,
75
type TreeItem,
86
} from '../types'
@@ -27,7 +25,7 @@ export const COLOR_UI_STATUS_MAP: { [key in TreeStatus]?: string } = {
2725
[TreeStatus.Opened]: 'neutral',
2826
} as const
2927

30-
export function buildTree(dbItems: BaseItem[], draftList: DraftItem[] | null, comparisonMethod: (document1: DatabasePageItem, document2: DatabasePageItem) => boolean):
28+
export function buildTree(dbItems: BaseItem[], draftList: DraftItem[] | null):
3129
TreeItem[] {
3230
const tree: TreeItem[] = []
3331
const directoryMap = new Map<string, TreeItem>()
@@ -91,7 +89,7 @@ TreeItem[] {
9189

9290
const draftFileItem = draftList?.find(draft => draft.fsPath === dbItem.fsPath)
9391
if (draftFileItem) {
94-
fileItem.status = getTreeStatus(draftFileItem.modified!, draftFileItem.original!, comparisonMethod)
92+
fileItem.status = getTreeStatus(draftFileItem)
9593
}
9694

9795
tree.push(fileItem)
@@ -148,7 +146,7 @@ TreeItem[] {
148146

149147
const draftFileItem = draftList?.find(draft => draft.fsPath === dbItem.fsPath)
150148
if (draftFileItem) {
151-
fileItem.status = getTreeStatus(draftFileItem.modified!, draftFileItem.original!, comparisonMethod)
149+
fileItem.status = getTreeStatus(draftFileItem)
152150
}
153151

154152
if (dbItem.path) {
@@ -163,36 +161,25 @@ TreeItem[] {
163161
return tree
164162
}
165163

166-
export function getTreeStatus(modified: BaseItem, original: BaseItem, comparisonMethod: (document1: DatabasePageItem, document2: DatabasePageItem) => boolean): TreeStatus {
167-
if (studioFlags.dev) {
164+
export function getTreeStatus(draftItem: DraftItem): TreeStatus {
165+
if (draftItem.status === DraftStatus.Pristine) {
168166
return TreeStatus.Opened
169167
}
170168

171-
if (!original && !modified) {
172-
throw new Error('Unconsistent state: both modified and original are undefined')
173-
}
174-
175-
if (!original) {
176-
return TreeStatus.Created
177-
}
178-
179-
if (!modified) {
169+
if (draftItem.status === DraftStatus.Deleted) {
180170
return TreeStatus.Deleted
181171
}
182172

183-
if (modified.id !== original.id) {
184-
return TreeStatus.Renamed
173+
if (draftItem.status === DraftStatus.Updated) {
174+
return TreeStatus.Updated
185175
}
186176

187-
if (original.extension === ContentFileExtension.Markdown) {
188-
if (!comparisonMethod(original as DatabasePageItem, modified as DatabasePageItem)) {
189-
return TreeStatus.Updated
190-
}
191-
}
192-
else {
193-
if (JSON.stringify(original) !== JSON.stringify(modified)) {
194-
return TreeStatus.Updated
177+
if (draftItem.status === DraftStatus.Created) {
178+
const { original, modified } = draftItem
179+
if (original && modified && original.id !== modified.id) {
180+
return TreeStatus.Renamed
195181
}
182+
return TreeStatus.Created
196183
}
197184

198185
return TreeStatus.Opened

src/app/test/unit/utils/tree.test.ts

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import type { DatabaseItem } from '../../../src/types/database'
1111
import { joinURL, withLeadingSlash } from 'ufo'
1212
import { VirtualMediaCollectionName } from '../../../src/utils/media'
1313

14-
const areDocumentsEqual = (document1: DatabaseItem, document2: DatabaseItem) => JSON.stringify(document1) === JSON.stringify(document2)
15-
1614
describe('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

531529
describe('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

Comments
 (0)