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 2 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
8 changes: 8 additions & 0 deletions src/libs/LocaleCompare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ Onyx.connect({
},
});

/**
* This is a wrapper around the localeCompare function that uses the preferred locale from the user's settings.
*
* It re-uses Intl.Collator 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
27 changes: 27 additions & 0 deletions tests/unit/LocaleCompareTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const {default: localeCompare} = require('@libs/LocaleCompare');

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

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);
});
});
Loading