Skip to content

Commit

Permalink
Add date unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgain committed Mar 17, 2024
1 parent b257b39 commit 95cdd35
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/client/utils/__test__/date.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import {
addDays,
areDatesEqual,
subtractYears,
subtractMonths,
getStartOfMonth,
getRandomDateInRange,
isWithinLastTwelveMonths,
convertDateToFieldDateObject,
convertDateToFieldShortDateObject,
getStartDateOfTwelveMonthsAgo,
} from '../date'

describe('convertDateToFieldShortDateObject', () => {
Expand Down Expand Up @@ -48,3 +56,76 @@ describe('convertDateToFieldDateObject', () => {
})
})
})

describe('getStartDateOfTwelveMonthsAgo', () => {
it(
'should return a date that is 12 months ago from the current ' +
'date and includes the 1st of the month',
() => {
const today = new Date()
const expectedDate = subtractMonths(getStartOfMonth(today), 12)
const actualDate = getStartDateOfTwelveMonthsAgo()
expect(areDatesEqual(actualDate, expectedDate)).to.equal(true)
}
)
})

describe('isWithinLastTwelveMonths', () => {
const twelveMonthsAgo = getStartDateOfTwelveMonthsAgo()
const twelveMonthsAgoAddOneDay = addDays(new Date(twelveMonthsAgo), 1)
const twelveMonthsAgoSubOneDay = subtractYears(new Date(twelveMonthsAgo), 1)
const today = new Date()
const tomorrow = addDays(today, 1)
const yesterday = subtractYears(today, 1)

it('should be valid for the 1st of the month twelve months ago', () => {
expect(isWithinLastTwelveMonths(twelveMonthsAgo)).to.equal(true)
})

it('should be valid for the 2nd of the month twelve months ago', () => {
expect(isWithinLastTwelveMonths(twelveMonthsAgoAddOneDay)).to.equal(true)
})

it('should be invalid one day before the 1st of the month, thirteen months ago', () => {
expect(isWithinLastTwelveMonths(twelveMonthsAgoSubOneDay)).to.equal(false)
})

it('should be valid for today', () => {
expect(isWithinLastTwelveMonths(today)).to.equal(true)
})

it('should be invalid for tomorrow', () => {
expect(isWithinLastTwelveMonths(tomorrow)).to.equal(false)
})

it('should be valid for yesterday', () => {
expect(isWithinLastTwelveMonths(yesterday)).to.equal(true)
})
})

describe('getRandomDateInRange', () => {
it('should return a random date within the specified range', () => {
const startDate = new Date(2024, 0, 1) // January 1, 2024
const endDate = new Date(2024, 0, 10) // January 10, 2024
const randomDate = getRandomDateInRange(startDate, endDate)
expect(randomDate).to.be.instanceOf(Date)
expect(randomDate.getTime()).to.be.at.least(startDate.getTime())
expect(randomDate.getTime()).to.be.at.most(endDate.getTime())
})

it('should throw an error if the start date and the end date are the same', () => {
const startDate = new Date(2024, 0, 1)
const endDate = new Date(2024, 0, 1)
expect(() => getRandomDateInRange(startDate, endDate)).to.throw(
'Start date and end date cannot be the same.'
)
})

it('should throw error if the start date is greater than the end date', () => {
const startDate = new Date(2024, 0, 10)
const endDate = new Date(2024, 0, 1)
expect(() => getRandomDateInRange(startDate, endDate)).to.throw(
'Start date cannot be greater than end date.'
)
})
})

0 comments on commit 95cdd35

Please sign in to comment.