Back to Repositories

Testing Date Comparison Plugin Implementation in dayjs

This test suite validates the minMax plugin functionality in Day.js, focusing on comparing and finding minimum and maximum dates. The tests verify both single and multiple date comparisons, handling of invalid dates, and edge cases with null values.

Test Coverage Overview

The test suite provides comprehensive coverage of the minMax plugin’s core functionality.

Key areas tested include:
  • Empty argument handling and null values
  • Single date operations
  • Multiple date comparisons
  • Array-based date comparisons
  • Invalid date handling
  • Edge cases with mixed valid and null arguments

Implementation Analysis

The testing approach utilizes Jest’s testing framework with MockDate for consistent date handling. The implementation follows a systematic pattern of testing both min() and max() methods with various input scenarios.

Technical patterns include:
  • Mock date management using beforeEach/afterEach hooks
  • Consistent date formatting for comparisons
  • Multiple assertion patterns for different input types

Technical Details

Testing infrastructure includes:
  • Jest as the primary testing framework
  • MockDate for date manipulation
  • Day.js plugin extension system
  • Format() method for consistent date string comparisons
  • Array and argument-based input testing

Best Practices Demonstrated

The test suite exhibits several testing best practices for date manipulation libraries.

Notable practices include:
  • Consistent test setup and teardown
  • Comprehensive edge case coverage
  • Clear test case isolation
  • Explicit expected vs actual value comparisons
  • Organized test structure with descriptive test cases

iamkun/dayjs

test/plugin/minMax.test.js

            
import MockDate from 'mockdate'
import dayjs from '../../src'
import minMax from '../../src/plugin/minMax'

dayjs.extend(minMax)

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

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

const arg1 = dayjs('2019-01-01')
const arg2 = dayjs('2018-01-01')
const arg3 = dayjs('2017-01-01')
const arg4 = dayjs('Invalid Date')

it('Return current time if no argument', () => {
  expect(dayjs.max())
    .toBe(null)
  expect(dayjs.min())
    .toBe(null)
  expect(dayjs.max(null))
    .toBe(null)
  expect(dayjs.min(null))
    .toBe(null)
})

it('Return current time if passing empty array', () => {
  expect(dayjs.max([]))
    .toBe(null)
  expect(dayjs.min([]))
    .toBe(null)
})

it('Compare between arguments', () => {
  expect(dayjs.max(arg1, arg2, arg3).format())
    .toBe(arg1.format())
  expect(dayjs.min(arg1, arg2, arg3).format())
    .toBe(arg3.format())
})

it('Compare in array', () => {
  expect(dayjs.max([arg1, arg2, arg3]).format())
    .toBe(arg1.format())
  expect(dayjs.min([arg1, arg2, arg3]).format())
    .toBe(arg3.format())
})

it('If Invalid Date return Invalid Date', () => {
  expect(dayjs.max(arg1, arg2, arg3, arg4).format())
    .toBe(arg4.format())
  expect(dayjs.min([arg1, arg2, arg3, arg4]).format())
    .toBe(arg4.format())
})

it('Ignore if exists an "null" argument', () => {
  expect(dayjs.max(null, null, arg1, arg2, null, arg3).format())
    .toBe(arg1.format())
  expect(dayjs.min([null, null, arg1, arg2, null, arg3]).format())
    .toBe(arg3.format())
})

it('Return the only date if just provided one argument', () => {
  expect(dayjs.max(arg1).format())
    .toBe(arg1.format())
  expect(dayjs.min([arg1]).format())
    .toBe(arg1.format())
})