|
| 1 | +/* |
| 2 | + * Copyright 2023 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import {act, render} from '@react-spectrum/test-utils-internal'; |
| 14 | +import {I18nProvider, useLocale} from '../src/context'; |
| 15 | +import React from 'react'; |
| 16 | + |
| 17 | +function TestComponent() { |
| 18 | + let locale = useLocale(); |
| 19 | + return ( |
| 20 | + <div> |
| 21 | + <div data-testid="locale">{locale.locale}</div> |
| 22 | + <div data-testid="direction">{locale.direction}</div> |
| 23 | + </div> |
| 24 | + ); |
| 25 | +} |
| 26 | + |
| 27 | +function languageProps(language) { |
| 28 | + return { |
| 29 | + value: language, |
| 30 | + writable: true, |
| 31 | + configurable: true |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +describe('useLocale languagechange', () => { |
| 36 | + let originalNavigator; |
| 37 | + let originalLanguage; |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + originalNavigator = window.navigator; |
| 41 | + originalLanguage = window.navigator.language; |
| 42 | + |
| 43 | + Object.defineProperty(window.navigator, 'language', languageProps('en-US')); |
| 44 | + |
| 45 | + act(() => { |
| 46 | + window.dispatchEvent(new Event('languagechange')); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + Object.defineProperty(window.navigator, 'language', languageProps(originalLanguage)); |
| 52 | + |
| 53 | + act(() => { |
| 54 | + window.dispatchEvent(new Event('languagechange')); |
| 55 | + }); |
| 56 | + |
| 57 | + Object.defineProperty(window, 'navigator', languageProps(originalNavigator)); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should update locale when languagechange event is triggered', () => { |
| 61 | + let {getByTestId} = render( |
| 62 | + <I18nProvider> |
| 63 | + <TestComponent /> |
| 64 | + </I18nProvider> |
| 65 | + ); |
| 66 | + |
| 67 | + // Initial render should show en-US |
| 68 | + expect(getByTestId('locale')).toHaveTextContent('en-US'); |
| 69 | + expect(getByTestId('direction')).toHaveTextContent('ltr'); |
| 70 | + |
| 71 | + // Change navigator.language and trigger languagechange event |
| 72 | + act(() => { |
| 73 | + Object.defineProperty(window.navigator, 'language', languageProps('pt-PT')); |
| 74 | + window.dispatchEvent(new Event('languagechange')); |
| 75 | + }); |
| 76 | + |
| 77 | + // Should re-render with new locale |
| 78 | + expect(getByTestId('locale')).toHaveTextContent('pt-PT'); |
| 79 | + expect(getByTestId('direction')).toHaveTextContent('ltr'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should update locale direction when changing from LTR to RTL language', () => { |
| 83 | + let {getByTestId} = render( |
| 84 | + <I18nProvider> |
| 85 | + <TestComponent /> |
| 86 | + </I18nProvider> |
| 87 | + ); |
| 88 | + |
| 89 | + // Change to Hebrew (RTL language) |
| 90 | + act(() => { |
| 91 | + Object.defineProperty(window.navigator, 'language', languageProps('he-IL')); |
| 92 | + window.dispatchEvent(new Event('languagechange')); |
| 93 | + }); |
| 94 | + |
| 95 | + // Should update to Hebrew with RTL direction |
| 96 | + expect(getByTestId('locale')).toHaveTextContent('he-IL'); |
| 97 | + expect(getByTestId('direction')).toHaveTextContent('rtl'); |
| 98 | + |
| 99 | + // Change back to Portuguese |
| 100 | + act(() => { |
| 101 | + Object.defineProperty(window.navigator, 'language', languageProps('pt-PT')); |
| 102 | + window.dispatchEvent(new Event('languagechange')); |
| 103 | + }); |
| 104 | + |
| 105 | + // Should update to Portuguese |
| 106 | + expect(getByTestId('locale')).toHaveTextContent('pt-PT'); |
| 107 | + expect(getByTestId('direction')).toHaveTextContent('ltr'); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should not change displayed locale when explicit locale is provided via I18nProvider', () => { |
| 111 | + let {getByTestId} = render( |
| 112 | + <I18nProvider locale="fr-FR"> |
| 113 | + <TestComponent /> |
| 114 | + </I18nProvider> |
| 115 | + ); |
| 116 | + |
| 117 | + // Initial render should show fr-FR |
| 118 | + expect(getByTestId('locale')).toHaveTextContent('fr-FR'); |
| 119 | + expect(getByTestId('direction')).toHaveTextContent('ltr'); |
| 120 | + |
| 121 | + // Change navigator.language and trigger languagechange event |
| 122 | + act(() => { |
| 123 | + Object.defineProperty(window.navigator, 'language', languageProps('ja-JP')); |
| 124 | + window.dispatchEvent(new Event('languagechange')); |
| 125 | + }); |
| 126 | + |
| 127 | + // Should still show fr-FR (explicit locale takes precedence) |
| 128 | + expect(getByTestId('locale')).toHaveTextContent('fr-FR'); |
| 129 | + expect(getByTestId('direction')).toHaveTextContent('ltr'); |
| 130 | + }); |
| 131 | +}); |
0 commit comments