Format and fix tests

This commit is contained in:
Presley Pizzo
2022-08-09 18:24:28 +00:00
parent 848732a285
commit bfbabe1213
3 changed files with 83 additions and 80 deletions
@@ -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)
})
@@ -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<WorkspaceScheduleFormProps> = ({
submitScheduleError,
@@ -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,