Skip to content

Commit

Permalink
fix: partial character date formatting (#4321)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyjablonski authored Sep 12, 2024
1 parent ba699aa commit ceed376
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
14 changes: 13 additions & 1 deletion sites/partners/__tests__/lib/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Application } from "@bloom-housing/shared-helpers/src/types/backend-swagger"
import { mergeApplicationNames } from "../../src/lib/helpers"
import { createDate, mergeApplicationNames } from "../../src/lib/helpers"

describe("helpers", () => {
describe("mergeApplicationNames", () => {
Expand All @@ -23,4 +23,16 @@ describe("helpers", () => {
expect(mergeApplicationNames([])).toEqual("")
})
})
describe("createDate", () => {
it("should create dates with variable numbers of characters", () => {
expect(createDate({ year: "2025", month: "12", day: "10" })).toEqual(new Date(2025, 11, 10))
expect(createDate({ year: "2025", month: "1", day: "5" })).toEqual(new Date(2025, 0, 5))
expect(createDate({ year: "2025", month: "01", day: "05" })).toEqual(new Date(2025, 0, 5))
})
it("should fail with invalid input", () => {
expect(createDate({ year: "202", month: "12", day: "10" })).toBeFalsy()
expect(createDate({ year: "2025", month: "13", day: "10" })).toBeFalsy()
expect(createDate({ year: "2025", month: "13", day: "35" })).toBeFalsy()
})
})
})
6 changes: 3 additions & 3 deletions sites/partners/cypress/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,13 @@ Cypress.Commands.add("addMinimalListing", (listingName, isLottery, isApproval, j
cy.get("button").contains("Application Process").click()
if (isLottery) {
cy.getByID("reviewOrderLottery").check()
cy.getByTestId("lottery-start-date-month").type("12")
cy.getByTestId("lottery-start-date-month").type("1")
cy.getByTestId("lottery-start-date-day").type("17")
cy.getByTestId("lottery-start-date-year").type("2026")
cy.getByTestId("lottery-start-time-hours").type("10")
cy.getByTestId("lottery-start-time-hours").type("9")
cy.getByTestId("lottery-start-time-minutes").type("00")
cy.getByTestId("lottery-start-time-period").select("AM")
cy.getByTestId("lottery-end-time-hours").type("11")
cy.getByTestId("lottery-end-time-hours").type("10")
cy.getByTestId("lottery-end-time-minutes").type("00")
cy.getByTestId("lottery-end-time-period").select("AM")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const RankingsAndResults = ({ listing, isAdmin }: RankingsAndResultsProps) => {
}
: null
}
errorMessage={t("errors.requiredFieldError")}
errorMessage={t("errors.dateError")}
defaultDate={
errors?.lotteryDate
? null
Expand Down Expand Up @@ -184,7 +184,7 @@ const RankingsAndResults = ({ listing, isAdmin }: RankingsAndResultsProps) => {
watch={watch}
error={errors?.lotteryDate ? true : false}
strings={{
timeError: errors?.lotteryDate ? t("errors.requiredFieldError") : null,
timeError: errors?.lotteryDate ? t("errors.dateError") : null,
}}
defaultValues={
errors?.lotteryDate
Expand Down Expand Up @@ -214,7 +214,7 @@ const RankingsAndResults = ({ listing, isAdmin }: RankingsAndResultsProps) => {
watch={watch}
error={errors?.lotteryDate ? true : false}
strings={{
timeError: errors?.lotteryDate ? t("errors.requiredFieldError") : null,
timeError: errors?.lotteryDate ? t("errors.dateError") : null,
}}
defaultValues={
errors?.lotteryDate
Expand Down
13 changes: 11 additions & 2 deletions sites/partners/src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,18 @@ export const createTime = (
* Create Date object depending on DateField component
*/
export const createDate = (formDate: { year: string; month: string; day: string }) => {
if (!formDate || !formDate?.year || !formDate?.month || !formDate?.day) return null
const year = formDate?.year
let month = formDate?.month
let day = formDate?.day
if (!formDate || !year || !month || !day || year.length !== 4) return null

return dayjs(`${formDate.year}-${formDate.month}-${formDate.day}`, "YYYY-MM-DD").toDate()
if (day.length === 1) day = `0${day}`
if (month.length === 1) month = `0${month}`

const date = dayjs(`${year}-${month}-${day}`, "YYYY-MM-DD", true)
if (!date.isValid()) return null

return date.toDate()
}

interface FileUploaderParams {
Expand Down

0 comments on commit ceed376

Please sign in to comment.