From c01f3d75255a9882a71b1879e391aec3daa2693c Mon Sep 17 00:00:00 2001 From: Anan Zhuang Date: Thu, 15 Aug 2024 02:14:36 +0000 Subject: [PATCH] fix tests Signed-off-by: Anan Zhuang --- .../public/application/scoped_history.test.ts | 59 +++++++++++++++++++ src/core/public/application/scoped_history.ts | 3 +- src/core/public/locale_helper.test.ts | 12 ++-- src/core/public/locale_helper.ts | 6 +- src/core/public/osd_bootstrap.ts | 5 +- 5 files changed, 73 insertions(+), 12 deletions(-) diff --git a/src/core/public/application/scoped_history.test.ts b/src/core/public/application/scoped_history.test.ts index 067c33256bd1..f2b9c3f7186c 100644 --- a/src/core/public/application/scoped_history.test.ts +++ b/src/core/public/application/scoped_history.test.ts @@ -30,6 +30,18 @@ import { ScopedHistory } from './scoped_history'; import { createMemoryHistory } from 'history'; +import { getLocaleInUrl } from '../locale_helper'; +import { i18n } from '@osd/i18n'; + +jest.mock('../locale_helper', () => ({ + getLocaleInUrl: jest.fn(), +})); + +jest.mock('@osd/i18n', () => ({ + i18n: { + getLocale: jest.fn(), + }, +})); describe('ScopedHistory', () => { describe('construction', () => { @@ -358,4 +370,51 @@ describe('ScopedHistory', () => { expect(gh.length).toBe(4); }); }); + + describe('locale handling', () => { + let originalLocation: Location; + + beforeEach(() => { + originalLocation = window.location; + delete (window as any).location; + window.location = { href: 'http://localhost/app/wow', reload: jest.fn() } as any; + (getLocaleInUrl as jest.Mock).mockReturnValue(null); + (i18n.getLocale as jest.Mock).mockReturnValue('en'); + }); + + afterEach(() => { + window.location = originalLocation; + jest.resetAllMocks(); + }); + + it('reloads the page when locale changes', () => { + const gh = createMemoryHistory(); + gh.push('/app/wow'); + + const h = new ScopedHistory(gh, '/app/wow'); + + // Mock getLocaleInUrl to return a different locale + (getLocaleInUrl as jest.Mock).mockReturnValue('fr'); + + // Simulate navigation + gh.push('/app/wow/new-page'); + + expect(window.location.reload).toHaveBeenCalled(); + }); + + it('does not reload the page when locale changes', () => { + const gh = createMemoryHistory(); + gh.push('/app/wow'); + + const h = new ScopedHistory(gh, '/app/wow'); + + // Mock getLocaleInUrl to return a different locale + (getLocaleInUrl as jest.Mock).mockReturnValue('en'); + + // Simulate navigation + gh.push('/app/wow/new-page'); + + expect(window.location.reload).not.toHaveBeenCalled(); + }); + }); }); diff --git a/src/core/public/application/scoped_history.ts b/src/core/public/application/scoped_history.ts index 2befe5335bc1..74e4bb068d3e 100644 --- a/src/core/public/application/scoped_history.ts +++ b/src/core/public/application/scoped_history.ts @@ -317,8 +317,9 @@ export class ScopedHistory this.isActive = false; return; } - // const fullUrl = `${location.pathname}${location.search}${location.hash}`; + const localeValue = getLocaleInUrl(window.location.href); + if (localeValue !== currentLocale) { // Force a full page reload window.location.reload(); diff --git a/src/core/public/locale_helper.test.ts b/src/core/public/locale_helper.test.ts index 0ba878feae38..238dbced3892 100644 --- a/src/core/public/locale_helper.test.ts +++ b/src/core/public/locale_helper.test.ts @@ -21,21 +21,21 @@ describe('getLocaleInUrl', () => { expect(getLocaleInUrl(url)).toBe('fr-FR'); }); - it('should return null for a URL without locale', () => { + it('should return en for a URL without locale', () => { const url = 'http://localhost:5603/app/home'; - expect(getLocaleInUrl(url)).toBeNull(); + expect(getLocaleInUrl(url)).toBe('en'); }); - it('should return null and set a warning for an invalid locale format in hash', () => { + it('should return en and set a warning for an invalid locale format in hash', () => { const url = 'http://localhost:5603/app/home#/&locale=de-DE'; - expect(getLocaleInUrl(url)).toBeNull(); + expect(getLocaleInUrl(url)).toBe('en'); expect((window as any).__localeWarning).toBeDefined(); expect((window as any).__localeWarning.title).toBe('Invalid URL Format'); }); - it('should return null for an empty locale value', () => { + it('should return en for an empty locale value', () => { const url = 'http://localhost:5603/app/home?locale='; - expect(getLocaleInUrl(url)).toBeNull(); + expect(getLocaleInUrl(url)).toBe('en'); }); it('should handle URLs with other query parameters', () => { diff --git a/src/core/public/locale_helper.ts b/src/core/public/locale_helper.ts index 77ac4b4583fd..38a734a523b1 100644 --- a/src/core/public/locale_helper.ts +++ b/src/core/public/locale_helper.ts @@ -43,11 +43,11 @@ export function getLocaleInUrl(url: string): string | null { // Check for non standard query format: if (localeValue === null && url.includes('&locale=')) { setInvalidUrlWithLocaleWarning(); - return null; + return 'en'; } - // Return the locale value if found, or null if not found - return localeValue && localeValue.trim() !== '' ? localeValue : null; + // Return the locale value if found, or 'en' if not found + return localeValue && localeValue.trim() !== '' ? localeValue : 'en'; } function setInvalidUrlWarning(): void { diff --git a/src/core/public/osd_bootstrap.ts b/src/core/public/osd_bootstrap.ts index b1f00d25ca18..5190fdc37e21 100644 --- a/src/core/public/osd_bootstrap.ts +++ b/src/core/public/osd_bootstrap.ts @@ -40,9 +40,10 @@ export async function __osdBootstrap__() { ); // Extract the locale from the URL if present + const currentLocale = i18n.getLocale(); const urlLocale = getLocaleInUrl(window.location.href); - if (urlLocale) { + if (urlLocale && urlLocale !== currentLocale) { // If a locale is specified in the URL, update the i18n settings // This enables dynamic language switching // Note: This works in conjunction with server-side changes: @@ -61,7 +62,7 @@ export async function __osdBootstrap__() { /\/([^/]+)\.json$/, `/${urlLocale}.json` ); - } else { + } else if (!urlLocale) { i18n.setLocale('en'); }