|
| 1 | +import {renderHook} from '@testing-library/react-native'; |
| 2 | +import React from 'react'; |
| 3 | +import Onyx from 'react-native-onyx'; |
| 4 | +import {LocaleContextProvider} from '@components/LocaleContextProvider'; |
| 5 | +import useLocalize from '@hooks/useLocalize'; |
| 6 | +import CONST from '@src/CONST'; |
| 7 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 8 | +import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; |
| 9 | + |
| 10 | +describe('useLocalize', () => { |
| 11 | + beforeAll(async () => { |
| 12 | + Onyx.init({keys: ONYXKEYS}); |
| 13 | + await Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.DEFAULT); |
| 14 | + return waitForBatchedUpdatesWithAct(); |
| 15 | + }); |
| 16 | + |
| 17 | + function TestWrapper({children}: {children: React.ReactNode}) { |
| 18 | + return <LocaleContextProvider>{children}</LocaleContextProvider>; |
| 19 | + } |
| 20 | + |
| 21 | + describe('localeCompare', () => { |
| 22 | + it('should return -1 for descending comparison', async () => { |
| 23 | + const {result} = renderHook(() => useLocalize(), { |
| 24 | + wrapper: TestWrapper, |
| 25 | + }); |
| 26 | + await waitForBatchedUpdatesWithAct(); |
| 27 | + const ans = result.current.localeCompare('Da Vinci', 'Tesla'); |
| 28 | + |
| 29 | + expect(ans).toBe(-1); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should return -1 for ascending comparison', async () => { |
| 33 | + const {result} = renderHook(() => useLocalize(), { |
| 34 | + wrapper: TestWrapper, |
| 35 | + }); |
| 36 | + await waitForBatchedUpdatesWithAct(); |
| 37 | + const ans = result.current.localeCompare('Zidane', 'Messi'); |
| 38 | + |
| 39 | + expect(ans).toBe(1); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return 0 for equal strings', async () => { |
| 43 | + const {result} = renderHook(() => useLocalize(), { |
| 44 | + wrapper: TestWrapper, |
| 45 | + }); |
| 46 | + await waitForBatchedUpdatesWithAct(); |
| 47 | + const ans = result.current.localeCompare('Cat', 'Cat'); |
| 48 | + |
| 49 | + expect(ans).toBe(0); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should put uppercase letters first', async () => { |
| 53 | + const {result} = renderHook(() => useLocalize(), { |
| 54 | + wrapper: TestWrapper, |
| 55 | + }); |
| 56 | + await waitForBatchedUpdatesWithAct(); |
| 57 | + const ans = result.current.localeCompare('apple', 'Apple'); |
| 58 | + |
| 59 | + expect(ans).toBe(1); |
| 60 | + }); |
| 61 | + |
| 62 | + it('distinguishes spanish diacritic characters', async () => { |
| 63 | + await Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.ES); |
| 64 | + await waitForBatchedUpdatesWithAct(); |
| 65 | + |
| 66 | + const {result} = renderHook(() => useLocalize(), { |
| 67 | + wrapper: TestWrapper, |
| 68 | + }); |
| 69 | + await waitForBatchedUpdatesWithAct(); |
| 70 | + |
| 71 | + const input = ['zorro', 'árbol', 'jalapeño', 'jalapeno', 'nino', 'niño']; |
| 72 | + |
| 73 | + input.sort(result.current.localeCompare); |
| 74 | + |
| 75 | + expect(input).toEqual(['árbol', 'jalapeno', 'jalapeño', 'nino', 'niño', 'zorro']); |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
0 commit comments