Back to Repositories

Validating Hungarian Locale Relative Time Formatting in dayjs

This test suite validates the Hungarian (hu) locale implementation for relative time formatting in the dayjs library. It ensures proper translation and formatting of time differences in Hungarian, comparing results with the moment.js implementation for accuracy.

Test Coverage Overview

The test suite comprehensively covers relative time formatting in Hungarian locale, testing various time intervals from seconds to years.

Key areas tested include:
  • Time differences ranging from 44.4 seconds to 18 months
  • Both forward and backward relative time expressions
  • Exact and relative time formatting variations

Implementation Analysis

The testing approach uses a data-driven methodology with an array of test cases covering different time units and durations. Each test case verifies three distinct formatting patterns:

  • from() method for past relative time
  • to() method for future relative time
  • from() with exact flag for precise durations

Technical Details

Testing infrastructure includes:

  • Jest as the testing framework
  • dayjs with relativeTime plugin
  • moment.js for comparison validation
  • Hungarian locale implementation for both libraries
  • Dynamic test case generation using array iteration

Best Practices Demonstrated

The test implementation showcases several testing best practices:

  • Systematic test case organization using data arrays
  • Consistent validation against an established library (moment.js)
  • Thorough coverage of edge cases and time units
  • Clean separation of test cases and assertions
  • Efficient test execution through iteration

iamkun/dayjs

test/locale/hu.test.js

            
import moment from 'moment'
import dayjs from '../../src'
import relativeTime from '../../src/plugin/relativeTime'
import '../../src/locale/hu'

dayjs.extend(relativeTime)

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('hu')
    moment.locale('hu')

    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))
  })
})