Back to Repositories

Testing Quarter Calculations and Manipulations in dayjs

This test suite validates the quarterOfYear plugin functionality in Day.js, ensuring accurate calendar quarter calculations and manipulations. The tests verify quarter-based operations including getting, setting, adding/subtracting quarters, and determining quarter boundaries.

Test Coverage Overview

The test suite provides comprehensive coverage of the quarterOfYear plugin functionality.

Key areas tested include:
  • Quarter determination for specific dates
  • Quarter boundary conditions (start/end of quarters)
  • Quarter manipulation operations
  • Integration with moment.js for result validation

Implementation Analysis

The testing approach utilizes Jest’s test framework with MockDate for consistent date handling. Tests are structured using direct comparisons between Day.js and Moment.js implementations to ensure compatibility.

Testing patterns include:
  • Explicit date string testing
  • Boundary condition verification
  • Method chaining validation
  • Cross-library result comparison

Technical Details

Testing infrastructure includes:
  • Jest as the testing framework
  • MockDate for date manipulation
  • Day.js with quarterOfYear plugin
  • Moment.js for comparison testing
  • ES6 module import system

Best Practices Demonstrated

The test suite exemplifies several testing best practices in JavaScript date handling.

Notable practices include:
  • Consistent test setup and teardown using beforeEach/afterEach
  • Precise timestamp testing
  • Comprehensive edge case coverage
  • Cross-validation with established libraries
  • Isolated test cases for specific functionality

iamkun/dayjs

test/plugin/quarterOfYear.test.js

            
import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import quarterOfYear from '../../src/plugin/quarterOfYear'

dayjs.extend(quarterOfYear)

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

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

it('get QuarterOfYear', () => {
  expect(dayjs('2013-01-01T00:00:00.000').quarter()).toBe(1)
  expect(dayjs('2013-04-01T00:00:00.000').subtract(1, 'ms').quarter()).toBe(1)
  expect(dayjs('2013-04-01T00:00:00.000').quarter()).toBe(2)
  expect(dayjs('2013-07-01T00:00:00.000').subtract(1, 'ms').quarter()).toBe(2)
  expect(dayjs('2013-07-01T00:00:00.000').quarter()).toBe(3)
  expect(dayjs('2013-10-01T00:00:00.000').subtract(1, 'ms').quarter()).toBe(3)
  expect(dayjs('2013-10-01T00:00:00.000').quarter()).toBe(4)
  expect(dayjs('2014-01-01T00:00:00.000').subtract(1, 'ms').quarter()).toBe(4)
})

it('set QuarterOfYear', () => {
  const d1 = '2013-01-01T00:00:00.000'
  expect(dayjs(d1).quarter(2).format())
    .toBe(moment(d1).quarter(2).format())
  const d2 = '2013-02-05T05:06:07.000'
  expect(dayjs(d2).quarter(2).format())
    .toBe(moment(d2).quarter(2).format())
  const d3 = '2018-11-25T05:06:07.000'
  expect(dayjs(d3).quarter(3).format())
    .toBe(moment(d3).quarter(3).format())
})

it('add subtract quarter', () => {
  expect(dayjs().add(2, 'quarter').format())
    .toBe(moment().add(2, 'quarter').format())
  expect(dayjs().subtract(2, 'quarter').format())
    .toBe(moment().subtract(2, 'quarter').format())
})

it('startOf endOf quarter', () => {
  expect(dayjs().startOf('quarter').format())
    .toBe(moment().startOf('quarter').format())
  expect(dayjs().endOf('quarter').format())
    .toBe(moment().endOf('quarter').format())
})