|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { normalizeLocale } from '../normalize-locale'; |
| 5 | +import setOptions from './utils/intl-polyfill'; |
| 6 | + |
| 7 | +function withDocumentLang(lang: string, callback: () => void) { |
| 8 | + const htmlElement = document.querySelector('html'); |
| 9 | + htmlElement!.setAttribute('lang', lang); |
| 10 | + callback(); |
| 11 | + htmlElement!.removeAttribute('lang'); |
| 12 | +} |
| 13 | + |
| 14 | +describe('normalizeLocale', () => { |
| 15 | + let consoleSpy: jest.SpyInstance; |
| 16 | + beforeEach(() => { |
| 17 | + consoleSpy = jest.spyOn(console, 'warn'); |
| 18 | + setOptions({ locale: 'en-US' }); |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(() => { |
| 22 | + expect(consoleSpy).not.toHaveBeenCalled(); |
| 23 | + }); |
| 24 | + |
| 25 | + test('should return the provided value', () => { |
| 26 | + expect(normalizeLocale('DatePickerTest', 'en-US')).toBe('en-US'); |
| 27 | + }); |
| 28 | + |
| 29 | + test('should extend provided value if it is short form', () => { |
| 30 | + expect(normalizeLocale('DatePickerTest', 'en')).toBe('en-US'); |
| 31 | + setOptions({ locale: 'en-GB' }); |
| 32 | + expect(normalizeLocale('DatePickerTest', 'en')).toBe('en-GB'); |
| 33 | + }); |
| 34 | + |
| 35 | + test('should not extend the provided value if it starts from different language', () => { |
| 36 | + expect(normalizeLocale('DatePickerTest', 'fr')).toBe('fr'); |
| 37 | + }); |
| 38 | + |
| 39 | + test('should replace underscores with dashes', () => { |
| 40 | + expect(normalizeLocale('DatePickerTest', 'zh_CN')).toBe('zh-CN'); |
| 41 | + }); |
| 42 | + |
| 43 | + test('should warn if the provided value is in invalid format', () => { |
| 44 | + expect(normalizeLocale('DatePickerTest', 'not-locale')).toBe('en-US'); |
| 45 | + expect(consoleSpy).toHaveBeenCalledWith( |
| 46 | + '[AwsUi] [DatePickerTest] Invalid locale provided: not-locale. Falling back to default' |
| 47 | + ); |
| 48 | + consoleSpy.mockReset(); |
| 49 | + }); |
| 50 | + |
| 51 | + test('should return document language by default', () => { |
| 52 | + withDocumentLang('en', () => { |
| 53 | + expect(normalizeLocale('DatePickerTest', null)).toBe('en-US'); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + test('should not extend document language with locale if they do not match', () => { |
| 58 | + withDocumentLang('fr', () => { |
| 59 | + expect(normalizeLocale('DatePickerTest', null)).toBe('fr'); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + test('should combine values from document lang and browser locale', () => { |
| 64 | + setOptions({ locale: 'fr-CA' }); |
| 65 | + withDocumentLang('fr', () => { |
| 66 | + expect(normalizeLocale('DatePickerTest', null)).toBe('fr-CA'); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + test('should replace underscores with dashes in document lang', () => { |
| 71 | + withDocumentLang('zh_CN', () => { |
| 72 | + expect(normalizeLocale('DatePickerTest', null)).toBe('zh-CN'); |
| 73 | + }); |
| 74 | + }); |
| 75 | +}); |
0 commit comments