2022-01-08 19:36:13 +08:00
|
|
|
import { h, ref, reactive, nextTick } from 'vue'
|
2021-07-25 15:26:00 +08:00
|
|
|
import { mount } from '@vue/test-utils'
|
2022-01-08 19:36:13 +08:00
|
|
|
import { 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'
|
|
|
|
import { sleep } from '@element-plus/test-utils'
|
2021-12-30 19:31:35 +08:00
|
|
|
import ConfigProvider from '../src/config-provider'
|
2021-07-25 15:26:00 +08:00
|
|
|
import type { Language } from '@element-plus/locale'
|
|
|
|
|
|
|
|
const TestComp = {
|
|
|
|
setup() {
|
2022-01-08 19:36:13 +08:00
|
|
|
const { t } = useLocale()
|
2021-07-25 15:26:00 +08:00
|
|
|
return () => {
|
2021-07-26 00:24:30 +08:00
|
|
|
return h('div', t('el.popconfirm.confirmButtonText'))
|
2021-07-25 15:26:00 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('config-provider', () => {
|
|
|
|
describe('locale-provider', () => {
|
|
|
|
let wrapper
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
wrapper = mount({
|
|
|
|
components: {
|
|
|
|
'el-test': TestComp,
|
|
|
|
[ConfigProvider.name]: ConfigProvider,
|
|
|
|
},
|
|
|
|
setup() {
|
|
|
|
const currentLocale = ref<Language>(English)
|
|
|
|
const oppositeLocale = ref<Language>(Chinese)
|
|
|
|
return {
|
|
|
|
currentLocale,
|
|
|
|
oppositeLocale,
|
|
|
|
toEn() {
|
|
|
|
currentLocale.value = English
|
|
|
|
oppositeLocale.value = Chinese
|
|
|
|
},
|
|
|
|
toZh() {
|
|
|
|
currentLocale.value = Chinese
|
|
|
|
oppositeLocale.value = English
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
template: `
|
|
|
|
<el-config-provider :locale="currentLocale">
|
|
|
|
<el-test class="current-locale" />
|
|
|
|
<el-config-provider :locale="oppositeLocale">
|
|
|
|
<el-test class="opposite-locale" />
|
|
|
|
</el-config-provider>
|
|
|
|
</el-config-provider>
|
|
|
|
|
|
|
|
<button @click="toEn" class="to-en">toEn</button>
|
|
|
|
<button @click="toZh" class="to-zh">toZh</button>
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
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 () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
components: {
|
|
|
|
[ConfigProvider.name]: ConfigProvider,
|
|
|
|
ElButton,
|
|
|
|
},
|
|
|
|
setup() {
|
|
|
|
const config = reactive({
|
|
|
|
autoInsertSpace: true,
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
config,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
template: `
|
|
|
|
<el-config-provider :button="config">
|
|
|
|
<el-button>中文</el-button>
|
|
|
|
</el-config-provider>
|
|
|
|
<button class="toggle" @click="config.autoInsertSpace = !config.autoInsertSpace">toggle</button>
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
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
|
|
|
|
|
|
|
describe('message-config', () => {
|
|
|
|
it('limit the number of messages displayed at the same time', async () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
components: {
|
|
|
|
[ConfigProvider.name]: ConfigProvider,
|
|
|
|
ElButton,
|
|
|
|
},
|
|
|
|
setup() {
|
|
|
|
const config = reactive({
|
|
|
|
max: 3,
|
|
|
|
})
|
|
|
|
const open = () => {
|
|
|
|
ElMessage('this is a message.')
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
config,
|
|
|
|
open,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
template: `
|
|
|
|
<el-config-provider :message="config">
|
|
|
|
<el-button @click="open">open</el-button>
|
|
|
|
</el-config-provider>
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
|
|
|
|
wrapper.vm.config.max = 10
|
|
|
|
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 () => {
|
|
|
|
const wrapper = mount({
|
|
|
|
components: {
|
|
|
|
[ConfigProvider.name]: ConfigProvider,
|
|
|
|
ElButton,
|
|
|
|
},
|
|
|
|
setup() {
|
|
|
|
const config = reactive({
|
|
|
|
max: 3,
|
|
|
|
})
|
|
|
|
const overrideConfig = reactive({
|
|
|
|
max: 1,
|
|
|
|
})
|
|
|
|
const open = () => {
|
|
|
|
ElMessage('this is a message.')
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
config,
|
|
|
|
overrideConfig,
|
|
|
|
open,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
template: `
|
|
|
|
<el-config-provider :message="config">
|
|
|
|
<el-config-provider :message="overrideConfig">
|
|
|
|
<el-button @click="open">open</el-button>
|
|
|
|
</el-config-provider>
|
|
|
|
</el-config-provider>
|
|
|
|
`,
|
|
|
|
})
|
|
|
|
ElMessage.closeAll()
|
|
|
|
await sleep(40)
|
|
|
|
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(1)
|
|
|
|
})
|
|
|
|
})
|
2021-07-25 15:26:00 +08:00
|
|
|
})
|