From bb0b67046eabc53ecfafa861113a88a8bbe5ef3d Mon Sep 17 00:00:00 2001 From: liuyunhe Date: Fri, 15 Nov 2024 14:02:57 +0800 Subject: [PATCH 1/3] =?UTF-8?q?docs:=20=E6=B7=BB=E5=8A=A0=20MIT=20?= =?UTF-8?q?=E5=BC=80=E6=BA=90=E8=AE=B8=E5=8F=AF=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在项目根目录下创建 LICENSE 文件 - 写入 MIT License 内容,包括版权声明、许可权限、免责条款等 --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d741e33 --- /dev/null +++ b/LICENSE @@ -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. From e704e9f20feec99ed6d9835b06d041ba30ac141f Mon Sep 17 00:00:00 2001 From: liuyunhe Date: Fri, 15 Nov 2024 14:12:32 +0800 Subject: [PATCH 2/3] =?UTF-8?q?refactor(hooks):=20=E9=87=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8D=20useLocal=20=E4=B8=BA=20useLocale?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 src/hooks/useLocal.ts 重命名为 src/hooks/useLocale.ts - 此修改统一了国际化相关的命名,避免了混淆 --- src/hooks/index.ts | 2 +- src/hooks/{useLocal.ts => useLocale.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/hooks/{useLocal.ts => useLocale.ts} (100%) diff --git a/src/hooks/index.ts b/src/hooks/index.ts index ec2b0e1..1c4d3d9 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -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' diff --git a/src/hooks/useLocal.ts b/src/hooks/useLocale.ts similarity index 100% rename from src/hooks/useLocal.ts rename to src/hooks/useLocale.ts From d903fe82cfda0b7a0202d40da2b61cd1e1327d87 Mon Sep 17 00:00:00 2001 From: liuyunhe Date: Fri, 15 Nov 2024 14:12:52 +0800 Subject: [PATCH 3/3] =?UTF-8?q?test(hooks):=20=E6=B7=BB=E5=8A=A0=20useLoca?= =?UTF-8?q?le=20=E9=92=A9=E5=AD=90=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建 useLocale.test.tsx 文件,实现对 useLocale 钩子的全面测试 - 测试内容包括: - 正确提供 locale 配置 - 反应式更新文本 - 未定义键时返回键名 - 覆盖全局配置的功能 - 使用 Vitest 和 @vue/test-utils 进行测试 --- src/hooks/__tests__/useLocale.test.tsx | 97 ++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/hooks/__tests__/useLocale.test.tsx diff --git a/src/hooks/__tests__/useLocale.test.tsx b/src/hooks/__tests__/useLocale.test.tsx new file mode 100644 index 0000000..6afe25a --- /dev/null +++ b/src/hooks/__tests__/useLocale.test.tsx @@ -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 () => ( +
{t('el.popconfirm.confirmButtonText')}
+ ) + }, +}) + +describe('use-locale', () => { + let wrapper: VueWrapper + + beforeEach(() => { + wrapper = mount( + defineComponent({ + props: { + locale: { + type: Object as PropType, + default: Chinese, + }, + }, + setup(props) { + provideGlobalConfig(computed(() => ({ locale: props.locale }))) + return () => + }, + }) + ) + }) + + 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: '
', + }), + { + global: { + provide: { + locale: Chinese, + }, + }, + } + ) + + expect(wrapper.vm.locale).toBe(override.value) + }) + }) +})