2021-09-13 23:14:48 +08:00
|
|
|
import { h, provide } from 'vue'
|
|
|
|
import { NOOP } from '@vue/shared'
|
|
|
|
import { mount } from '@vue/test-utils'
|
|
|
|
import { ElButton } from '@element-plus/components'
|
|
|
|
import {
|
|
|
|
elFormKey,
|
|
|
|
elFormItemKey,
|
2021-11-23 00:10:54 +08:00
|
|
|
buttonGroupContextKey,
|
2021-09-13 23:14:48 +08:00
|
|
|
} from '@element-plus/tokens'
|
|
|
|
|
|
|
|
import type {
|
|
|
|
ElFormContext,
|
|
|
|
ElFormItemContext,
|
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'
|
|
|
|
|
|
|
|
const Component = {
|
|
|
|
render() {
|
|
|
|
return h(ElButton, this.$attrs, {
|
|
|
|
default: () => [AXIOM],
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const mountComponent = (setup = NOOP, options = {}) => {
|
|
|
|
return mount(
|
|
|
|
{
|
|
|
|
...Component,
|
|
|
|
setup,
|
|
|
|
},
|
|
|
|
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(
|
|
|
|
() => {
|
|
|
|
provide(elFormItemKey, {
|
|
|
|
size: 'large',
|
|
|
|
} as ElFormItemContext)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
props: {
|
|
|
|
size: propSize,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
provide(elFormItemKey, {
|
|
|
|
size: 'large',
|
|
|
|
} as ElFormItemContext)
|
|
|
|
})
|
|
|
|
|
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(() => {
|
|
|
|
provide(elFormItemKey, {
|
|
|
|
size: itemSize,
|
2021-11-23 00:10:54 +08:00
|
|
|
} as ElFormItemContext)
|
2021-09-13 23:14:48 +08:00
|
|
|
|
|
|
|
provide(elFormKey, {
|
|
|
|
size: 'large',
|
|
|
|
} as ElFormContext)
|
|
|
|
})
|
|
|
|
|
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
|
|
|
})
|
|
|
|
})
|