20260917212907
This commit is contained in:
1 parent
6fa3d8535e
commit
5cbf745bbf
115 files changed
+4104
-85
No files matched your search
@@ -0,0 +1,76 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { shallowMount } from '@vue/test-utils'
|
||||
import { createTestPinia, createTestRouter } from '../helpers/app'
|
||||
import AppTopbar from '@/layouts/components/AppTopbar.vue'
|
||||
import { useI18nStore } from '@/stores/i18n'
|
||||
|
||||
vi.mock('@/services/api', () => ({
|
||||
loadDataApi: vi.fn().mockResolvedValue({ data: [] }),
|
||||
}))
|
||||
|
||||
async function mountTopbar() {
|
||||
const pinia = createTestPinia()
|
||||
const router = createTestRouter('/dashboard')
|
||||
await router.isReady()
|
||||
const wrapper = shallowMount(AppTopbar, {
|
||||
global: { plugins: [pinia, router] },
|
||||
})
|
||||
return { wrapper, i18n: useI18nStore() }
|
||||
}
|
||||
|
||||
/** 顶栏语言下拉:组件库 Dropdown 被 shallow 打桩,桩名取文件名推断的 'dropdown' */
|
||||
function languageDropdown(wrapper) {
|
||||
return wrapper
|
||||
.findAllComponents({ name: 'dropdown' })
|
||||
.find((item) => Array.isArray(item.props('items')) && item.props('items').some((i) => i.shortcut))
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
localStorage.clear()
|
||||
sessionStorage.clear()
|
||||
})
|
||||
|
||||
describe('顶栏语言切换', () => {
|
||||
it('语种来自 s_i18n_type 语言清单,当前语言带勾选标记', async () => {
|
||||
const { wrapper, i18n } = await mountTopbar()
|
||||
i18n.languages = [
|
||||
{ b_id: 'zh-CN', b_name: '简体中文', b_canuse: 1, b_default: 1, b_xh: 10 },
|
||||
{ b_id: 'en-US', b_name: 'English', b_canuse: 1, b_default: 0, b_xh: 20 },
|
||||
]
|
||||
i18n.setLocale('zh-CN')
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
const items = languageDropdown(wrapper).props('items')
|
||||
expect(items.map((item) => item.key)).toEqual(['zh-CN', 'en-US'])
|
||||
expect(items.map((item) => item.label)).toEqual(['简体中文', 'English'])
|
||||
expect(items[0].shortcut).toBe('✓')
|
||||
expect(items[1].shortcut).toBe('')
|
||||
})
|
||||
|
||||
it('点击语种即切换(不再硬编码 zh-CN / en-US 互切)', async () => {
|
||||
const { wrapper, i18n } = await mountTopbar()
|
||||
i18n.languages = [
|
||||
{ b_id: 'zh-CN', b_name: '简体中文', b_canuse: 1, b_default: 1, b_xh: 10 },
|
||||
{ b_id: 'en-US', b_name: 'English', b_canuse: 1, b_default: 0, b_xh: 20 },
|
||||
]
|
||||
i18n.setLocale('zh-CN')
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
languageDropdown(wrapper).vm.$emit('click', { key: 'en-US' })
|
||||
expect(i18n.locale).toBe('en-US')
|
||||
|
||||
// 重复点当前语种不产生变化(无副作用)
|
||||
languageDropdown(wrapper).vm.$emit('click', { key: 'en-US' })
|
||||
expect(i18n.locale).toBe('en-US')
|
||||
})
|
||||
|
||||
it('语言清单未加载时退化成只显示当前语种', async () => {
|
||||
const { wrapper, i18n } = await mountTopbar()
|
||||
i18n.languages = []
|
||||
i18n.setLocale('zh-CN')
|
||||
await wrapper.vm.$nextTick()
|
||||
|
||||
const items = languageDropdown(wrapper).props('items')
|
||||
expect(items).toEqual([{ key: 'zh-CN', label: 'zh-CN', shortcut: '✓' }])
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user