Back to Repositories

Validating Serbian Cyrillic Locale Relative Time in dayjs

This test suite validates the Serbian Cyrillic locale implementation for relative time formatting in Day.js, covering both past and future time expressions. It ensures proper grammatical forms and time unit handling in the Serbian language.

Test Coverage Overview

The test suite provides comprehensive coverage of relative time formatting in Serbian Cyrillic.

Key areas tested include:
  • Time units: seconds, minutes, hours, days, months, and years
  • Past and future expressions
  • Singular and plural forms
  • Grammatical cases for different numeric values

Implementation Analysis

The testing approach utilizes Jest’s test framework with MockDate for consistent time-based testing. The implementation follows a data-driven pattern using an array of test cases that verify both fromNow() and fromNow(true) methods.

Each test case validates:
  • Time unit translation
  • Correct grammatical forms
  • Proper prefix/suffix handling

Technical Details

Testing tools and configuration:
  • Jest as the testing framework
  • MockDate for date manipulation
  • Day.js with relativeTime plugin
  • Serbian Cyrillic locale import
  • BeforeEach/AfterEach hooks for test isolation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Systematic test case organization
  • Proper test isolation using MockDate
  • Comprehensive edge case coverage
  • Clear test case structure
  • Maintainable data-driven test approach

iamkun/dayjs

test/locale/sr-cyrl.test.js

            
import MockDate from 'mockdate'
import dayjs from '../../src'
import relativeTime from '../../src/plugin/relativeTime'
import '../../src/locale/sr-cyrl'

dayjs.extend(relativeTime)

beforeEach(() => {
  MockDate.set(new Date())
})

afterEach(() => {
  MockDate.reset()
})

it('Serbian cyrillic locale relative time in past and future', () => {
  const cases = [
    [1, 's', 'за неколико секунди', 'неколико секунди'],
    [-1, 's', 'пре неколико секунди', 'неколико секунди'],
    [4, 's', 'за неколико секунди', 'неколико секунди'],
    [1, 'm', 'за један минут', 'један минут'],
    [-1, 'm', 'пре једног минута', 'један минут'],
    [4, 'm', 'за 4 минута', '4 минута'],
    [5, 'm', 'за 5 минута', '5 минута'],
    [21, 'm', 'за 21 минут', '21 минут'],
    [1, 'h', 'за један сат', 'један сат'],
    [-1, 'h', 'пре једног сата', 'један сат'],
    [4, 'h', 'за 4 сата', '4 сата'],
    [5, 'h', 'за 5 сати', '5 сати'],
    [21, 'h', 'за 21 сат', '21 сат'],
    [1, 'd', 'за један дан', 'један дан'],
    [-1, 'd', 'пре једног дана', 'један дан'],
    [4, 'd', 'за 4 дана', '4 дана'],
    [5, 'd', 'за 5 дана', '5 дана'],
    [21, 'd', 'за 21 дан', '21 дан'],
    [1, 'M', 'за један месец', 'један месец'],
    [-1, 'M', 'пре једног месеца', 'један месец'],
    [4, 'M', 'за 4 месеца', '4 месеца'],
    [5, 'M', 'за 5 месеци', '5 месеци'],
    [10, 'M', 'за 10 месеци', '10 месеци'],
    [1, 'y', 'за једну годину', 'једна година'],
    [-1, 'y', 'пре једне године', 'једна година'],
    [4, 'y', 'за 4 године', '4 године'],
    [5, 'y', 'за 5 година', '5 година'],
    [21, 'y', 'за 21 годину', '21 година']
  ]

  cases.forEach((c) => {
    expect(dayjs().add(c[0], c[1]).locale('sr-cyrl').fromNow()).toBe(c[2])
    expect(dayjs().add(c[0], c[1]).locale('sr-cyrl').fromNow(true)).toBe(c[3])
    // TODO: compare to momentjs once logic and grammar are fixed there
  })
})