Skip to content

Template settings fixes/kira pilot #3668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Aug 24, 2022
Prev Previous commit
Next Next commit
added ttl tests
  • Loading branch information
Kira-Pilot committed Aug 24, 2022
commit 45a41d1a2e88fd203e23c400bb0640de1c690b7a
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe("validationSchema", () => {
ttl: 24 * 7 + 1,
}
const validate = () => validationSchema.validateSync(values)
expect(validate).toThrowError("ttl must be less than or equal to 168")
expect(validate).toThrowError(Language.errorTtlMax)
})
})

Expand Down
12 changes: 10 additions & 2 deletions site/src/pages/TemplateSettingsPage/TemplateSettingsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ export const TemplateSettingsForm: FC<TemplateSettingsForm> = ({
initialValues: {
name: template.name,
description: template.description,
max_ttl_ms: template.max_ttl_ms,
// on display, convert from ms => hours
max_ttl_ms: template.max_ttl_ms / 3600000,
icon: template.icon,
},
validationSchema,
onSubmit,
onSubmit: (formData) => {
// on submit, convert from hours => ms
onSubmit({
...formData,
max_ttl_ms: formData.max_ttl_ms ? formData.max_ttl_ms * 3600000 : undefined,
})
},
initialTouched,
})
const getFieldHelpers = getFormHelpersWithError<UpdateTemplateMeta>(form, error)
Expand Down Expand Up @@ -152,6 +159,7 @@ export const TemplateSettingsForm: FC<TemplateSettingsForm> = ({
inputProps={{ min: 0, step: 1 }}
label={Language.maxTtlLabel}
variant="outlined"
type="number"
/>
</Stack>

Expand Down
36 changes: 11 additions & 25 deletions site/src/pages/TemplateSettingsPage/TemplateSettingsPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ const renderTemplateSettingsPage = async () => {
return renderResult
}

const validFormValues: UpdateTemplateMeta = {
name: "A name",
const validFormValues = {
name: "Name",
description: "A description",
icon: "A string",
max_ttl_ms: 24,
min_autostart_interval_ms: 24,
max_ttl_ms: 1,
}

const fillAndSubmitForm = async ({
Expand Down Expand Up @@ -63,46 +62,33 @@ describe("TemplateSettingsPage", () => {
it("succeeds", async () => {
await renderTemplateSettingsPage()

const newTemplateSettings = {
name: "edited-template-name",
description: "Edited description",
max_ttl_ms: 4000,
icon: "/icon/code.svg",
}
jest.spyOn(API, "updateTemplateMeta").mockResolvedValueOnce({
...MockTemplate,
...newTemplateSettings,
...validFormValues,
})
await fillAndSubmitForm(newTemplateSettings)
await fillAndSubmitForm(validFormValues)

await waitFor(() => expect(API.updateTemplateMeta).toBeCalledTimes(1))
})

test("ttl is converted to and from hours", async () => {
await renderTemplateSettingsPage()

const newTemplateSettings = {
name: "edited-template-name",
description: "Edited description",
max_ttl_ms: 1,
icon: "/icon/code.svg",
}

jest.spyOn(API, "updateTemplateMeta").mockResolvedValueOnce({
...MockTemplate,
...newTemplateSettings,
...validFormValues,
})

await fillAndSubmitForm(newTemplateSettings)
expect(screen.getByDisplayValue(1)).toBeInTheDocument()
await fillAndSubmitForm(validFormValues)
expect(screen.getByDisplayValue(1)).toBeInTheDocument() // the max_ttl_ms
await waitFor(() => expect(API.updateTemplateMeta).toBeCalledTimes(1))

await waitFor(() =>
expect(API.updateTemplateMeta).toBeCalledWith(
"test-template",
expect.objectContaining({
...newTemplateSettings,
max_ttl_ms: 3600000,
...validFormValues,
max_ttl_ms: 3600000, // the max_ttl_ms to ms
}),
),
)
Expand All @@ -123,6 +109,6 @@ describe("TemplateSettingsPage", () => {
max_ttl_ms: 24 * 7 + 1,
}
const validate = () => validationSchema.validateSync(values)
expect(validate).toThrowError("ttl must be less than or equal to 168")
expect(validate).toThrowError(FormLanguage.ttlMaxError)
})
})