-
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.
Merge pull request #7445 from uktrade/refactor/dates
Refactor and simplify date utility functions
- Loading branch information
Showing
12 changed files
with
249 additions
and
72 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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 | ||
) | ||
}) | ||
}) |
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
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { isFuture } from 'date-fns' | ||
|
||
import { transformValueForAPI } from '../../../utils/date' | ||
import { parseDateWithYearMonth } from '../../../utils/date' | ||
|
||
export const validateIfDateInFuture = (values) => | ||
isFuture(transformValueForAPI(values)) ? null : 'Enter a date in the future' | ||
export const validateIfDateInFuture = ({ year, month, day }) => | ||
isFuture(parseDateWithYearMonth(year, month, day)) | ||
? null | ||
: 'Enter a date in the future' | ||
|
||
export const validateDaysRange = (value) => | ||
!value || value < 1 || value > 365 ? 'Enter a number between 1 and 365' : null |
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
Oops, something went wrong.