Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NoQA] LocaleCompare util tests #36879

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have asked to add more tests to verify that the Spanish locale works asa expected/ compared to English

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']);
mountiny marked this conversation as resolved.
Show resolved Hide resolved
});
});
Loading