From adcba39052e48be5920ec5427a24af4baac22f57 Mon Sep 17 00:00:00 2001 From: Andrew Patton Date: Sun, 28 Apr 2024 17:03:25 -0700 Subject: [PATCH] Add tests for getMonthFromDate util --- packages/date-picker/src/utils.test.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/date-picker/src/utils.test.ts b/packages/date-picker/src/utils.test.ts index 5931c4eb..348e0367 100644 --- a/packages/date-picker/src/utils.test.ts +++ b/packages/date-picker/src/utils.test.ts @@ -2,8 +2,26 @@ import { describe, expect, it } from 'vitest'; import { getMonthFromDate, getMonthNameFromMonth } from './utils.js'; +const INVALID_DATE = new Date(''); + describe('@acusti/date-picker', () => { describe('utils', () => { + describe('getMonthFromDate', () => { + it('returns the correct month digit for a post-unix epoch month', () => { + expect(getMonthFromDate(new Date(1970, 0, 8))).toBe(0); // january 8, 1970 + expect(getMonthFromDate(new Date(1971, 11, 1))).toBe(23); // november 1, 1971 + }); + + it('returns the correct month digit for a pre-unix epoch month', () => { + expect(getMonthFromDate(new Date(1969, 11, 31))).toBe(-1); // december 31, 1969 + expect(getMonthFromDate(new Date(1968, 3, 30))).toBe(-21); // april 30, 1968 + }); + + it('returns NaN for an Invalid Date)', () => { + expect(getMonthFromDate(INVALID_DATE)).toBe(NaN); + }); + }); + describe('getMonthNameFromMonth', () => { it('returns the correct month name for a post-unix epoch month', () => { expect( @@ -13,12 +31,12 @@ describe('@acusti/date-picker', () => { it('returns the correct month name for a pre-unix epoch month', () => { expect( - getMonthNameFromMonth(getMonthFromDate(new Date(1865, 5, 19))), + getMonthNameFromMonth(getMonthFromDate(new Date(1865, 5, 2))), ).toBe('June'); }); it('returns an empty string if given NaN (e.g. if dealing with an Invalid Date)', () => { - expect(getMonthNameFromMonth(getMonthFromDate(new Date('')))).toBe(''); + expect(getMonthNameFromMonth(getMonthFromDate(INVALID_DATE))).toBe(''); }); }); });