Back to Repositories

Validating Estonian Locale Relative Time Formatting in iamkun/dayjs

This test suite validates the Estonian (et) locale implementation for relative time formatting in Day.js, comparing outputs with Moment.js for consistency. The tests verify various time intervals and their localized string representations.

Test Coverage Overview

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

Key areas tested include:
  • Various time intervals from seconds to years
  • Both past and future relative times
  • Exact and approximate time differences
  • Direct comparison with Moment.js output

Implementation Analysis

The testing approach uses Jest’s test runner with a systematic comparison methodology. Each test case validates Day.js output against Moment.js as the reference implementation.

Technical patterns include:
  • MockDate usage for consistent timestamp testing
  • Array-driven test cases for multiple scenarios
  • Plugin extension for relative time functionality
  • Bidirectional time difference testing

Technical Details

Testing infrastructure includes:
  • Jest as the testing framework
  • MockDate for date manipulation
  • Day.js relative time plugin
  • Moment.js for comparison validation
  • Estonian locale configuration

Best Practices Demonstrated

The test suite exemplifies several testing best practices in JavaScript datetime library validation.

Notable practices include:
  • Consistent test setup and teardown
  • Parametrized test cases
  • Cross-library validation
  • Locale-specific formatting verification
  • Comprehensive time unit coverage

iamkun/dayjs

test/locale/et.test.js

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

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
    [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('et')
    moment.locale('et')
    expect(dayjs().from(dayjs().add(t[0], t[1])))
      .toBe(moment().from(moment().add(t[0], t[1])))
    expect(dayjs().from(dayjs().subtract(t[0], t[1])))
      .toBe(moment().from(moment().subtract(t[0], t[1])))
    expect(dayjs().from(dayjs().add(t[0], t[1]), true))
      .toBe(moment().from(moment().add(t[0], t[1]), true))
  })
})