shinkan-jinkendo/frontend/src/utils/skillCatalogTree.test.js

96 lines
3.0 KiB
JavaScript
Raw Normal View History

import { describe, expect, it } from 'vitest'
import {
buildSkillCatalogTree,
catalogSkillsForSelection,
collectSkillLeavesFromTree,
countCatalogSkillsWithoutCategory,
countCatalogSkillsWithoutMain,
defaultExpandedKeysForSkillTree,
filterSkillTreeByQuery,
SKILL_CATEGORY_UNASSIGNED_KEY,
SKILL_MAIN_UNASSIGNED_KEY,
skillCatalogPathLabel,
} from './skillCatalogTree.js'
const sample = [
{
id: 1,
name: 'Dachi Waza',
main_category_id: 10,
category_id: 20,
catalog_main_category_name: 'Karate',
catalog_main_sort: 1,
catalog_category_name: 'Kihon',
catalog_category_sort: 2,
sort_order: 1,
},
{
id: 2,
name: 'Kata Ablauf',
main_category_id: 10,
category_id: 21,
catalog_main_category_name: 'Karate',
catalog_main_sort: 1,
catalog_category_name: 'Kata',
catalog_category_sort: 1,
sort_order: 1,
},
]
describe('skillCatalogTree', () => {
it('buildSkillCatalogTree groups by main and category', () => {
const tree = buildSkillCatalogTree(sample)
expect(tree).toHaveLength(1)
expect(tree[0].label).toBe('Karate')
expect(tree[0].children).toHaveLength(2)
expect(tree[0].children[0].label).toBe('Kata')
const kataSkill = tree[0].children[0].children[0]
expect(kataSkill.type).toBe('skill')
expect(kataSkill.skillId).toBe(2)
})
it('defaultExpandedKeysForSkillTree opens main and category only', () => {
const tree = buildSkillCatalogTree(sample)
const keys = defaultExpandedKeysForSkillTree(tree)
expect(keys).toContain('m-10')
expect(keys.some((k) => k.includes('c-21'))).toBe(true)
expect(keys.some((k) => k.startsWith('s-'))).toBe(false)
})
it('skillCatalogPathLabel', () => {
expect(skillCatalogPathLabel(sample[0])).toBe('Karate › Kihon › Dachi Waza')
})
it('collectSkillLeavesFromTree respects exclude', () => {
const tree = buildSkillCatalogTree(sample)
const leaves = collectSkillLeavesFromTree(tree, [1])
expect(leaves).toHaveLength(1)
expect(leaves[0].id).toBe(2)
})
it('filterSkillTreeByQuery', () => {
const tree = buildSkillCatalogTree(sample)
const filtered = filterSkillTreeByQuery(tree, 'dachi')
const kihon = filtered[0].children.find((c) => c.label === 'Kihon')
expect(kihon?.children?.some((s) => s.skillId === 1)).toBe(true)
})
it('catalogSkillsForSelection lists unassigned buckets', () => {
const skills = [
{ id: 1, main_category_id: null, category_id: null },
{ id: 2, main_category_id: 10, category_id: null },
{ id: 3, main_category_id: 10, category_id: 20 },
]
expect(
catalogSkillsForSelection(skills, SKILL_MAIN_UNASSIGNED_KEY, SKILL_CATEGORY_UNASSIGNED_KEY).map(
(s) => s.id
)
).toEqual([1])
expect(catalogSkillsForSelection(skills, 10, SKILL_CATEGORY_UNASSIGNED_KEY).map((s) => s.id)).toEqual([
2,
])
expect(countCatalogSkillsWithoutMain(skills)).toBe(1)
expect(countCatalogSkillsWithoutCategory(skills, 10)).toBe(1)
})
})