Back to Repositories

Testing Chinese Locale Formatting Implementations in dayjs

This test suite evaluates the Chinese locale implementations (zh, zh-cn, zh-hk, zh-tw) in the Day.js library. It focuses on verifying proper formatting of ordinal numbers and meridiem expressions across different Chinese variants. The tests ensure consistent localization behavior for time-related formatting.

Test Coverage Overview

The test suite provides comprehensive coverage of Chinese locale-specific formatting features:

  • Ordinal number formatting validation across all Chinese variants
  • Meridiem (AM/PM) expression testing for 24-hour cycles
  • Comparison of formatting consistency between standard Chinese and regional variants
  • Week number formatting with locale-specific characters (周/週)

Implementation Analysis

The testing approach utilizes Jest’s framework capabilities for systematic locale verification. Tests employ dayjs instance creation with specific locale settings and compare formatted outputs across different Chinese variants. The implementation leverages Day.js plugins (advancedFormat, weekOfYear) to enable extended formatting capabilities.

Technical Details

Testing Environment:
  • Framework: Jest
  • Plugins: advancedFormat, weekOfYear
  • Locale Files: zh, zh-cn, zh-hk, zh-tw
  • Testing Methods: expect().toBe(), expect().toEqual()
  • Dynamic Testing: Iterative testing using for loops

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for specific functionality
  • Comprehensive coverage of edge cases through iterative testing
  • Clear test descriptions and expectations
  • Proper plugin initialization and setup
  • Consistent testing patterns across different locale variants

iamkun/dayjs

test/locale/zh-hk.test.js

            
import dayjs from '../../src'
import advancedFormat from '../../src/plugin/advancedFormat'
import weekOfYear from '../../src/plugin/weekOfYear'
import '../../src/locale/zh'
import '../../src/locale/zh-cn'
import '../../src/locale/zh-hk'
import '../../src/locale/zh-tw'

dayjs.extend(advancedFormat).extend(weekOfYear)

const zh = dayjs().locale('zh')
const zhCN = dayjs().locale('zh-cn')
const zhHK = dayjs().locale('zh-hk')
const zhTW = dayjs().locale('zh-tw')

test('ordinal', () => {
  expect(zh.format('wo')).toEqual(`${zh.format('w')}周`)
  expect(zhCN.format('wo')).toEqual(`${zhCN.format('w')}周`)
  expect(zhHK.format('wo')).toEqual(`${zhHK.format('w')}週`)
  expect(zhTW.format('wo')).toEqual(`${zhTW.format('w')}週`)
})

test('Meridiem', () => {
  for (let i = 0; i <= 24; i += 1) {
    expect(zh.add(i, 'hour').format('A')).toBe(zhCN.add(i, 'hour').format('A'))
  }
})