Back to Repositories

Validating Ukrainian Locale Implementation in dayjs

This test suite validates the Ukrainian (uk) locale implementation in dayjs, focusing on date formatting, relative time calculations, and locale-specific time unit handling. The tests ensure proper localization functionality by comparing dayjs outputs with moment.js reference implementations.

Test Coverage Overview

The test suite provides comprehensive coverage of Ukrainian locale features in dayjs.

Key areas tested include:
  • Month formatting with various format patterns
  • Relative time expressions with different time units
  • Time difference calculations
  • Edge cases for time unit boundaries

Implementation Analysis

The testing approach utilizes Jest’s testing framework to compare dayjs implementations against moment.js as the reference standard.

Technical patterns include:
  • Iterative testing across multiple format patterns
  • Time unit array-based test cases
  • Mock date management for consistent testing
  • Direct comparison of formatting outputs

Technical Details

Testing infrastructure includes:
  • Jest as the primary testing framework
  • MockDate for date manipulation
  • dayjs with relativeTime plugin
  • moment.js for reference implementations
  • Locale-specific format strings and patterns

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through structured organization and thorough validation.

Notable practices include:
  • Consistent test setup and teardown
  • Comprehensive format pattern testing
  • Systematic relative time validations
  • Explicit locale handling
  • Clear test case organization

iamkun/dayjs

test/locale/uk.test.js

            
import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../../src'
import relativeTime from '../../src/plugin/relativeTime'
import '../../src/locale/uk'

dayjs.extend(relativeTime)

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

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

it('Format Month with locale function', () => {
  for (let i = 0; i <= 7; i += 1) {
    const dayjsUK = dayjs().locale('uk').add(i, 'day')
    const momentUK = moment().locale('uk').add(i, 'day')
    const testFormat1 = 'DD MMMM YYYY MMM'
    const testFormat2 = 'MMMM'
    const testFormat3 = 'MMM'
    expect(dayjsUK.format(testFormat1)).toEqual(momentUK.format(testFormat1))
    expect(dayjsUK.format(testFormat2)).toEqual(momentUK.format(testFormat2))
    expect(dayjsUK.format(testFormat3)).toEqual(momentUK.format(testFormat3))
  }
})

it('RelativeTime: Time from X', () => {
  const T = [
    [44.4, 'second'], // a few seconds
    [89.5, 'second'], // a minute
    [43, 'minute'], // 44 minutes
    [21, 'hour'], // 21 hours
    [25, 'day'], // 25 days
    [10, 'month'], // 2 month
    [18, 'month'] // 2 years
  ]

  T.forEach((t) => {
    dayjs.locale('uk')
    moment.locale('uk')
    expect(dayjs().from(dayjs().add(t[0], t[1])))
      .toBe(moment().from(moment().add(t[0], t[1])))
    expect(dayjs().from(dayjs().add(t[0], t[1]), true))
      .toBe(moment().from(moment().add(t[0], t[1]), true))
  })
})

it('hour', () => {
  const str0 = '2020-03-18 19:15:00'
  const str = '2020-03-18 20:15:00'
  const result = dayjs(str0).locale('uk').to(str)

  expect(result).toEqual(moment(str0).locale('uk').to(str))
  const result2 = dayjs(str).locale('uk').to(str0, true)
  expect(result2).toEqual('година') // different from moment.js
})