element-plus/packages/hooks/__tests__/use-locale.vitest.tsx
三咲智子 aaf90d99d0
test: switch to vitest (#5991)
* test: use vitest

* test: add script and ci

* chore: improve tsconfig

* refactor: use-form-item

* fix: remove unused

* chore: improve scripts

* test: improve mock

* refactor: change coverage
2022-02-21 14:28:22 +08:00

75 lines
2.0 KiB
TypeScript

import { nextTick, computed, defineComponent } from 'vue'
import { mount } from '@vue/test-utils'
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import Chinese from '@element-plus/locale/lang/zh-cn'
import English from '@element-plus/locale/lang/en'
import { useLocale, buildTranslator } from '../use-locale'
import { provideGlobalConfig } from '..'
import type { Language } from '@element-plus/locale'
import type { PropType, ComponentPublicInstance } from 'vue'
import type { VueWrapper } from '@vue/test-utils'
const TestComp = defineComponent({
setup() {
const { t } = useLocale()
return () => (
<div class="locale-manifest">{t('el.popconfirm.confirmButtonText')}</div>
)
},
})
describe('use-locale', () => {
let wrapper: VueWrapper<ComponentPublicInstance>
beforeEach(() => {
wrapper = mount(
{
props: {
locale: Object as PropType<Language>,
},
components: {
'el-test': TestComp,
},
setup(props, { slots }) {
provideGlobalConfig(computed(() => ({ locale: props.locale })))
return () => slots.default?.()
},
},
{
props: { locale: Chinese },
slots: { default: () => <TestComp /> },
}
)
})
afterEach(() => {
wrapper.unmount()
})
it('should provide locale correctly', async () => {
await nextTick()
expect(wrapper.find('.locale-manifest').text()).toBe(
Chinese.el.popconfirm.confirmButtonText
)
})
it('should update the text reactively', async () => {
await nextTick()
expect(wrapper.find('.locale-manifest').text()).toBe(
Chinese.el.popconfirm.confirmButtonText
)
await wrapper.setProps({
locale: English,
})
expect(wrapper.find('.locale-manifest').text()).toBe(
English.el.popconfirm.confirmButtonText
)
})
it('return key name if not defined', () => {
const t = buildTranslator(English)
expect(t('el.popconfirm.someThing')).toBe('el.popconfirm.someThing')
})
})