From bfbabe121300d06d49c7120c87c2d8dc4b21dc9b Mon Sep 17 00:00:00 2001 From: Presley Pizzo Date: Tue, 9 Aug 2022 18:24:28 +0000 Subject: [PATCH] Format and fix tests --- .../WorkspaceScheduleForm.test.ts | 14 +- .../WorkspaceScheduleForm.tsx | 144 +++++++++--------- .../WorkspaceSchedulePage.test.tsx | 5 +- 3 files changed, 83 insertions(+), 80 deletions(-) diff --git a/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.test.ts b/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.test.ts index c3b2cf1f40..49d97a3c7c 100644 --- a/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.test.ts +++ b/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.test.ts @@ -1,7 +1,7 @@ import { + getValidationSchema, Language, ttlShutdownAt, - getValidationSchema, WorkspaceScheduleFormValues, } from "./WorkspaceScheduleForm" import { zones } from "./zones" @@ -21,7 +21,7 @@ const valid: WorkspaceScheduleFormValues = { } describe("validationSchema", () => { - it("allows everything to be falsy", () => { + it("allows everything to be falsy when switches are off", () => { const values: WorkspaceScheduleFormValues = { sunday: false, monday: false, @@ -35,7 +35,7 @@ describe("validationSchema", () => { timezone: "", ttl: 0, } - const validate = () => getValidationSchema(true, true).validateSync(values) + const validate = () => getValidationSchema(false, false).validateSync(values) expect(validate).not.toThrow() }) @@ -48,7 +48,7 @@ describe("validationSchema", () => { expect(validate).toThrow() }) - it("disallows all days-of-week to be false when startTime is set", () => { + it("disallows all days-of-week to be false when auto-start is enabled", () => { const values: WorkspaceScheduleFormValues = { ...valid, sunday: false, @@ -59,11 +59,11 @@ describe("validationSchema", () => { friday: false, saturday: false, } - const validate = () => getValidationSchema(true, true).validateSync(values) + const validate = () => getValidationSchema(true, false).validateSync(values) expect(validate).toThrowError(Language.errorNoDayOfWeek) }) - it("disallows empty startTime when at least one day is set", () => { + it("disallows empty startTime when auto-start is enabled", () => { const values: WorkspaceScheduleFormValues = { ...valid, sunday: false, @@ -75,7 +75,7 @@ describe("validationSchema", () => { saturday: false, startTime: "", } - const validate = () => getValidationSchema(true, true).validateSync(values) + const validate = () => getValidationSchema(true, false).validateSync(values) expect(validate).toThrowError(Language.errorNoTime) }) diff --git a/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.tsx b/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.tsx index 8fb8f4cf7c..3fda342cad 100644 --- a/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.tsx +++ b/site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.tsx @@ -21,7 +21,6 @@ import { AutoStart } from "pages/WorkspaceSchedulePage/schedule" import { AutoStop } from "pages/WorkspaceSchedulePage/ttl" import { FC } from "react" import * as Yup from "yup" -import { OptionalObjectSchema } from "yup/lib/object" import { getFormHelpersWithError } from "../../util/formUtils" import { FormFooter } from "../FormFooter/FormFooter" import { FullPageForm } from "../FullPageForm/FullPageForm" @@ -92,83 +91,84 @@ export interface WorkspaceScheduleFormValues { } // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types -export const getValidationSchema = (autoStartEnabled: boolean, autoStopEnabled: boolean) => (Yup.object({ - sunday: Yup.boolean(), - monday: Yup.boolean().test("at-least-one-day", Language.errorNoDayOfWeek, function (value) { - const parent = this.parent as WorkspaceScheduleFormValues - - if (!autoStartEnabled) { - return true - } else { - return ![ - parent.sunday, - value, - parent.tuesday, - parent.wednesday, - parent.thursday, - parent.friday, - parent.saturday, - ].every((day) => day === false) - } - }), - tuesday: Yup.boolean(), - wednesday: Yup.boolean(), - thursday: Yup.boolean(), - friday: Yup.boolean(), - saturday: Yup.boolean(), - - startTime: Yup.string() - .ensure() - .test("required-if-auto-start", Language.errorNoTime, function (value) { - if (autoStartEnabled) { - return value !== "" - } else { - return true - } - }) - .test("is-time-string", Language.errorTime, (value) => { - if (value === "") { - return true - } else if (!/^[0-9][0-9]:[0-9][0-9]$/.test(value)) { - return false - } else { - const parts = value.split(":") - const HH = Number(parts[0]) - const mm = Number(parts[1]) - return HH >= 0 && HH <= 23 && mm >= 0 && mm <= 59 - } - }), - timezone: Yup.string() - .ensure() - .test("is-timezone", Language.errorTimezone, function (value) { +export const getValidationSchema = (autoStartEnabled: boolean, autoStopEnabled: boolean) => + Yup.object({ + sunday: Yup.boolean(), + monday: Yup.boolean().test("at-least-one-day", Language.errorNoDayOfWeek, function (value) { const parent = this.parent as WorkspaceScheduleFormValues - if (!parent.startTime) { + if (!autoStartEnabled) { return true } else { - // Unfortunately, there's not a good API on dayjs at this time for - // evaluating a timezone. Attempt to parse today in the supplied timezone - // and return as valid if the function doesn't throw. - try { - dayjs.tz(dayjs(), value) + return ![ + parent.sunday, + value, + parent.tuesday, + parent.wednesday, + parent.thursday, + parent.friday, + parent.saturday, + ].every((day) => day === false) + } + }), + tuesday: Yup.boolean(), + wednesday: Yup.boolean(), + thursday: Yup.boolean(), + friday: Yup.boolean(), + saturday: Yup.boolean(), + + startTime: Yup.string() + .ensure() + .test("required-if-auto-start", Language.errorNoTime, function (value) { + if (autoStartEnabled) { + return value !== "" + } else { return true - } catch (e) { - return false } - } - }), - ttl: Yup.number() - .integer() - .min(0) - .max(24 * 7 /* 7 days */) - .test("positive-if-auto-stop", Language.errorNoStop, (value) => { - if (autoStopEnabled) { - return !!value - } else { - return true - } - }), -})) + }) + .test("is-time-string", Language.errorTime, (value) => { + if (value === "") { + return true + } else if (!/^[0-9][0-9]:[0-9][0-9]$/.test(value)) { + return false + } else { + const parts = value.split(":") + const HH = Number(parts[0]) + const mm = Number(parts[1]) + return HH >= 0 && HH <= 23 && mm >= 0 && mm <= 59 + } + }), + timezone: Yup.string() + .ensure() + .test("is-timezone", Language.errorTimezone, function (value) { + const parent = this.parent as WorkspaceScheduleFormValues + + if (!parent.startTime) { + return true + } else { + // Unfortunately, there's not a good API on dayjs at this time for + // evaluating a timezone. Attempt to parse today in the supplied timezone + // and return as valid if the function doesn't throw. + try { + dayjs.tz(dayjs(), value) + return true + } catch (e) { + return false + } + } + }), + ttl: Yup.number() + .integer() + .min(0) + .max(24 * 7 /* 7 days */) + .test("positive-if-auto-stop", Language.errorNoStop, (value) => { + if (autoStopEnabled) { + return !!value + } else { + return true + } + }), + }) export const WorkspaceScheduleForm: FC = ({ submitScheduleError, diff --git a/site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.test.tsx b/site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.test.tsx index 66923e03db..365d2540a9 100644 --- a/site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.test.tsx +++ b/site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.test.tsx @@ -1,8 +1,11 @@ +import { + formValuesToAutoStartRequest, + formValuesToTTLRequest, +} from "pages/WorkspaceSchedulePage/formToRequest" import { AutoStart, scheduleToAutoStart } from "pages/WorkspaceSchedulePage/schedule" import { AutoStop, ttlMsToAutoStop } from "pages/WorkspaceSchedulePage/ttl" import * as TypesGen from "../../api/typesGenerated" import { WorkspaceScheduleFormValues } from "../../components/WorkspaceScheduleForm/WorkspaceScheduleForm" -import { formValuesToAutoStartRequest, formValuesToTTLRequest } from "pages/WorkspaceSchedulePage/formToRequest" const validValues: WorkspaceScheduleFormValues = { sunday: false,