element-plus/packages/components/collection/__tests__/collection-item.test.ts
Anas Boudih 9edd89c642
refactor(components): [collection] switch to script-setup syntax (#7710)
Co-authored-by: metanas <matanas@pre-history.com>
2022-05-16 22:33:46 +08:00

81 lines
2.2 KiB
TypeScript

import { h, nextTick } from 'vue'
import { mount } from '@vue/test-utils'
import { afterEach, describe, expect, it } from 'vitest'
import TestCollection, {
CollectionChildComponent,
CollectionItemChildComponent,
} from '../test-helper'
import type { ComponentPublicInstance } from 'vue'
import type { ElCollectionInjectionContext } from '../src/tokens'
const { ElCollection, ElCollectionItem } = TestCollection
const AXIOM = 'rem is the best girl'
describe('<ElCollectionItem />', () => {
const createComponent = (props = {}, count = 3) =>
mount(ElCollection as any, {
props,
slots: {
default: () => {
return h(
CollectionChildComponent as any,
{},
{
default: () =>
Array.from({ length: count }).map((idx) => {
return h(
ElCollectionItem as any,
{},
{
default: () => [
h(
CollectionItemChildComponent,
{},
{ default: () => `${AXIOM} ${idx}` }
),
],
}
)
}),
}
)
},
},
})
let wrapper: ReturnType<typeof createComponent>
afterEach(() => {
wrapper.unmount()
})
it('should be able to render correctly', async () => {
wrapper = createComponent()
await nextTick()
expect(wrapper.findAllComponents(ElCollectionItem as any)).toHaveLength(3)
expect(wrapper.findComponent(ElCollectionItem as any).text()).toContain(
AXIOM
)
})
it('register child instance', () => {
wrapper = createComponent()
const childItemComponent = wrapper.findComponent(
CollectionChildComponent as any
)
const childVm =
childItemComponent.vm as ComponentPublicInstance<ElCollectionInjectionContext>
const collectionItems = wrapper.findAllComponents(
CollectionItemChildComponent as any
)
expect(childVm.itemMap.size).toBe(3)
const items = childVm.getItems()
expect(childVm.getItems()).toHaveLength(3)
expect(items[0].ref).toBe(collectionItems.at(0)?.element)
})
})