Back to Repositories

Testing Arabic Locale Meridiem Implementation in dayjs

This test suite validates the Arabic (Libya) locale implementation in the Day.js library, focusing on meridiem (AM/PM) formatting. The tests ensure proper time-of-day indicators in Arabic script for the Libyan dialect.

Test Coverage Overview

The test suite provides comprehensive coverage of meridiem formatting for the ar-ly locale.

Key areas tested include:
  • Morning hours (ص) validation before noon
  • Afternoon/evening hours (م) validation
  • Boundary cases at different times of day
  • Proper locale switching and formatting

Implementation Analysis

The testing approach utilizes Jest’s testing framework with MockDate for consistent time-based testing. The implementation follows a systematic pattern of testing time formatting across different periods of the day, using dayjs’s locale API and format method for verification.

The tests specifically validate the correct Arabic character representation for AM (ص) and PM (م) across multiple time points.

Technical Details

Testing infrastructure includes:
  • Jest as the testing framework
  • MockDate for date manipulation
  • Day.js library with relative time plugin
  • Arabic (Libya) locale implementation
  • Custom locale switching functionality

Best Practices Demonstrated

The test suite demonstrates several testing best practices:

  • Proper test setup and teardown using beforeEach and afterEach hooks
  • Isolated testing environment with mock dates
  • Clear test case organization
  • Consistent assertion patterns
  • Comprehensive boundary testing

iamkun/dayjs

test/locale/ar-ly.test.js

            
import MockDate from 'mockdate'
import dayjs from '../../src'
import relativeTime from '../../src/plugin/relativeTime'
import '../../src/locale/ru'
import locale from '../../src/locale/ar-ly'

dayjs.extend(relativeTime)

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

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

it('Meridiem', () => {
  dayjs.locale(locale)
  expect(dayjs('2020-01-01 03:00:00').locale('ar-ly').format('A')).toEqual('ص')
  expect(dayjs('2020-01-01 11:00:00').locale('ar-ly').format('A')).toEqual('ص')
  expect(dayjs('2020-01-01 16:00:00').locale('ar-ly').format('A')).toEqual('م')
  expect(dayjs('2020-01-01 20:00:00').locale('ar-ly').format('A')).toEqual('م')
})