Skip to content

Commit

Permalink
Merge pull request #36879 from callstack-internal/audit/implementatio…
Browse files Browse the repository at this point in the history
…n/localeCompare-tests

[NoQA] LocaleCompare util tests
  • Loading branch information
mountiny authored Feb 23, 2024
2 parents 1d2016b + 1ed0f0d commit c2aa828
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/libs/LocaleCompare.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import Onyx from 'react-native-onyx';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';

const DEFAULT_LOCALE = 'en';

const COLLATOR_OPTIONS: Intl.CollatorOptions = {usage: 'sort', sensitivity: 'base'};

let collator = new Intl.Collator(DEFAULT_LOCALE, COLLATOR_OPTIONS);
let collator = new Intl.Collator(CONST.LOCALES.DEFAULT, COLLATOR_OPTIONS);

Onyx.connect({
key: ONYXKEYS.NVP_PREFERRED_LOCALE,
callback: (locale) => {
collator = new Intl.Collator(locale ?? DEFAULT_LOCALE, COLLATOR_OPTIONS);
collator = new Intl.Collator(locale ?? CONST.LOCALES.DEFAULT, COLLATOR_OPTIONS);
},
});

/**
* This is a wrapper around the localeCompare function that uses the preferred locale from the user's settings.
*
* It re-uses Intl.Collator with static options for performance reasons. See https://github.com/facebook/hermes/issues/867 for more details.
* @param a
* @param b
* @returns -1 if a < b, 1 if a > b, 0 if a === b
*/
function localeCompare(a: string, b: string) {
return collator.compare(a, b);
}
Expand Down
51 changes: 51 additions & 0 deletions tests/unit/LocaleCompareTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Onyx from 'react-native-onyx';
import localeCompare from '@libs/LocaleCompare';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

describe('localeCompare', () => {
beforeAll(() => {
Onyx.init({
keys: {NVP_PREFERRED_LOCALE: ONYXKEYS.NVP_PREFERRED_LOCALE},
initialKeyStates: {[ONYXKEYS.NVP_PREFERRED_LOCALE]: CONST.LOCALES.DEFAULT},
});
return waitForBatchedUpdates();
});

afterEach(() => Onyx.clear());

it('should return -1 for descending comparison', () => {
const result = localeCompare('Da Vinci', 'Tesla');

expect(result).toBe(-1);
});

it('should return -1 for ascending comparison', () => {
const result = localeCompare('Zidane', 'Messi');

expect(result).toBe(1);
});

it('should return 0 for equal strings', () => {
const result = localeCompare('Cat', 'Cat');

expect(result).toBe(0);
});

it('should discard sensitivity differences', () => {
const result = localeCompare('apple', 'Apple');

expect(result).toBe(0);
});

it('distinguishes spanish diacritic characters', async () => {
await Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.ES);

const input = ['zorro', 'árbol', 'jalapeño', 'jalapeno', 'nino', 'niño'];

input.sort(localeCompare);

expect(input).toEqual(['árbol', 'jalapeno', 'jalapeño', 'nino', 'niño', 'zorro']);
});
});

0 comments on commit c2aa828

Please sign in to comment.