-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests - validateIfDateInFuture
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/client/modules/Tasks/TaskForm/__test__/validators.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
}) | ||
}) |