|
| 1 | +import { screen, waitFor } from "@testing-library/react" |
| 2 | +import userEvent from "@testing-library/user-event" |
| 3 | +import * as API from "api/api" |
| 4 | +import { UpdateTemplateMeta } from "api/typesGenerated" |
| 5 | +import { Language as FooterFormLanguage } from "components/FormFooter/FormFooter" |
| 6 | +import { MockTemplate } from "../../../testHelpers/entities" |
| 7 | +import { renderWithAuth } from "../../../testHelpers/renderHelpers" |
| 8 | +import { getValidationSchema } from "./TemplateScheduleForm" |
| 9 | +import TemplateSchedulePage from "./TemplateSchedulePage" |
| 10 | +import i18next from "i18next" |
| 11 | + |
| 12 | +const { t } = i18next |
| 13 | + |
| 14 | +const validFormValues = { |
| 15 | + name: "Name", |
| 16 | + display_name: "A display name", |
| 17 | + description: "A description", |
| 18 | + icon: "vscode.png", |
| 19 | + // these are the form values which are actually hours |
| 20 | + default_ttl_ms: 1, |
| 21 | + max_ttl_ms: 2, |
| 22 | + allow_user_cancel_workspace_jobs: false, |
| 23 | +} |
| 24 | + |
| 25 | +const renderTemplateSchedulePage = async () => { |
| 26 | + renderWithAuth(<TemplateSchedulePage />, { |
| 27 | + route: `/templates/${MockTemplate.name}/settings`, |
| 28 | + path: `/templates/:template/settings`, |
| 29 | + extraRoutes: [{ path: "templates/:template", element: <></> }], |
| 30 | + }) |
| 31 | + // Wait the form to be rendered |
| 32 | + const label = t("nameLabel", { ns: "TemplateSchedulePage" }) |
| 33 | + await screen.findAllByLabelText(label) |
| 34 | +} |
| 35 | + |
| 36 | +const fillAndSubmitForm = async ({ |
| 37 | + name, |
| 38 | + display_name, |
| 39 | + description, |
| 40 | + default_ttl_ms, |
| 41 | + max_ttl_ms, |
| 42 | + icon, |
| 43 | + allow_user_cancel_workspace_jobs, |
| 44 | +}: Required<UpdateTemplateMeta>) => { |
| 45 | + const label = t("nameLabel", { ns: "TemplateSchedulePage" }) |
| 46 | + const nameField = await screen.findByLabelText(label) |
| 47 | + await userEvent.clear(nameField) |
| 48 | + await userEvent.type(nameField, name) |
| 49 | + |
| 50 | + const displayNameLabel = t("displayNameLabel", { ns: "TemplateSchedulePage" }) |
| 51 | + |
| 52 | + const displayNameField = await screen.findByLabelText(displayNameLabel) |
| 53 | + await userEvent.clear(displayNameField) |
| 54 | + await userEvent.type(displayNameField, display_name) |
| 55 | + |
| 56 | + const descriptionLabel = t("descriptionLabel", { ns: "TemplateSchedulePage" }) |
| 57 | + const descriptionField = await screen.findByLabelText(descriptionLabel) |
| 58 | + await userEvent.clear(descriptionField) |
| 59 | + await userEvent.type(descriptionField, description) |
| 60 | + |
| 61 | + const iconLabel = t("iconLabel", { ns: "TemplateSchedulePage" }) |
| 62 | + const iconField = await screen.findByLabelText(iconLabel) |
| 63 | + await userEvent.clear(iconField) |
| 64 | + await userEvent.type(iconField, icon) |
| 65 | + |
| 66 | + const defaultTtlLabel = t("defaultTtlLabel", { ns: "TemplateSchedulePage" }) |
| 67 | + const defaultTtlField = await screen.findByLabelText(defaultTtlLabel) |
| 68 | + await userEvent.clear(defaultTtlField) |
| 69 | + await userEvent.type(defaultTtlField, default_ttl_ms.toString()) |
| 70 | + |
| 71 | + const entitlements = await API.getEntitlements() |
| 72 | + if (entitlements.features["advanced_template_scheduling"].enabled) { |
| 73 | + const maxTtlLabel = t("maxTtlLabel", { ns: "TemplateSchedulePage" }) |
| 74 | + const maxTtlField = await screen.findByLabelText(maxTtlLabel) |
| 75 | + await userEvent.clear(maxTtlField) |
| 76 | + await userEvent.type(maxTtlField, max_ttl_ms.toString()) |
| 77 | + } |
| 78 | + |
| 79 | + const allowCancelJobsField = screen.getByRole("checkbox") |
| 80 | + // checkbox is checked by default, so it must be clicked to get unchecked |
| 81 | + if (!allow_user_cancel_workspace_jobs) { |
| 82 | + await userEvent.click(allowCancelJobsField) |
| 83 | + } |
| 84 | + |
| 85 | + const submitButton = await screen.findByText( |
| 86 | + FooterFormLanguage.defaultSubmitLabel, |
| 87 | + ) |
| 88 | + await userEvent.click(submitButton) |
| 89 | +} |
| 90 | + |
| 91 | +describe("TemplateSchedulePage", () => { |
| 92 | + it("renders", async () => { |
| 93 | + const { t } = i18next |
| 94 | + const pageTitle = t("title", { |
| 95 | + ns: "TemplateSchedulePage", |
| 96 | + }) |
| 97 | + await renderTemplateSchedulePage() |
| 98 | + const element = await screen.findByText(pageTitle) |
| 99 | + expect(element).toBeDefined() |
| 100 | + }) |
| 101 | + |
| 102 | + it("succeeds", async () => { |
| 103 | + await renderTemplateSchedulePage() |
| 104 | + |
| 105 | + jest.spyOn(API, "updateTemplateMeta").mockResolvedValueOnce({ |
| 106 | + ...MockTemplate, |
| 107 | + ...validFormValues, |
| 108 | + }) |
| 109 | + await fillAndSubmitForm(validFormValues) |
| 110 | + |
| 111 | + await waitFor(() => expect(API.updateTemplateMeta).toBeCalledTimes(1)) |
| 112 | + }) |
| 113 | + |
| 114 | + test("ttl is converted to and from hours", async () => { |
| 115 | + await renderTemplateSchedulePage() |
| 116 | + |
| 117 | + jest.spyOn(API, "updateTemplateMeta").mockResolvedValueOnce({ |
| 118 | + ...MockTemplate, |
| 119 | + ...validFormValues, |
| 120 | + }) |
| 121 | + |
| 122 | + await fillAndSubmitForm(validFormValues) |
| 123 | + await waitFor(() => expect(API.updateTemplateMeta).toBeCalledTimes(1)) |
| 124 | + await waitFor(() => |
| 125 | + expect(API.updateTemplateMeta).toBeCalledWith( |
| 126 | + "test-template", |
| 127 | + expect.objectContaining({ |
| 128 | + ...validFormValues, |
| 129 | + // convert from the display value (hours) to ms |
| 130 | + default_ttl_ms: validFormValues.default_ttl_ms * 3600000, |
| 131 | + // this value is undefined if not entitled |
| 132 | + max_ttl_ms: undefined, |
| 133 | + }), |
| 134 | + ), |
| 135 | + ) |
| 136 | + }) |
| 137 | + |
| 138 | + it("allows a ttl of 7 days", () => { |
| 139 | + const values: UpdateTemplateMeta = { |
| 140 | + ...validFormValues, |
| 141 | + default_ttl_ms: 24 * 7, |
| 142 | + } |
| 143 | + const validate = () => getValidationSchema().validateSync(values) |
| 144 | + expect(validate).not.toThrowError() |
| 145 | + }) |
| 146 | + |
| 147 | + it("allows ttl of 0", () => { |
| 148 | + const values: UpdateTemplateMeta = { |
| 149 | + ...validFormValues, |
| 150 | + default_ttl_ms: 0, |
| 151 | + } |
| 152 | + const validate = () => getValidationSchema().validateSync(values) |
| 153 | + expect(validate).not.toThrowError() |
| 154 | + }) |
| 155 | + |
| 156 | + it("disallows a ttl of 7 days + 1 hour", () => { |
| 157 | + const values: UpdateTemplateMeta = { |
| 158 | + ...validFormValues, |
| 159 | + default_ttl_ms: 24 * 7 + 1, |
| 160 | + } |
| 161 | + const validate = () => getValidationSchema().validateSync(values) |
| 162 | + expect(validate).toThrowError( |
| 163 | + t("defaultTTLMaxError", { ns: "TemplateSchedulePage" }), |
| 164 | + ) |
| 165 | + }) |
| 166 | + |
| 167 | + it("allows a description of 128 chars", () => { |
| 168 | + const values: UpdateTemplateMeta = { |
| 169 | + ...validFormValues, |
| 170 | + description: |
| 171 | + "Nam quis nulla. Integer malesuada. In in enim a arcu imperdiet malesuada. Sed vel lectus. Donec odio urna, tempus molestie, port", |
| 172 | + } |
| 173 | + const validate = () => getValidationSchema().validateSync(values) |
| 174 | + expect(validate).not.toThrowError() |
| 175 | + }) |
| 176 | + |
| 177 | + it("disallows a description of 128 + 1 chars", () => { |
| 178 | + const values: UpdateTemplateMeta = { |
| 179 | + ...validFormValues, |
| 180 | + description: |
| 181 | + "Nam quis nulla. Integer malesuada. In in enim a arcu imperdiet malesuada. Sed vel lectus. Donec odio urna, tempus molestie, port a", |
| 182 | + } |
| 183 | + const validate = () => getValidationSchema().validateSync(values) |
| 184 | + expect(validate).toThrowError( |
| 185 | + t("descriptionMaxError", { ns: "TemplateSchedulePage" }), |
| 186 | + ) |
| 187 | + }) |
| 188 | +}) |
0 commit comments