Skip to content

Commit

Permalink
Add unit tests - validateIfDateInFuture
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgain committed Jan 7, 2025
1 parent 4689403 commit 6fe0047
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/client/modules/Tasks/TaskForm/__test__/validators.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { addDays, subDays, format } from 'date-fns'

import { validateIfDateInFuture } from '../validators'

describe('validateIfDateInFuture', () => {
it('should return null for a date in the future (with day provided)', () => {
const futureDate = addDays(new Date(), 10)
const year = format(futureDate, 'yyyy')
const month = format(futureDate, 'MM')
const day = format(futureDate, 'dd')

const result = validateIfDateInFuture({ year, month, day })
expect(result).to.be.null
})

it('should return null for a date in the future (year and month only)', () => {
const futureDate = addDays(new Date(), 40) // Ensures crossing month boundary
const year = format(futureDate, 'yyyy')
const month = format(futureDate, 'MM')

const result = validateIfDateInFuture({ year, month })
expect(result).to.be.null
})

it('should return error message for a past date (with day provided)', () => {
const pastDate = subDays(new Date(), 10)
const year = format(pastDate, 'yyyy')
const month = format(pastDate, 'MM')
const day = format(pastDate, 'dd')

const result = validateIfDateInFuture({ year, month, day })
expect(result).to.equal('Enter a date in the future')
})

it('should return error message for a past date (year and month only)', () => {
const pastDate = subDays(new Date(), 40)
const year = format(pastDate, 'yyyy')
const month = format(pastDate, 'MM')

const result = validateIfDateInFuture({ year, month })
expect(result).to.equal('Enter a date in the future')
})

it('should handle edge cases correctly (today is not in the future)', () => {
const today = new Date()
const year = format(today, 'yyyy')
const month = format(today, 'MM')
const day = format(today, 'dd')

const result = validateIfDateInFuture({ year, month, day })
expect(result).to.equal('Enter a date in the future')
})

it('should handle invalid inputs gracefully', () => {
const invalidMessage = 'Enter a date in the future'

expect(validateIfDateInFuture({})).to.equal(invalidMessage)
expect(validateIfDateInFuture({ year: '2025' })).to.equal(invalidMessage)
expect(validateIfDateInFuture({ year: '2025', month: '13' })).to.equal(
invalidMessage
)
})
})

0 comments on commit 6fe0047

Please sign in to comment.