2022-02-21 14:28:22 +08:00
|
|
|
import { defineComponent, provide } from 'vue'
|
2021-09-13 23:14:48 +08:00
|
|
|
import { NOOP } from '@vue/shared'
|
|
|
|
import { mount } from '@vue/test-utils'
|
2022-02-21 14:28:22 +08:00
|
|
|
import { describe, it, expect } from 'vitest'
|
2021-09-13 23:14:48 +08:00
|
|
|
import { ElButton } from '@element-plus/components'
|
|
|
|
import {
|
2022-03-06 22:20:56 +08:00
|
|
|
formContextKey,
|
|
|
|
formItemContextKey,
|
2021-11-23 00:10:54 +08:00
|
|
|
buttonGroupContextKey,
|
2021-09-13 23:14:48 +08:00
|
|
|
} from '@element-plus/tokens'
|
|
|
|
|
|
|
|
import type {
|
2022-03-06 22:20:56 +08:00
|
|
|
FormContext,
|
|
|
|
FormItemContext,
|
2021-11-23 00:10:54 +08:00
|
|
|
ButtonGroupContext,
|
2021-09-13 23:14:48 +08:00
|
|
|
} from '@element-plus/tokens'
|
|
|
|
|
|
|
|
const AXIOM = 'Rem is the best girl'
|
|
|
|
|
2022-02-21 14:28:22 +08:00
|
|
|
const mountComponent = (setup = NOOP, options = {}) =>
|
|
|
|
mount(
|
|
|
|
defineComponent({
|
2021-09-13 23:14:48 +08:00
|
|
|
setup,
|
2022-02-21 14:28:22 +08:00
|
|
|
render() {
|
|
|
|
return <ElButton {...this.$attrs}>{AXIOM}</ElButton>
|
|
|
|
},
|
|
|
|
}),
|
2021-09-13 23:14:48 +08:00
|
|
|
options
|
|
|
|
)
|
|
|
|
|
|
|
|
describe('use-form-item', () => {
|
|
|
|
it('should return local value', () => {
|
|
|
|
const wrapper = mountComponent()
|
2022-02-12 18:37:16 +08:00
|
|
|
expect(wrapper.find('.el-button--default').exists()).toBe(true)
|
2021-09-13 23:14:48 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return props.size instead of injected.size', () => {
|
2021-12-12 17:54:21 +08:00
|
|
|
const propSize = 'small'
|
2021-09-13 23:14:48 +08:00
|
|
|
const wrapper = mountComponent(
|
|
|
|
() => {
|
2022-03-06 22:20:56 +08:00
|
|
|
provide(formItemContextKey, { size: 'large' })
|
2021-09-13 23:14:48 +08:00
|
|
|
},
|
|
|
|
{
|
2022-02-21 14:28:22 +08:00
|
|
|
props: { size: propSize },
|
2021-09-13 23:14:48 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-02-12 18:37:16 +08:00
|
|
|
expect(wrapper.find(`.el-button--${propSize}`).exists()).toBe(true)
|
2021-09-13 23:14:48 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return fallback.size instead inject.size', () => {
|
2021-12-12 17:54:21 +08:00
|
|
|
const fallbackSize = 'small'
|
2021-09-13 23:14:48 +08:00
|
|
|
const wrapper = mountComponent(() => {
|
2021-11-23 00:10:54 +08:00
|
|
|
provide(buttonGroupContextKey, {
|
2021-09-13 23:14:48 +08:00
|
|
|
size: fallbackSize,
|
2021-11-23 00:10:54 +08:00
|
|
|
} as ButtonGroupContext)
|
2021-09-13 23:14:48 +08:00
|
|
|
|
2022-03-06 22:20:56 +08:00
|
|
|
provide(formItemContextKey, {
|
2021-09-13 23:14:48 +08:00
|
|
|
size: 'large',
|
2022-03-06 22:20:56 +08:00
|
|
|
} as FormItemContext)
|
2021-09-13 23:14:48 +08:00
|
|
|
})
|
|
|
|
|
2022-02-12 18:37:16 +08:00
|
|
|
expect(wrapper.find(`.el-button--${fallbackSize}`).exists()).toBe(true)
|
2021-09-13 23:14:48 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should return formItem.size instead form.size', () => {
|
2021-12-12 17:54:21 +08:00
|
|
|
const itemSize = 'small'
|
2021-09-13 23:14:48 +08:00
|
|
|
const wrapper = mountComponent(() => {
|
2022-03-06 22:20:56 +08:00
|
|
|
provide(formItemContextKey, {
|
2021-09-13 23:14:48 +08:00
|
|
|
size: itemSize,
|
2022-03-06 22:20:56 +08:00
|
|
|
} as FormItemContext)
|
2021-09-13 23:14:48 +08:00
|
|
|
|
2022-03-06 22:20:56 +08:00
|
|
|
provide(formContextKey, {
|
2021-09-13 23:14:48 +08:00
|
|
|
size: 'large',
|
2022-03-06 22:20:56 +08:00
|
|
|
} as FormContext)
|
2021-09-13 23:14:48 +08:00
|
|
|
})
|
|
|
|
|
2022-02-12 18:37:16 +08:00
|
|
|
expect(wrapper.find(`.el-button--${itemSize}`).exists()).toBe(true)
|
2021-09-13 23:14:48 +08:00
|
|
|
})
|
|
|
|
})
|