Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Anan Zhuang <ananzh@amazon.com>
  • Loading branch information
ananzh committed Aug 15, 2024
1 parent 2052a66 commit c01f3d7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
59 changes: 59 additions & 0 deletions src/core/public/application/scoped_history.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
});
});
});
3 changes: 2 additions & 1 deletion src/core/public/application/scoped_history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,9 @@ export class ScopedHistory<HistoryLocationState = unknown>
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();
Expand Down
12 changes: 6 additions & 6 deletions src/core/public/locale_helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/core/public/locale_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/core/public/osd_bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -61,7 +62,7 @@ export async function __osdBootstrap__() {
/\/([^/]+)\.json$/,
`/${urlLocale}.json`
);
} else {
} else if (!urlLocale) {
i18n.setLocale('en');
}

Expand Down

0 comments on commit c01f3d7

Please sign in to comment.