|
1 | 1 | import { describe, it, expect } from 'vitest' |
2 | 2 | import type { DatabaseItem } from '../../src/types/database' |
3 | | -import { getDraftStatus } from '../../src/utils/draft' |
| 3 | +import { getDraftStatus, findDescendantsFromId } from '../../src/utils/draft' |
4 | 4 | import { DraftStatus } from '../../src/types/draft' |
5 | 5 | import { dbItemsList } from '../mocks/database' |
| 6 | +import { draftItemsList } from '../mocks/draft' |
| 7 | +import { ROOT_ITEM } from '../../src/utils/tree' |
6 | 8 |
|
7 | 9 | describe('getDraftStatus', () => { |
8 | 10 | it('draft is CREATED if originalDatabaseItem is not defined', () => { |
@@ -43,3 +45,51 @@ describe('getDraftStatus', () => { |
43 | 45 | expect(status).toBe(DraftStatus.Updated) |
44 | 46 | }) |
45 | 47 | }) |
| 48 | + |
| 49 | +describe('findDescendantsFromId', () => { |
| 50 | + it('returns exact match for a root level file', () => { |
| 51 | + const descendants = findDescendantsFromId(draftItemsList, 'landing/index.md') |
| 52 | + expect(descendants).toHaveLength(1) |
| 53 | + expect(descendants[0].id).toBe('landing/index.md') |
| 54 | + expect(descendants[0].fsPath).toBe('/index.md') |
| 55 | + }) |
| 56 | + |
| 57 | + it('returns empty array for non-existent id', () => { |
| 58 | + const descendants = findDescendantsFromId(draftItemsList, 'non-existent/file.md') |
| 59 | + expect(descendants).toHaveLength(0) |
| 60 | + }) |
| 61 | + |
| 62 | + it('returns all descendants files for a directory path', () => { |
| 63 | + const descendants = findDescendantsFromId(draftItemsList, 'docs/1.getting-started') |
| 64 | + |
| 65 | + expect(descendants).toHaveLength(5) |
| 66 | + |
| 67 | + expect(descendants.some(item => item.id === 'docs/1.getting-started/2.introduction.md')).toBe(true) |
| 68 | + expect(descendants.some(item => item.id === 'docs/1.getting-started/3.installation.md')).toBe(true) |
| 69 | + expect(descendants.some(item => item.id === 'docs/1.getting-started/4.configuration.md')).toBe(true) |
| 70 | + expect(descendants.some(item => item.id === 'docs/1.getting-started/1.advanced/1.studio.md')).toBe(true) |
| 71 | + expect(descendants.some(item => item.id === 'docs/1.getting-started/1.advanced/2.deployment.md')).toBe(true) |
| 72 | + }) |
| 73 | + |
| 74 | + it('returns all descendants for a nested directory path', () => { |
| 75 | + const descendants = findDescendantsFromId(draftItemsList, 'docs/1.getting-started/1.advanced') |
| 76 | + |
| 77 | + expect(descendants).toHaveLength(2) |
| 78 | + |
| 79 | + expect(descendants.some(item => item.id === 'docs/1.getting-started/1.advanced/1.studio.md')).toBe(true) |
| 80 | + expect(descendants.some(item => item.id === 'docs/1.getting-started/1.advanced/2.deployment.md')).toBe(true) |
| 81 | + }) |
| 82 | + |
| 83 | + it('returns all descendants for root item', () => { |
| 84 | + const descendants = findDescendantsFromId(draftItemsList, ROOT_ITEM.id) |
| 85 | + |
| 86 | + expect(descendants).toHaveLength(draftItemsList.length) |
| 87 | + }) |
| 88 | + |
| 89 | + it('returns only the file itself when searching for a specific file', () => { |
| 90 | + const descendants = findDescendantsFromId(draftItemsList, 'docs/1.getting-started/1.advanced/1.studio.md') |
| 91 | + |
| 92 | + expect(descendants).toHaveLength(1) |
| 93 | + expect(descendants[0].id).toBe('docs/1.getting-started/1.advanced/1.studio.md') |
| 94 | + }) |
| 95 | +}) |
0 commit comments