2022-01-04 09:15:15 +08:00
|
|
|
import { h, nextTick } from 'vue'
|
|
|
|
import { mount } from '@vue/test-utils'
|
2022-04-19 12:46:57 +08:00
|
|
|
import { afterEach, describe, expect, it } from 'vitest'
|
2022-01-04 09:15:15 +08:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
|
2022-04-19 12:46:57 +08:00
|
|
|
it('should be able to render correctly', async () => {
|
|
|
|
wrapper = createComponent()
|
|
|
|
await nextTick()
|
2022-01-04 09:15:15 +08:00
|
|
|
|
2022-04-19 12:46:57 +08:00
|
|
|
expect(wrapper.findAllComponents(ElCollectionItem as any)).toHaveLength(3)
|
|
|
|
expect(wrapper.findComponent(ElCollectionItem as any).text()).toContain(
|
|
|
|
AXIOM
|
|
|
|
)
|
2022-01-04 09:15:15 +08:00
|
|
|
})
|
|
|
|
|
2022-04-19 12:46:57 +08:00
|
|
|
it('register child instance', () => {
|
2022-01-04 09:15:15 +08:00
|
|
|
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)
|
2022-05-16 22:33:46 +08:00
|
|
|
expect(items[0].ref).toBe(collectionItems.at(0)?.element)
|
2022-01-04 09:15:15 +08:00
|
|
|
})
|
|
|
|
})
|