import { mockI18nSuccess, mockAllRequiredAPIs } from '../../support/mock/handlers/i18n'; import LoginPage from '../../support/pageObjects/LoginPage'; describe('多语言资源语言切换测试', () => { const loginPage = new LoginPage(); beforeEach(() => { cy.clearAllSessionStorage(); cy.clearAllLocalStorage(); // Mock所有必要的API,避免影响页面加载 mockAllRequiredAPIs(); }); it('从中文切换到英文', () => { // 首先加载中文 mockI18nSuccess('zh'); cy.window().then((win) => { Object.defineProperty(win.navigator, 'language', { value: 'zh-CN', writable: false }); }); loginPage.visit(); cy.wait('@getI18nZHSuccess'); // 验证中文内容显示 cy.get('body').should('contain', '患者管理'); // 验证Redux状态为中文 cy.window().its('store').invoke('getState').then((state) => { expect(state.i18n.currentLocale).to.equal('zh'); expect(state.i18n.messages).to.have.property('patient', '患者管理'); }); // 模拟语言切换 - 重新访问页面并设置英文语言 mockI18nSuccess('en'); cy.window().then((win) => { Object.defineProperty(win.navigator, 'language', { value: 'en-US', writable: false }); }); loginPage.visit(); cy.wait('@getI18nENSuccess'); // 验证英文内容显示 cy.get('body').should('contain', 'Patient Management'); // 验证Redux状态为英文 cy.window().its('store').invoke('getState').then((state) => { expect(state.i18n.currentLocale).to.equal('en'); expect(state.i18n.messages).to.have.property('patient', 'Patient Management'); }); }); it('从英文切换到中文', () => { // 首先加载英文 mockI18nSuccess('en'); cy.window().then((win) => { Object.defineProperty(win.navigator, 'language', { value: 'en-US', writable: false }); }); loginPage.visit(); cy.wait('@getI18nENSuccess'); // 验证英文内容显示 cy.get('body').should('contain', 'Patient Management'); // 模拟语言切换到中文 mockI18nSuccess('zh'); cy.window().then((win) => { Object.defineProperty(win.navigator, 'language', { value: 'zh-CN', writable: false }); }); loginPage.visit(); cy.wait('@getI18nZHSuccess'); // 验证中文内容显示 cy.get('body').should('contain', '患者管理'); // 验证Redux状态正确更新 cy.window().its('store').invoke('getState').then((state) => { expect(state.i18n.currentLocale).to.equal('zh'); expect(state.i18n.messages).to.have.property('patient', '患者管理'); }); }); it('验证不同地区变体的语言检测', () => { const testCases = [ // 中文变体都应该加载中文资源 { browserLang: 'zh', expectedLocale: 'zh', expectedText: '患者管理' }, { browserLang: 'zh-CN', expectedLocale: 'zh', expectedText: '患者管理' }, { browserLang: 'zh-TW', expectedLocale: 'zh', expectedText: '患者管理' }, { browserLang: 'zh-HK', expectedLocale: 'zh', expectedText: '患者管理' }, { browserLang: 'zh-SG', expectedLocale: 'zh', expectedText: '患者管理' }, // 英文变体都应该加载英文资源 { browserLang: 'en', expectedLocale: 'en', expectedText: 'Patient Management' }, { browserLang: 'en-US', expectedLocale: 'en', expectedText: 'Patient Management' }, { browserLang: 'en-GB', expectedLocale: 'en', expectedText: 'Patient Management' }, { browserLang: 'en-AU', expectedLocale: 'en', expectedText: 'Patient Management' }, { browserLang: 'en-CA', expectedLocale: 'en', expectedText: 'Patient Management' } ]; testCases.forEach(({ browserLang, expectedLocale, expectedText }, index) => { mockI18nSuccess(expectedLocale as 'zh' | 'en'); cy.window().then((win) => { Object.defineProperty(win.navigator, 'language', { value: browserLang, writable: false }); }); loginPage.visit(); cy.wait(`@getI18n${expectedLocale.toUpperCase()}Success`); // 验证正确的语言内容显示 cy.get('body').should('contain', expectedText); // 验证Redux状态 cy.window().its('store').invoke('getState').then((state) => { expect(state.i18n.currentLocale).to.equal(expectedLocale); }); // 清理,准备下一个测试 if (index < testCases.length - 1) { cy.clearAllSessionStorage(); cy.clearAllLocalStorage(); } }); }); it('验证语言切换时的加载状态', () => { // 首先加载中文 mockI18nSuccess('zh'); cy.window().then((win) => { Object.defineProperty(win.navigator, 'language', { value: 'zh-CN', writable: false }); }); loginPage.visit(); cy.wait('@getI18nZHSuccess'); cy.get('body').should('contain', '患者管理'); // 切换到英文,验证加载过程 mockI18nSuccess('en'); cy.window().then((win) => { Object.defineProperty(win.navigator, 'language', { value: 'en-US', writable: false }); }); loginPage.visit(); // 验证加载状态显示 cy.contains('加载多语言资源中...').should('be.visible'); // 等待加载完成 cy.wait('@getI18nENSuccess'); // 验证加载状态消失,新语言内容显示 cy.contains('加载多语言资源中...').should('not.exist'); cy.get('body').should('contain', 'Patient Management'); }); it('验证语言切换时Redux状态的完整性', () => { // 加载中文 mockI18nSuccess('zh'); cy.window().then((win) => { Object.defineProperty(win.navigator, 'language', { value: 'zh-CN', writable: false }); }); loginPage.visit(); cy.wait('@getI18nZHSuccess'); // 验证中文状态 cy.window().its('store').invoke('getState').then((state) => { expect(state.i18n.currentLocale).to.equal('zh'); expect(state.i18n.loading).to.be.false; expect(state.i18n.error).to.be.null; expect(state.i18n.messages).to.have.property('register', '注册'); expect(state.i18n.messages).to.have.property('worklist', '任务清单'); }); // 切换到英文 mockI18nSuccess('en'); cy.window().then((win) => { Object.defineProperty(win.navigator, 'language', { value: 'en-US', writable: false }); }); loginPage.visit(); cy.wait('@getI18nENSuccess'); // 验证英文状态完全替换了中文状态 cy.window().its('store').invoke('getState').then((state) => { expect(state.i18n.currentLocale).to.equal('en'); expect(state.i18n.loading).to.be.false; expect(state.i18n.error).to.be.null; expect(state.i18n.messages).to.have.property('register', 'Register'); expect(state.i18n.messages).to.have.property('worklist', 'Task List'); // 确保中文内容已被替换 expect(state.i18n.messages.register).to.not.equal('注册'); expect(state.i18n.messages.worklist).to.not.equal('任务清单'); }); }); it('验证快速语言切换的稳定性', () => { // 快速切换多次语言,验证系统稳定性 const switchSequence = [ { lang: 'zh-CN', locale: 'zh', text: '患者管理' }, { lang: 'en-US', locale: 'en', text: 'Patient Management' }, { lang: 'zh-TW', locale: 'zh', text: '患者管理' }, { lang: 'en-GB', locale: 'en', text: 'Patient Management' }, { lang: 'zh-CN', locale: 'zh', text: '患者管理' } ]; switchSequence.forEach(({ lang, locale, text }, index) => { mockI18nSuccess(locale as 'zh' | 'en'); cy.window().then((win) => { Object.defineProperty(win.navigator, 'language', { value: lang, writable: false }); }); loginPage.visit(); cy.wait(`@getI18n${locale.toUpperCase()}Success`); cy.get('body').should('contain', text); // 验证每次切换后状态都正确 cy.window().its('store').invoke('getState').then((state) => { expect(state.i18n.currentLocale).to.equal(locale); expect(state.i18n.loading).to.be.false; expect(state.i18n.error).to.be.null; }); }); }); });