Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyunhe committed Nov 15, 2024
2 parents 7d4b969 + d903fe8 commit 795ecba
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 1 deletion.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024-PRESENT SHARP UI

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
97 changes: 97 additions & 0 deletions src/hooks/__tests__/useLocale.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { computed, defineComponent, nextTick } from 'vue'
import { mount } from '@vue/test-utils'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import Chinese from '@/locale/lang/zh-cn'
import English from '@/locale/lang/en'
import { provideGlobalConfig } from '@/components/configProvider'
import { buildTranslator, useLocale } from '../useLocale'
import type { Language } from '@/locale'
import type { ComponentPublicInstance, PropType } 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(
defineComponent({
props: {
locale: {
type: Object as PropType<Language>,
default: Chinese,
},
},
setup(props) {
provideGlobalConfig(computed(() => ({ locale: props.locale })))
return () => <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)
})
})
})
2 changes: 1 addition & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export * from './useId'
export * from './useLockscreen'
export * from './useSize'
export * from './useZIndexPro'
export * from './useLocal'
export * from './useLocale'
export * from './useEmptyValues'
export * from './useProp'
export * from './useTransitionFallthrough'
Expand Down
File renamed without changes.

0 comments on commit 795ecba

Please sign in to comment.