Skip to content

Commit 2a253ee

Browse files
authored
Merge pull request Expensify#71735 from shubham1206agra/revert-71724-revert-71570-refactor-onyx-57
Refactored usage of localeCompare in ReportUtils (part 4)
2 parents 57a9c63 + 31793e7 commit 2a253ee

5 files changed

Lines changed: 88 additions & 91 deletions

File tree

cspell.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@
800800
"src/TIMEZONES.ts",
801801
"tests/unit/EmojiTest.ts",
802802
"tests/unit/FastSearchTest.ts",
803-
"tests/unit/LocaleCompareTest.ts",
803+
"tests/unit/useLocalizeTest.tsx",
804804
"tests/unit/removeInvisibleCharacters.ts",
805805
"tests/unit/searchCountryOptionsTest.ts",
806806
"tests/unit/currencyList.json",

src/libs/LocaleCompare.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/libs/ReportUtils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ import {getMicroSecondOnyxErrorWithTranslationKey, isReceiptError} from './Error
101101
import getAttachmentDetails from './fileDownload/getAttachmentDetails';
102102
import getBase62ReportID from './getBase62ReportID';
103103
import {isReportMessageAttachment} from './isReportMessageAttachment';
104-
import localeCompareLibs from './LocaleCompare';
105104
import {formatPhoneNumber} from './LocalePhoneNumber';
106105
import {translateLocal} from './Localize';
107106
import Log from './Log';
@@ -3107,6 +3106,14 @@ function buildParticipantsFromAccountIDs(accountIDs: number[]): Participants {
31073106
}, finalParticipants);
31083107
}
31093108

3109+
/**
3110+
* @private
3111+
* This is a custom collator only for getGroupChatName function.
3112+
* The reason for this is that the computation of default group name should not depend on the locale.
3113+
* This is used to ensure that group name stays consistent across locales.
3114+
*/
3115+
const customCollator = new Intl.Collator('en', {usage: 'sort', sensitivity: 'variant', numeric: true, caseFirst: 'upper'});
3116+
31103117
/**
31113118
* Returns the report name if the report is a group chat
31123119
*/
@@ -3138,7 +3145,7 @@ function getGroupChatName(participants?: SelectedParticipant[], shouldApplyLimit
31383145
(participantAccountID, index) =>
31393146
getDisplayNameForParticipant({accountID: participantAccountID, shouldUseShortForm: isMultipleParticipantReport}) || formatPhoneNumber(participants?.[index]?.login ?? ''),
31403147
)
3141-
.sort((first, second) => localeCompareLibs(first ?? '', second ?? ''))
3148+
.sort((first, second) => customCollator.compare(first ?? '', second ?? ''))
31423149
.filter(Boolean)
31433150
.join(', ')
31443151
.slice(0, CONST.REPORT_NAME_LIMIT)

tests/unit/LocaleCompareTest.ts

Lines changed: 0 additions & 51 deletions
This file was deleted.

tests/unit/useLocalizeTest.tsx

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)