Back to Repositories

Validating Czech Locale Relative Time Implementation in iamkun/dayjs

This test suite validates the Czech (cs) locale implementation for relative time functionality in Day.js, comparing its output with Moment.js. It ensures accurate time difference expressions in Czech language across various time intervals.

Test Coverage Overview

The test suite provides comprehensive coverage of relative time formatting in Czech locale.

Key areas tested include:
  • Multiple time units from seconds to years
  • Various time intervals and thresholds
  • Both directional (from/to) and absolute time differences
  • Edge cases around unit transitions

Implementation Analysis

The testing approach employs Jest’s unit testing framework with MockDate for consistent time-based testing. The implementation uses a data-driven approach with an array of test cases covering different time units and durations. Each test case validates both directional and absolute relative time formatting against Moment.js as the reference implementation.

Technical Details

Testing tools and configuration:
  • Jest as the testing framework
  • MockDate for date mocking
  • Day.js with relativeTime plugin
  • Moment.js for comparison validation
  • Locale-specific testing for Czech language

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolated tests through proper setup/teardown with MockDate, comprehensive test data coverage, and comparative validation against an established library. The structured array of test cases enables maintainable and extensible testing of locale-specific formatting.

iamkun/dayjs

test/locale/cs.test.js

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

dayjs.extend(relativeTime)

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

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

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

  T.forEach((t) => {
    dayjs.locale('cs')
    moment.locale('cs')
    const dayjsDay = dayjs()
    const momentDay = moment()
    const dayjsCompare = dayjs().add(t[0], t[1])
    const momentCompare = moment().add(t[0], t[1])
    expect(dayjsDay.from(dayjsCompare))
      .toBe(momentDay.from(momentCompare))
    expect(dayjsDay.to(dayjsCompare))
      .toBe(momentDay.to(momentCompare))
    expect(dayjsDay.from(dayjsCompare, true))
      .toBe(momentDay.from(momentCompare, true))
  })
})