Files
element-plus/packages/hooks/__tests__/use-locale.test.tsx
Noblet Ouways 2f17df1209 style(eslint-config): newline before import type (#21036)
* perf: change to import-x

* feat: add rules

* chore: fix rule

* chore: fix

* chore: fix

* chore: fix

* style: `pnpm lint:fix`

* Revert "style: `pnpm lint:fix`"

This reverts commit db0116a288.

* Revert "chore: fix"

This reverts commit 69c82a90c0.

* chore: fix

* style: `pnpm lint:fix`

* fix: lint

* chore: `pnpm format`
2025-06-16 15:37:12 +08:00

91 lines
2.3 KiB
TypeScript

import { computed, defineComponent, nextTick } from 'vue'
import { mount } from '@vue/test-utils'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import Chinese from '@element-plus/locale/lang/zh-cn'
import English from '@element-plus/locale/lang/en'
import { buildTranslator, useLocale } from '../use-locale'
import type { Language } from '@element-plus/locale'
import type { ComponentPublicInstance, PropType } from 'vue'
import type { VueWrapper } from '@vue/test-utils'
const TestComp = defineComponent({
props: {
locale: {
type: Object as PropType<Language>,
default: Chinese,
},
},
setup(props) {
const { t } = useLocale(computed(() => props.locale))
return () => (
<div class="locale-manifest">{t('el.popconfirm.confirmButtonText')}</div>
)
},
})
describe('use-locale', () => {
let wrapper: VueWrapper<ComponentPublicInstance>
beforeEach(() => {
wrapper = mount(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')
})
describe('overrides', () => {
it('should be override correctly', () => {
const override = computed(() => English)
const wrapper = mount(
defineComponent({
setup(_, { expose }) {
const { locale } = useLocale(override)
expose({
locale,
})
},
template: '<div></div>',
}),
{
global: {
provide: {
locale: Chinese,
},
},
}
)
expect(wrapper.vm.locale).toBe(override.value)
})
})
})