Skip to content

Commit

Permalink
Improve test coverage of getPluralSuffix
Browse files Browse the repository at this point in the history
  • Loading branch information
36degrees committed Oct 19, 2022
1 parent d3ebcaa commit 00f4566
Showing 1 changed file with 72 additions and 4 deletions.
76 changes: 72 additions & 4 deletions src/govuk/i18n.unit.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -165,27 +165,77 @@ describe('I18n', () => {
})

describe('.getPluralSuffix', () => {
it('returns the preferred plural form for the locale if a translation exists', () => {
let consoleWarn

beforeEach(() => {
// Silence warnings in test output, and allow us to 'expect' them
consoleWarn = jest.spyOn(global.console, 'warn')
.mockImplementation(() => { /* noop */ })
})

afterEach(() => {
jest.restoreAllMocks()
})

it('uses `Intl.PluralRules` when available', () => {
const IntlPluralRulesSelect = jest.spyOn(global.Intl.PluralRules.prototype, 'select')
.mockImplementation(() => 'one')

const i18n = new I18n({
'test.one': 'test',
'test.other': 'test'
}, {
locale: 'en'
})

expect(i18n.getPluralSuffix('test', 1)).toBe('one')
expect(IntlPluralRulesSelect).toBeCalledWith(1)
})

it('falls back to `other` if a translation for the plural form does not exist', () => {
it('falls back to internal fallback rules', () => {
const i18n = new I18n({
'test.one': 'test',
'test.other': 'test'
}, {
locale: 'en'
})

jest.spyOn(i18n, 'hasIntlPluralRulesSupport')
.mockImplementation(() => false)

const selectPluralFormUsingFallbackRules = jest.spyOn(i18n, 'selectPluralFormUsingFallbackRules')

i18n.getPluralSuffix('test', 1)
expect(selectPluralFormUsingFallbackRules).toBeCalledWith(1)
})

it('returns the preferred plural form for the locale if a translation exists', () => {
const i18n = new I18n({
'test.one': 'test',
'test.other': 'test'
}, {
locale: 'en'
})
expect(i18n.getPluralSuffix('test', 1)).toBe('other')
expect(i18n.getPluralSuffix('test', 1)).toBe('one')
})

const welshForms = [
{ form: 'one', count: 1 },
{ form: 'two', count: 2 },
{ form: 'few', count: 3 },
{ form: 'many', count: 6 }
]

it.each(welshForms)('`$form` falls back to `other` if translation for preferred form `$form` is missing', ({ count }) => {
const i18n = new I18n({
'test.other': 'test'
}, {
locale: 'cy'
})
expect(i18n.getPluralSuffix('test', count)).toBe('other')
})

it('logs a console warning when falling back to `other`', () => {
const consoleWarn = jest.spyOn(global.console, 'warn')
const i18n = new I18n({
'test.other': 'test'
}, {
Expand All @@ -199,6 +249,14 @@ describe('I18n', () => {
)
})

it('throws an error if trying to use `other` but `other` is not provided', () => {
const i18n = new I18n({}, {
locale: 'en'
})
expect(() => { i18n.getPluralSuffix('test', 2) })
.toThrowError('i18n: Plural form ".other" is required for "en" locale')
})

it('throws an error if a plural form is not provided and neither is `other`', () => {
const i18n = new I18n({
'test.one': 'test'
Expand All @@ -208,6 +266,16 @@ describe('I18n', () => {
expect(() => { i18n.getPluralSuffix('test', 2) })
.toThrowError('i18n: Plural form ".other" is required for "en" locale')
})

it('returns `other` for non-numbers', () => {
const i18n = new I18n({
'test.other': 'test'
}, {
locale: 'en'
})

expect(i18n.getPluralSuffix('test', 'nonsense')).toBe('other')
})
})

describe('.getPluralRulesForLocale', () => {
Expand Down

0 comments on commit 00f4566

Please sign in to comment.