2026-09-18 09:52:22 +08:00
|
|
|
import { mount, type VueWrapper } from '@vue/test-utils';
|
|
|
|
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
2026-09-18 09:12:45 +08:00
|
|
|
import { nextTick } from 'vue';
|
|
|
|
|
import App from '../App.vue';
|
2026-09-18 09:52:22 +08:00
|
|
|
import { LOCALE_STORAGE_KEY, createViewerI18n } from '../i18n';
|
2026-09-18 09:12:45 +08:00
|
|
|
import { router } from '../router';
|
|
|
|
|
|
2026-09-18 09:52:22 +08:00
|
|
|
// i18n 切换机制(顶栏 chrome 层):活动页级的「切换语言 → 重取内容」由
|
|
|
|
|
// activity.spec.ts 覆盖,此处聚焦首页 chrome(无需 api mock)。
|
|
|
|
|
// 逐挂载独立 i18n + 逐用例卸载(背景见 activity.spec.ts 头注)。
|
|
|
|
|
let i18n: ReturnType<typeof createViewerI18n>;
|
|
|
|
|
let wrapper: VueWrapper;
|
|
|
|
|
|
2026-09-18 09:12:45 +08:00
|
|
|
describe('viewer i18n', () => {
|
2026-09-18 09:52:22 +08:00
|
|
|
beforeEach(async () => {
|
2026-09-18 09:12:45 +08:00
|
|
|
localStorage.clear();
|
2026-09-18 09:52:22 +08:00
|
|
|
i18n = createViewerI18n('zh-CN');
|
|
|
|
|
await router.push('/');
|
|
|
|
|
await router.isReady();
|
|
|
|
|
wrapper = mount(App, { global: { plugins: [router, i18n] } });
|
2026-09-18 09:12:45 +08:00
|
|
|
});
|
|
|
|
|
|
2026-09-18 09:52:22 +08:00
|
|
|
afterEach(() => {
|
|
|
|
|
wrapper.unmount();
|
|
|
|
|
});
|
2026-09-18 09:12:45 +08:00
|
|
|
|
2026-09-18 09:52:22 +08:00
|
|
|
it('switching locale re-renders messages and persists the choice', async () => {
|
|
|
|
|
expect(wrapper.text()).toContain('打开相册链接即可观看');
|
2026-09-18 09:12:45 +08:00
|
|
|
expect(document.title).toBe('现场相册');
|
|
|
|
|
|
2026-09-21 21:22:36 +08:00
|
|
|
// UI-02 V01 紧凑语言控件:展开菜单 → 选择 English。
|
|
|
|
|
await wrapper.find('.locale-trigger').trigger('click');
|
|
|
|
|
await nextTick();
|
|
|
|
|
const english = wrapper.findAll('.locale-option').find((btn) => btn.text() === 'English');
|
|
|
|
|
expect(english).toBeDefined();
|
|
|
|
|
await english!.trigger('click');
|
2026-09-18 09:12:45 +08:00
|
|
|
await nextTick();
|
2026-09-18 09:52:22 +08:00
|
|
|
expect(wrapper.text()).toContain('Open an album link to start viewing');
|
2026-09-18 09:12:45 +08:00
|
|
|
expect(document.title).toBe('Live Gallery');
|
|
|
|
|
expect(document.documentElement.getAttribute('lang')).toBe('en');
|
|
|
|
|
expect(localStorage.getItem(LOCALE_STORAGE_KEY)).toBe('en');
|
|
|
|
|
|
2026-09-21 21:22:36 +08:00
|
|
|
// 切回 zh-CN 同样生效(菜单内当前语言 English 为勾选态)。
|
|
|
|
|
await wrapper.find('.locale-trigger').trigger('click');
|
|
|
|
|
await nextTick();
|
|
|
|
|
const options = wrapper.findAll('.locale-option');
|
|
|
|
|
const englishNow = options.find((btn) => btn.text() === 'English');
|
|
|
|
|
expect(englishNow!.attributes('aria-checked')).toBe('true');
|
|
|
|
|
const chinese = options.find((btn) => btn.text() === '中文');
|
|
|
|
|
expect(chinese!.attributes('aria-checked')).toBe('false');
|
|
|
|
|
await chinese!.trigger('click');
|
2026-09-18 09:12:45 +08:00
|
|
|
await nextTick();
|
2026-09-18 09:52:22 +08:00
|
|
|
expect(wrapper.text()).toContain('打开相册链接即可观看');
|
2026-09-18 09:12:45 +08:00
|
|
|
expect(localStorage.getItem(LOCALE_STORAGE_KEY)).toBe('zh-CN');
|
|
|
|
|
});
|
|
|
|
|
});
|