mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-14 20:19:34 +01:00
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Benjamin Canac <canacb1@gmail.com>
85 lines
4.1 KiB
TypeScript
85 lines
4.1 KiB
TypeScript
import { describe, it, expect, test } from 'vitest'
|
|
import Tree from '../../src/runtime/components/Tree.vue'
|
|
import type { TreeProps, TreeSlots, 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]>()
|
|
})
|
|
})
|