Back to Repositories

Validating Arabic Locale Meridiem Formatting in iamkun/dayjs

This test suite validates the Arabic (Morocco) locale functionality in the Day.js library, focusing on meridiem formatting. The tests ensure proper handling of time periods (AM/PM) in Arabic script for different times of day.

Test Coverage Overview

The test coverage focuses on meridiem (AM/PM) formatting in the Arabic (Morocco) locale. It verifies time period representations for morning (ص) and evening (م) across different hours.

  • Tests morning period (ص) for 3:00 and 11:00
  • Tests evening period (م) for 16:00 and 20:00
  • Validates correct Arabic character display

Implementation Analysis

The testing approach utilizes Jest’s assertion framework with MockDate for consistent time-based testing. The implementation employs dayjs locale plugins and format methods to verify Arabic meridiem formatting.

Key patterns include:
  • Mock date manipulation for consistent testing
  • Locale switching with dayjs.locale()
  • Format string ‘A’ for meridiem output

Technical Details

Testing infrastructure includes:
  • Jest testing framework
  • MockDate for date simulation
  • Day.js library with relativeTime plugin
  • Arabic (Morocco) locale configuration
  • BeforeEach/AfterEach hooks for test isolation

Best Practices Demonstrated

The test suite exemplifies strong testing practices through isolated test cases and proper test setup/teardown. It demonstrates effective use of mocking for time-dependent tests and clear assertion patterns.

  • Proper test isolation with MockDate
  • Consistent locale handling
  • Clear test case organization
  • Comprehensive time period coverage

iamkun/dayjs

test/locale/ar-ma.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-ma'

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-ma').format('A')).toEqual('ص')
  expect(dayjs('2020-01-01 11:00:00').locale('ar-ma').format('A')).toEqual('ص')
  expect(dayjs('2020-01-01 16:00:00').locale('ar-ma').format('A')).toEqual('م')
  expect(dayjs('2020-01-01 20:00:00').locale('ar-ma').format('A')).toEqual('م')
})