Files
element-plus/packages/components/select/__tests__/options.test.tsx
2026-01-13 23:11:17 +01:00

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).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).toHaveBeenCalled()
vi.resetAllMocks()
})
})