Skip to content

Commit bfbabe1

Browse files
committed
Format and fix tests
1 parent 848732a commit bfbabe1

File tree

3 files changed

+80
-77
lines changed

3 files changed

+80
-77
lines changed

site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
2+
getValidationSchema,
23
Language,
34
ttlShutdownAt,
4-
getValidationSchema,
55
WorkspaceScheduleFormValues,
66
} from "./WorkspaceScheduleForm"
77
import { zones } from "./zones"
@@ -21,7 +21,7 @@ const valid: WorkspaceScheduleFormValues = {
2121
}
2222

2323
describe("validationSchema", () => {
24-
it("allows everything to be falsy", () => {
24+
it("allows everything to be falsy when switches are off", () => {
2525
const values: WorkspaceScheduleFormValues = {
2626
sunday: false,
2727
monday: false,
@@ -35,7 +35,7 @@ describe("validationSchema", () => {
3535
timezone: "",
3636
ttl: 0,
3737
}
38-
const validate = () => getValidationSchema(true, true).validateSync(values)
38+
const validate = () => getValidationSchema(false, false).validateSync(values)
3939
expect(validate).not.toThrow()
4040
})
4141

@@ -48,7 +48,7 @@ describe("validationSchema", () => {
4848
expect(validate).toThrow()
4949
})
5050

51-
it("disallows all days-of-week to be false when startTime is set", () => {
51+
it("disallows all days-of-week to be false when auto-start is enabled", () => {
5252
const values: WorkspaceScheduleFormValues = {
5353
...valid,
5454
sunday: false,
@@ -59,11 +59,11 @@ describe("validationSchema", () => {
5959
friday: false,
6060
saturday: false,
6161
}
62-
const validate = () => getValidationSchema(true, true).validateSync(values)
62+
const validate = () => getValidationSchema(true, false).validateSync(values)
6363
expect(validate).toThrowError(Language.errorNoDayOfWeek)
6464
})
6565

66-
it("disallows empty startTime when at least one day is set", () => {
66+
it("disallows empty startTime when auto-start is enabled", () => {
6767
const values: WorkspaceScheduleFormValues = {
6868
...valid,
6969
sunday: false,
@@ -75,7 +75,7 @@ describe("validationSchema", () => {
7575
saturday: false,
7676
startTime: "",
7777
}
78-
const validate = () => getValidationSchema(true, true).validateSync(values)
78+
const validate = () => getValidationSchema(true, false).validateSync(values)
7979
expect(validate).toThrowError(Language.errorNoTime)
8080
})
8181

site/src/components/WorkspaceScheduleForm/WorkspaceScheduleForm.tsx

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import { AutoStart } from "pages/WorkspaceSchedulePage/schedule"
2121
import { AutoStop } from "pages/WorkspaceSchedulePage/ttl"
2222
import { FC } from "react"
2323
import * as Yup from "yup"
24-
import { OptionalObjectSchema } from "yup/lib/object"
2524
import { getFormHelpersWithError } from "../../util/formUtils"
2625
import { FormFooter } from "../FormFooter/FormFooter"
2726
import { FullPageForm } from "../FullPageForm/FullPageForm"
@@ -92,83 +91,84 @@ export interface WorkspaceScheduleFormValues {
9291
}
9392

9493
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
95-
export const getValidationSchema = (autoStartEnabled: boolean, autoStopEnabled: boolean) => (Yup.object({
96-
sunday: Yup.boolean(),
97-
monday: Yup.boolean().test("at-least-one-day", Language.errorNoDayOfWeek, function (value) {
98-
const parent = this.parent as WorkspaceScheduleFormValues
99-
100-
if (!autoStartEnabled) {
101-
return true
102-
} else {
103-
return ![
104-
parent.sunday,
105-
value,
106-
parent.tuesday,
107-
parent.wednesday,
108-
parent.thursday,
109-
parent.friday,
110-
parent.saturday,
111-
].every((day) => day === false)
112-
}
113-
}),
114-
tuesday: Yup.boolean(),
115-
wednesday: Yup.boolean(),
116-
thursday: Yup.boolean(),
117-
friday: Yup.boolean(),
118-
saturday: Yup.boolean(),
94+
export const getValidationSchema = (autoStartEnabled: boolean, autoStopEnabled: boolean) =>
95+
Yup.object({
96+
sunday: Yup.boolean(),
97+
monday: Yup.boolean().test("at-least-one-day", Language.errorNoDayOfWeek, function (value) {
98+
const parent = this.parent as WorkspaceScheduleFormValues
11999

120-
startTime: Yup.string()
121-
.ensure()
122-
.test("required-if-auto-start", Language.errorNoTime, function (value) {
123-
if (autoStartEnabled) {
124-
return value !== ""
125-
} else {
100+
if (!autoStartEnabled) {
126101
return true
127-
}
128-
})
129-
.test("is-time-string", Language.errorTime, (value) => {
130-
if (value === "") {
131-
return true
132-
} else if (!/^[0-9][0-9]:[0-9][0-9]$/.test(value)) {
133-
return false
134102
} else {
135-
const parts = value.split(":")
136-
const HH = Number(parts[0])
137-
const mm = Number(parts[1])
138-
return HH >= 0 && HH <= 23 && mm >= 0 && mm <= 59
103+
return ![
104+
parent.sunday,
105+
value,
106+
parent.tuesday,
107+
parent.wednesday,
108+
parent.thursday,
109+
parent.friday,
110+
parent.saturday,
111+
].every((day) => day === false)
139112
}
140113
}),
141-
timezone: Yup.string()
142-
.ensure()
143-
.test("is-timezone", Language.errorTimezone, function (value) {
144-
const parent = this.parent as WorkspaceScheduleFormValues
114+
tuesday: Yup.boolean(),
115+
wednesday: Yup.boolean(),
116+
thursday: Yup.boolean(),
117+
friday: Yup.boolean(),
118+
saturday: Yup.boolean(),
145119

146-
if (!parent.startTime) {
147-
return true
148-
} else {
149-
// Unfortunately, there's not a good API on dayjs at this time for
150-
// evaluating a timezone. Attempt to parse today in the supplied timezone
151-
// and return as valid if the function doesn't throw.
152-
try {
153-
dayjs.tz(dayjs(), value)
120+
startTime: Yup.string()
121+
.ensure()
122+
.test("required-if-auto-start", Language.errorNoTime, function (value) {
123+
if (autoStartEnabled) {
124+
return value !== ""
125+
} else {
154126
return true
155-
} catch (e) {
127+
}
128+
})
129+
.test("is-time-string", Language.errorTime, (value) => {
130+
if (value === "") {
131+
return true
132+
} else if (!/^[0-9][0-9]:[0-9][0-9]$/.test(value)) {
156133
return false
134+
} else {
135+
const parts = value.split(":")
136+
const HH = Number(parts[0])
137+
const mm = Number(parts[1])
138+
return HH >= 0 && HH <= 23 && mm >= 0 && mm <= 59
157139
}
158-
}
159-
}),
160-
ttl: Yup.number()
161-
.integer()
162-
.min(0)
163-
.max(24 * 7 /* 7 days */)
164-
.test("positive-if-auto-stop", Language.errorNoStop, (value) => {
165-
if (autoStopEnabled) {
166-
return !!value
167-
} else {
168-
return true
169-
}
170-
}),
171-
}))
140+
}),
141+
timezone: Yup.string()
142+
.ensure()
143+
.test("is-timezone", Language.errorTimezone, function (value) {
144+
const parent = this.parent as WorkspaceScheduleFormValues
145+
146+
if (!parent.startTime) {
147+
return true
148+
} else {
149+
// Unfortunately, there's not a good API on dayjs at this time for
150+
// evaluating a timezone. Attempt to parse today in the supplied timezone
151+
// and return as valid if the function doesn't throw.
152+
try {
153+
dayjs.tz(dayjs(), value)
154+
return true
155+
} catch (e) {
156+
return false
157+
}
158+
}
159+
}),
160+
ttl: Yup.number()
161+
.integer()
162+
.min(0)
163+
.max(24 * 7 /* 7 days */)
164+
.test("positive-if-auto-stop", Language.errorNoStop, (value) => {
165+
if (autoStopEnabled) {
166+
return !!value
167+
} else {
168+
return true
169+
}
170+
}),
171+
})
172172

173173
export const WorkspaceScheduleForm: FC<WorkspaceScheduleFormProps> = ({
174174
submitScheduleError,

site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import {
2+
formValuesToAutoStartRequest,
3+
formValuesToTTLRequest,
4+
} from "pages/WorkspaceSchedulePage/formToRequest"
15
import { AutoStart, scheduleToAutoStart } from "pages/WorkspaceSchedulePage/schedule"
26
import { AutoStop, ttlMsToAutoStop } from "pages/WorkspaceSchedulePage/ttl"
37
import * as TypesGen from "../../api/typesGenerated"
48
import { WorkspaceScheduleFormValues } from "../../components/WorkspaceScheduleForm/WorkspaceScheduleForm"
5-
import { formValuesToAutoStartRequest, formValuesToTTLRequest } from "pages/WorkspaceSchedulePage/formToRequest"
69

710
const validValues: WorkspaceScheduleFormValues = {
811
sunday: false,

0 commit comments

Comments
 (0)