mirror of
https://github.com/element-plus/element-plus.git
synced 2026-03-13 07:51:17 +08:00
* fix(components): [select] Slot default invoked outside of the render * test: fix expect error --------- Co-authored-by: rzzf <cszhjh@gmail.com>
92 lines
2.3 KiB
TypeScript
92 lines
2.3 KiB
TypeScript
import { defineComponent, nextTick } from 'vue'
|
|
import { mount } from '@vue/test-utils'
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import Options from '../src/options'
|
|
import Select from '../src/select.vue'
|
|
|
|
import type { PropType, Slots } from 'vue'
|
|
import type { VueWrapper } from '@vue/test-utils'
|
|
|
|
describe('options', () => {
|
|
let wrapper: ReturnType<typeof mount>
|
|
|
|
const ElOptionStub = defineComponent({
|
|
name: 'ElOption',
|
|
props: {
|
|
label: String,
|
|
value: [String, Number, Boolean, Object] as PropType<
|
|
string | number | boolean | object
|
|
>,
|
|
},
|
|
template: '<div></div>',
|
|
})
|
|
|
|
const getLabel = (i: number | string) => `label-${i}`
|
|
|
|
const ElOptionGroupStub = defineComponent({
|
|
name: 'ElOptionGroup',
|
|
props: {
|
|
label: String,
|
|
},
|
|
template: '<div><slot /></div>',
|
|
})
|
|
|
|
const samples = Array.from({ length: 3 })
|
|
|
|
const createWrapper = (slots = {}) => {
|
|
wrapper = mount(
|
|
(_: unknown, { slots }: { slots: Slots }) => (
|
|
<Select>
|
|
<Options>{slots?.default?.()}</Options>
|
|
</Select>
|
|
),
|
|
{
|
|
global: {
|
|
components: {
|
|
ElOption: ElOptionStub,
|
|
ElOptionGroup: ElOptionGroupStub,
|
|
},
|
|
},
|
|
slots,
|
|
}
|
|
) as VueWrapper<any>
|
|
}
|
|
|
|
afterEach(() => {
|
|
wrapper.unmount()
|
|
})
|
|
|
|
it('renders emit correct options', async () => {
|
|
const mockWarn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
createWrapper({
|
|
default: () =>
|
|
samples.map((_, i) => <ElOptionStub label={getLabel(i)} />),
|
|
})
|
|
expect(mockWarn).not.toHaveBeenCalled()
|
|
vi.resetAllMocks()
|
|
await nextTick()
|
|
})
|
|
|
|
it('renders emit correct options with option group', async () => {
|
|
const spy = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
createWrapper({
|
|
default: () =>
|
|
samples.map((_, i) => (
|
|
<ElOptionGroupStub label={getLabel(i)}>
|
|
{{
|
|
default: () =>
|
|
samples.map((_, j) => (
|
|
<ElOptionStub
|
|
label={getLabel(`${i}-${j}`)}
|
|
value={j}
|
|
></ElOptionStub>
|
|
)),
|
|
}}
|
|
</ElOptionGroupStub>
|
|
)),
|
|
})
|
|
expect(spy).not.toHaveBeenCalled()
|
|
vi.resetAllMocks()
|
|
})
|
|
})
|