Back to Repositories

Validating Kurdish Locale Implementation in dayjs

This test suite validates Kurdish (ku) locale functionality in the Day.js library, focusing on meridiem formatting and locale-specific date parsing. The tests ensure proper handling of time formats and number conversions specific to the Kurdish language implementation.

Test Coverage Overview

The test suite provides comprehensive coverage of Kurdish locale-specific functionality in Day.js.

Key areas tested include:
  • Meridiem (AM/PM) formatting with Kurdish characters
  • Number conversion between English and Arabic numerals
  • Time format parsing with locale-specific requirements
  • Integration with moment.js for format compatibility

Implementation Analysis

The testing approach utilizes Jest’s testing framework with MockDate for consistent date manipulation. The implementation follows a systematic pattern of testing time conversions across a 24-hour period and week-long date ranges.

Technical patterns include:
  • Iterative testing using for loops to cover all hour variations
  • Direct comparison with moment.js output for validation
  • Usage of preParsePostFormat plugin for locale handling

Technical Details

Testing tools and configuration:
  • Jest as the primary testing framework
  • MockDate for date manipulation and consistency
  • Day.js core library with locale plugin
  • preParsePostFormat plugin for locale handling
  • moment.js for comparison testing

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices through systematic validation of locale-specific functionality.

Notable practices include:
  • Consistent test setup and teardown using beforeEach/afterEach
  • Comprehensive coverage of edge cases
  • Clear test case isolation
  • Proper plugin integration testing
  • Cross-library validation with moment.js

iamkun/dayjs

test/locale/ku.test.js

            
import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../../src'
import locale, { englishToArabicNumbersMap } from '../../src/locale/ku'
import preParsePostFormat from '../../src/plugin/preParsePostFormat'

dayjs.extend(preParsePostFormat)

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

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

it('Format meridiem correctly', () => {
  for (let i = 0; i <= 23; i += 1) {
    const dayjsKu = dayjs()
      .startOf('day')
      .add(i, 'hour')
    const hour = (i % 12 || 12)
      .toString()
      .replace(/\d/g, match => englishToArabicNumbersMap[match])
    const m = i < 12 ? 'پ.ن' : 'د.ن'
    expect(dayjsKu.locale('ku').format('h A')).toBe(`${hour} ${m}`)
  }
})

it('Preparse with locale function', () => {
  for (let i = 0; i <= 7; i += 1) {
    dayjs.locale(locale)
    const momentKu = moment()
      .locale('ku')
      .add(i, 'day')
    expect(dayjs(momentKu.format()).format()).toEqual(momentKu.format())
  }
})