Skip to content

Commit

Permalink
add tests for plural forms function
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksim Sinelnikov committed Sep 24, 2024
1 parent f684b73 commit e424708
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/prelude/i18n/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export function pluralizeText(
* @param n - The number to evaluate for pluralization.
* @param rules - Plural rules object. If undefined, a default rule set is used.
*/
function getPluralFormName(n: number, rules?: CanUndef<Intl.PluralRules>): keyof Required<PluralTranslation> {
export function getPluralFormName(n: number, rules?: CanUndef<Intl.PluralRules>): keyof Required<PluralTranslation> {
if (rules != null) {
return <keyof PluralTranslation>rules.select(n);
}
Expand Down
17 changes: 17 additions & 0 deletions src/core/prelude/i18n/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { pluralizeText, resolveTemplate } from 'core/prelude/i18n';
import { getPluralFormName } from 'core/prelude/i18n/helpers';

describe('core/prelude/i18n', () => {
const rules = new Intl.PluralRules('en');
Expand All @@ -22,6 +23,22 @@ describe('core/prelude/i18n', () => {

const formNames = <Array<keyof typeof forms>>Object.keys(forms);

describe('pluralization forms detection', () => {
it('detecting plural form without Intl rules', () => {
expect(getPluralFormName(0)).toBe('zero');
expect(getPluralFormName(1)).toBe('one');
expect(getPluralFormName(2)).toBe('few');
expect(getPluralFormName(5)).toBe('many');
});

it('detecting plural form using Intl rules', () => {
expect(getPluralFormName(0, rules)).toBe('other');
expect(getPluralFormName(1, rules)).toBe('one');
expect(getPluralFormName(2, rules)).toBe('other');
expect(getPluralFormName(5, rules)).toBe('other');
});
});

describe('text pluralization', () => {
it('using pluralization constants to choose the right form', () => {
formNames.forEach((form) => {
Expand Down

0 comments on commit e424708

Please sign in to comment.