feat(tag): hasAnyDocuments recursive helper + unit tests
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
29
frontend/src/lib/tag/tagUtils.test.ts
Normal file
29
frontend/src/lib/tag/tagUtils.test.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { hasAnyDocuments } from './tagUtils';
|
||||
import type { components } from '$lib/generated/api';
|
||||
|
||||
type TagTreeNodeDTO = components['schemas']['TagTreeNodeDTO'];
|
||||
|
||||
function makeNode(documentCount: number, children: TagTreeNodeDTO[] = []): TagTreeNodeDTO {
|
||||
return { id: 'id', name: 'name', documentCount, children };
|
||||
}
|
||||
|
||||
describe('hasAnyDocuments', () => {
|
||||
it('returns false for a leaf node with documentCount=0', () => {
|
||||
expect(hasAnyDocuments(makeNode(0))).toBe(false);
|
||||
});
|
||||
|
||||
it('returns true for a leaf node with documentCount=3', () => {
|
||||
expect(hasAnyDocuments(makeNode(3))).toBe(true);
|
||||
});
|
||||
|
||||
it('returns true for a root with documentCount=0 but a child with documentCount=5', () => {
|
||||
const node = makeNode(0, [makeNode(5)]);
|
||||
expect(hasAnyDocuments(node)).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false for a root with documentCount=0 and all children also 0', () => {
|
||||
const node = makeNode(0, [makeNode(0), makeNode(0)]);
|
||||
expect(hasAnyDocuments(node)).toBe(false);
|
||||
});
|
||||
});
|
||||
7
frontend/src/lib/tag/tagUtils.ts
Normal file
7
frontend/src/lib/tag/tagUtils.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import type { components } from '$lib/generated/api';
|
||||
|
||||
type TagTreeNodeDTO = components['schemas']['TagTreeNodeDTO'];
|
||||
|
||||
export function hasAnyDocuments(node: TagTreeNodeDTO): boolean {
|
||||
return (node.documentCount ?? 0) > 0 || (node.children ?? []).some(hasAnyDocuments);
|
||||
}
|
||||
Reference in New Issue
Block a user