-
Notifications
You must be signed in to change notification settings - Fork 688
/
Copy pathTree.spec.ts
83 lines (77 loc) · 4.01 KB
/
Tree.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { describe, it, expect, test } from 'vitest'
import Tree, { type TreeProps, type TreeSlots, type TreeItem } from '../../src/runtime/components/Tree.vue'
import ComponentRender from '../component-render'
import theme from '#build/ui/tree'
import { expectEmitPayloadType } from '../utils/types'
describe('Tree', () => {
const sizes = Object.keys(theme.variants.size) as any
const items: TreeItem[] = [
{
value: 'root',
label: 'app',
slot: 'app',
children: [{
label: 'composables',
icon: 'i-lucide-house',
children: [
{ label: 'useAuth.ts', icon: 'vscode-icons:file-type-typescript' },
{ label: 'useUser.ts', icon: 'vscode-icons:file-type-typescript' }
]
}]
},
{ value: 'app-vue', label: 'app.vue', icon: 'i-vscode-icons-file-type-vue' },
{ value: 'nuxt-config-ts', label: 'nuxt.config.ts', icon: 'i-vscode-icons-file-type-nuxt' }
]
const props = { items }
it.each([
// Props
['with items', { props }],
['with modelValue', { props: { ...props, modelValue: items[0] } }],
['with defaultValue', { props: { ...props, defaultValue: items[0] } }],
// Expanded
['with expanded', { props: { ...props, expanded: [items[0]] } }],
['with defaultExpanded', { props: { ...props, defaultExpanded: [items[0]] } }],
// Key mapping
['with valueKey', { props: { ...props, valueKey: 'label' } }],
['with labelKey', { props: { ...props, labelKey: 'value' } }],
// Multiple
['with multiple', { props: { ...props, multiple: true } }],
['with multiple and modelValue', { props: { ...props, multiple: true, modelValue: [items[0], items[1]] } }],
['with multiple and defaultValue', { props: { ...props, multiple: true, defaultValue: [items[0], items[1]] } }],
// Disabled
['with disabled', { props: { ...props, disabled: true } }],
// Item properties
['with defautExpanded item', { props: { items: [{ label: 'Default Expanded', defaultExpanded: true, children: items }] } }],
['with disabled item', { props: { items: [{ label: 'Disabled item', disabled: true, children: items }] } }],
// Icons
['with trailingIcon', { props: { ...props, trailingIcon: 'i-lucide-arrow-down' } }],
['with expandedIcon', { props: { items: [{ label: 'Default Expanded', defaultExpanded: true, children: items }], expandedIcon: 'i-lucide-chevron-up' } }],
['with collapsedIcon', { props: { items: [{ label: 'Default Collapsed', defaultExpanded: false, children: items }], collapsedIcon: 'i-lucide-chevron-down' } }],
// Style variants
...sizes.map((size: string) => [`with size ${size}`, { props: { ...props, size } }]),
['with neutral color', { props: { ...props, color: 'neutral' } }],
['with as', { props: { ...props, as: 'div' } }],
['with class', { props: { ...props, class: 'absolute' } }],
['with ui', { props: { ...props, ui: { link: 'font-bold' } } }],
// Slots
['with default slot', { props, slots: { default: () => 'default slot' } }],
['with item slot', { props, slots: { item: () => 'item slot' } }],
['with item-leading slot', { props, slots: { 'item-leading': () => 'leading slot' } }],
['with item-trailing slot', { props, slots: { 'item-trailing': () => 'trailing slot' } }],
['with dynamic slot', { props, slots: { app: () => 'dynamic slot' } }]
])('renders %s correctly', async (nameOrHtml: string, options: { props?: Partial<TreeProps>, slots?: Partial<TreeSlots> }) => {
const html = await ComponentRender(nameOrHtml, options, Tree)
expect(html).toMatchSnapshot()
})
test('should have the correct types', () => {
// with default `value` key
expectEmitPayloadType('update:modelValue', () => Tree({
items: [{ label: 'foo', value: 'bar' }, { label: 'baz', value: 'qux' }]
})).toEqualTypeOf<[string]>()
// with custom value key
expectEmitPayloadType('update:modelValue', () => Tree({
items: [{ label: 'foo', value: 'bar', id: 1 }, { label: 'baz', value: 'qux', id: 2 }],
valueKey: 'id'
})).toEqualTypeOf<[number]>()
})
})