2022-05-30 10:36:10 +08:00
|
|
|
import { defineComponent, nextTick, reactive, ref } from 'vue'
|
2021-07-25 15:26:00 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2022-04-19 12:46:57 +08:00
|
|
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
2022-03-25 15:35:56 +08:00
|
|
|
import { useGlobalConfig, useLocale } from '@element-plus/hooks'
|
2021-07-25 15:26:00 +08:00
|
|
|
import Chinese from '@element-plus/locale/lang/zh-cn'
|
|
|
|
import English from '@element-plus/locale/lang/en'
|
2022-01-08 20:03:13 +08:00
|
|
|
import { ElButton, ElMessage } from '@element-plus/components'
|
2022-01-10 09:45:01 +08:00
|
|
|
import { rAF } from '@element-plus/test-utils/tick'
|
2021-12-30 19:31:35 +08:00
|
|
|
import ConfigProvider from '../src/config-provider'
|
2022-03-24 13:48:58 +08:00
|
|
|
|
2022-03-24 15:19:32 +08:00
|
|
|
import type { PropType } from 'vue'
|
2022-03-24 13:48:58 +08:00
|
|
|
import type { VueWrapper } from '@vue/test-utils'
|
2021-07-25 15:26:00 +08:00
|
|
|
import type { Language } from '@element-plus/locale'
|
2022-03-24 15:19:32 +08:00
|
|
|
import type { ConfigProviderProps } from '../src/config-provider'
|
2021-07-25 15:26:00 +08:00
|
|
|
|
2022-05-30 10:36:10 +08:00
|
|
|
const TestComp = defineComponent({
|
2021-07-25 15:26:00 +08:00
|
|
|
setup() {
|
2022-01-08 19:36:13 +08:00
|
|
|
const { t } = useLocale()
|
2022-05-30 10:36:10 +08:00
|
|
|
return () => <div>{t('el.popconfirm.confirmButtonText')}</div>
|
2021-07-25 15:26:00 +08:00
|
|
|
},
|
2022-05-30 10:36:10 +08:00
|
|
|
})
|
2021-07-25 15:26:00 +08:00
|
|
|
|
|
|
|
describe('config-provider', () => {
|
|
|
|
describe('locale-provider', () => {
|
2022-03-24 13:48:58 +08:00
|
|
|
let wrapper: VueWrapper<any>
|
2021-07-25 15:26:00 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-05-30 10:36:10 +08:00
|
|
|
const currentLocale = ref<Language>(English)
|
|
|
|
const oppositeLocale = ref<Language>(Chinese)
|
|
|
|
const toEn = () => {
|
|
|
|
currentLocale.value = English
|
|
|
|
oppositeLocale.value = Chinese
|
|
|
|
}
|
|
|
|
const toZh = () => {
|
|
|
|
currentLocale.value = Chinese
|
|
|
|
oppositeLocale.value = English
|
|
|
|
}
|
2021-07-25 15:26:00 +08:00
|
|
|
|
2022-05-30 10:36:10 +08:00
|
|
|
wrapper = mount(() => (
|
|
|
|
<>
|
|
|
|
<ConfigProvider locale={currentLocale.value}>
|
|
|
|
<TestComp class="current-locale" />
|
|
|
|
<ConfigProvider locale={oppositeLocale.value}>
|
|
|
|
<TestComp class="opposite-locale" />
|
|
|
|
</ConfigProvider>
|
|
|
|
</ConfigProvider>
|
|
|
|
|
|
|
|
<button onClick={toEn} class="to-en">
|
|
|
|
toEn
|
|
|
|
</button>
|
|
|
|
<button onClick={toZh} class="to-zh">
|
|
|
|
toZh
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
))
|
2021-07-25 15:26:00 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
wrapper.unmount()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should provide locale properly', async () => {
|
|
|
|
expect(wrapper.find('.current-locale').text()).toBe(
|
2021-09-04 19:29:28 +08:00
|
|
|
English.el.popconfirm.confirmButtonText
|
2021-07-25 15:26:00 +08:00
|
|
|
)
|
|
|
|
expect(wrapper.find('.opposite-locale').text()).toBe(
|
2021-09-04 19:29:28 +08:00
|
|
|
Chinese.el.popconfirm.confirmButtonText
|
2021-07-25 15:26:00 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should reactively update the text on page', async () => {
|
|
|
|
expect(wrapper.find('.current-locale').text()).toBe(
|
2021-09-04 19:29:28 +08:00
|
|
|
English.el.popconfirm.confirmButtonText
|
2021-07-25 15:26:00 +08:00
|
|
|
)
|
|
|
|
expect(wrapper.find('.opposite-locale').text()).toBe(
|
2021-09-04 19:29:28 +08:00
|
|
|
Chinese.el.popconfirm.confirmButtonText
|
2021-07-25 15:26:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
await wrapper.find('.to-zh').trigger('click')
|
|
|
|
|
|
|
|
expect(wrapper.find('.current-locale').text()).toBe(
|
2021-09-04 19:29:28 +08:00
|
|
|
Chinese.el.popconfirm.confirmButtonText
|
2021-07-25 15:26:00 +08:00
|
|
|
)
|
|
|
|
expect(wrapper.find('.opposite-locale').text()).toBe(
|
2021-09-04 19:29:28 +08:00
|
|
|
English.el.popconfirm.confirmButtonText
|
2021-07-25 15:26:00 +08:00
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2021-11-05 18:10:07 +08:00
|
|
|
|
|
|
|
describe('button-config', () => {
|
|
|
|
it('autoInsertSpace', async () => {
|
2022-05-30 10:36:10 +08:00
|
|
|
const config = reactive({
|
|
|
|
autoInsertSpace: true,
|
2021-11-05 18:10:07 +08:00
|
|
|
})
|
2022-05-30 10:36:10 +08:00
|
|
|
|
|
|
|
const wrapper = mount(() => (
|
|
|
|
<>
|
|
|
|
<ConfigProvider button={config}>
|
|
|
|
<ElButton>中文</ElButton>
|
|
|
|
</ConfigProvider>
|
|
|
|
<button
|
|
|
|
class="toggle"
|
|
|
|
onClick={() => (config.autoInsertSpace = !config.autoInsertSpace)}
|
|
|
|
>
|
|
|
|
toggle
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
))
|
|
|
|
|
2021-11-05 18:10:07 +08:00
|
|
|
await nextTick()
|
|
|
|
expect(
|
|
|
|
wrapper.find('.el-button .el-button__text--expand').exists()
|
|
|
|
).toBeTruthy()
|
|
|
|
await wrapper.find('.toggle').trigger('click')
|
|
|
|
expect(
|
|
|
|
wrapper.find('.el-button .el-button__text--expand').exists()
|
|
|
|
).toBeFalsy()
|
|
|
|
})
|
|
|
|
})
|
2022-01-08 20:03:13 +08:00
|
|
|
|
2022-01-15 18:12:48 +08:00
|
|
|
describe('namespace-config', () => {
|
|
|
|
it('reactive namespace', async () => {
|
2022-05-30 10:36:10 +08:00
|
|
|
const namespace = ref()
|
|
|
|
|
|
|
|
const wrapper = mount(() => (
|
|
|
|
<ConfigProvider namespace={namespace.value}>
|
|
|
|
<ElButton>test str</ElButton>
|
|
|
|
</ConfigProvider>
|
|
|
|
))
|
|
|
|
|
2022-01-15 18:12:48 +08:00
|
|
|
await nextTick()
|
2022-03-24 19:42:09 +08:00
|
|
|
expect(wrapper.find('button').classes().join('')).toBe('el-button')
|
2022-05-30 10:36:10 +08:00
|
|
|
namespace.value = 'ep'
|
2022-01-15 18:12:48 +08:00
|
|
|
await nextTick()
|
2022-03-24 19:42:09 +08:00
|
|
|
expect(wrapper.find('button').classes().join('')).toBe('ep-button')
|
2022-01-15 18:12:48 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-01-08 20:03:13 +08:00
|
|
|
describe('message-config', () => {
|
2022-04-19 12:46:57 +08:00
|
|
|
afterEach(() => {
|
|
|
|
ElMessage.closeAll()
|
|
|
|
})
|
|
|
|
|
2022-01-08 20:03:13 +08:00
|
|
|
it('limit the number of messages displayed at the same time', async () => {
|
2022-05-30 10:36:10 +08:00
|
|
|
const config = reactive({
|
|
|
|
max: 3,
|
2022-01-08 20:03:13 +08:00
|
|
|
})
|
2022-05-30 10:36:10 +08:00
|
|
|
const open = () => {
|
|
|
|
ElMessage('this is a message.')
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapper = mount(() => (
|
|
|
|
<ConfigProvider message={config}>
|
|
|
|
<ElButton onClick={open}>open</ElButton>
|
|
|
|
</ConfigProvider>
|
|
|
|
))
|
|
|
|
|
2022-01-08 20:03:13 +08:00
|
|
|
await nextTick()
|
|
|
|
wrapper.find('.el-button').trigger('click')
|
|
|
|
wrapper.find('.el-button').trigger('click')
|
|
|
|
wrapper.find('.el-button').trigger('click')
|
|
|
|
wrapper.find('.el-button').trigger('click')
|
|
|
|
await nextTick()
|
|
|
|
expect(document.querySelectorAll('.el-message').length).toBe(3)
|
|
|
|
|
2022-05-30 10:36:10 +08:00
|
|
|
config.max = 10
|
2022-01-08 20:03:13 +08:00
|
|
|
await nextTick()
|
|
|
|
wrapper.find('.el-button').trigger('click')
|
|
|
|
wrapper.find('.el-button').trigger('click')
|
|
|
|
wrapper.find('.el-button').trigger('click')
|
|
|
|
wrapper.find('.el-button').trigger('click')
|
|
|
|
await nextTick()
|
|
|
|
expect(document.querySelectorAll('.el-message').length).toBe(7)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('multiple config-provider config override', async () => {
|
2022-05-30 10:36:10 +08:00
|
|
|
const config = reactive({
|
|
|
|
max: 3,
|
|
|
|
})
|
|
|
|
const overrideConfig = reactive({
|
|
|
|
max: 1,
|
2022-01-08 20:03:13 +08:00
|
|
|
})
|
2022-05-30 10:36:10 +08:00
|
|
|
const open = () => {
|
|
|
|
ElMessage('this is a message.')
|
|
|
|
}
|
|
|
|
|
|
|
|
const wrapper = mount(() => (
|
|
|
|
<ConfigProvider message={config}>
|
|
|
|
<ConfigProvider message={overrideConfig}>
|
|
|
|
<ElButton onClick={open}>open</ElButton>
|
|
|
|
</ConfigProvider>
|
|
|
|
</ConfigProvider>
|
|
|
|
))
|
|
|
|
|
2022-01-10 09:45:01 +08:00
|
|
|
await rAF()
|
2022-04-19 12:46:57 +08:00
|
|
|
await wrapper.find('.el-button').trigger('click')
|
|
|
|
await wrapper.find('.el-button').trigger('click')
|
|
|
|
await wrapper.find('.el-button').trigger('click')
|
2022-01-08 20:03:13 +08:00
|
|
|
await nextTick()
|
|
|
|
expect(document.querySelectorAll('.el-message').length).toBe(1)
|
|
|
|
})
|
|
|
|
})
|
2022-03-24 13:48:58 +08:00
|
|
|
|
2022-03-24 15:19:32 +08:00
|
|
|
describe('feature checking', () => {
|
|
|
|
const TestComponent = defineComponent({
|
|
|
|
props: {
|
|
|
|
configKey: {
|
|
|
|
type: String as PropType<keyof ConfigProviderProps>,
|
|
|
|
required: true,
|
2022-03-24 13:48:58 +08:00
|
|
|
},
|
2022-03-24 15:19:32 +08:00
|
|
|
},
|
|
|
|
setup(props) {
|
|
|
|
const features = useGlobalConfig(props.configKey)
|
|
|
|
return {
|
|
|
|
[props.configKey]: features,
|
|
|
|
}
|
|
|
|
},
|
2022-05-30 10:36:10 +08:00
|
|
|
render: () => <div />,
|
2022-03-24 15:19:32 +08:00
|
|
|
})
|
2022-03-24 13:48:58 +08:00
|
|
|
|
2022-04-19 12:46:57 +08:00
|
|
|
it.each([
|
|
|
|
{ feature: 'a11y', config: false },
|
|
|
|
{ feature: 'keyboardNavigation', config: false },
|
|
|
|
{ feature: 'experimentalFeatures', config: { someFeature: true } },
|
|
|
|
])(
|
2022-03-24 15:19:32 +08:00
|
|
|
'should inject config $feature to $config correctly',
|
|
|
|
({ feature, config }: { feature: string; config: any }) => {
|
2022-05-30 10:36:10 +08:00
|
|
|
const props: Record<string, any> = {}
|
|
|
|
props[feature] = config
|
|
|
|
|
|
|
|
const wrapper = mount(() => (
|
|
|
|
<ConfigProvider {...props}>
|
|
|
|
<TestComponent configKey={feature as keyof ConfigProviderProps} />
|
|
|
|
</ConfigProvider>
|
|
|
|
))
|
2022-03-24 13:48:58 +08:00
|
|
|
|
2022-03-24 15:19:32 +08:00
|
|
|
expect(wrapper.findComponent(TestComponent).vm[feature]).toEqual(config)
|
|
|
|
}
|
|
|
|
)
|
2022-03-24 13:48:58 +08:00
|
|
|
})
|
2021-07-25 15:26:00 +08:00
|
|
|
})
|