2023-09-06 18:59:26 +00:00
|
|
|
import { screen, waitFor } from "@testing-library/react";
|
|
|
|
import userEvent from "@testing-library/user-event";
|
|
|
|
import * as API from "api/api";
|
|
|
|
import { UpdateTemplateMeta } from "api/typesGenerated";
|
|
|
|
import { Language as FooterFormLanguage } from "components/FormFooter/FormFooter";
|
2023-09-28 15:21:36 +00:00
|
|
|
import { MockTemplate } from "testHelpers/entities";
|
2023-03-23 19:43:12 +00:00
|
|
|
import {
|
|
|
|
renderWithTemplateSettingsLayout,
|
|
|
|
waitForLoaderToBeRemoved,
|
2023-09-28 15:21:36 +00:00
|
|
|
} from "testHelpers/renderHelpers";
|
2023-09-06 18:59:26 +00:00
|
|
|
import { getValidationSchema } from "./TemplateSettingsForm";
|
|
|
|
import { TemplateSettingsPage } from "./TemplateSettingsPage";
|
2022-12-07 16:32:39 +00:00
|
|
|
|
2023-05-05 21:46:51 +00:00
|
|
|
type FormValues = Required<
|
2023-11-20 19:16:18 +00:00
|
|
|
Omit<
|
|
|
|
UpdateTemplateMeta,
|
2024-02-13 07:00:35 +00:00
|
|
|
"default_ttl_ms" | "activity_bump_ms" | "max_ttl_ms" | "deprecation_message"
|
2023-11-20 19:16:18 +00:00
|
|
|
>
|
2023-09-06 18:59:26 +00:00
|
|
|
>;
|
2023-05-05 21:46:51 +00:00
|
|
|
|
|
|
|
const validFormValues: FormValues = {
|
2022-08-24 18:07:56 +00:00
|
|
|
name: "Name",
|
2022-11-14 20:11:50 +00:00
|
|
|
display_name: "A display name",
|
2022-08-24 18:07:56 +00:00
|
|
|
description: "A description",
|
2023-01-18 17:31:31 +00:00
|
|
|
icon: "vscode.png",
|
2022-11-21 10:43:53 +00:00
|
|
|
allow_user_cancel_workspace_jobs: false,
|
2023-05-05 21:46:51 +00:00
|
|
|
allow_user_autostart: false,
|
|
|
|
allow_user_autostop: false,
|
2023-08-29 18:35:05 +00:00
|
|
|
autostop_requirement: {
|
2023-07-20 13:35:41 +00:00
|
|
|
days_of_week: [],
|
|
|
|
weeks: 1,
|
|
|
|
},
|
2023-10-13 16:57:18 +00:00
|
|
|
autostart_requirement: {
|
|
|
|
days_of_week: [
|
|
|
|
"monday",
|
|
|
|
"tuesday",
|
|
|
|
"wednesday",
|
|
|
|
"thursday",
|
|
|
|
"friday",
|
|
|
|
"saturday",
|
|
|
|
"sunday",
|
|
|
|
],
|
|
|
|
},
|
2023-05-05 21:46:51 +00:00
|
|
|
failure_ttl_ms: 0,
|
2023-08-24 18:25:54 +00:00
|
|
|
time_til_dormant_ms: 0,
|
|
|
|
time_til_dormant_autodelete_ms: 0,
|
2023-08-22 20:15:13 +00:00
|
|
|
update_workspace_last_used_at: false,
|
2023-08-24 18:25:54 +00:00
|
|
|
update_workspace_dormant_at: false,
|
2023-10-18 22:07:21 +00:00
|
|
|
require_active_version: false,
|
2024-01-05 21:04:14 +00:00
|
|
|
disable_everyone_group_access: false,
|
2024-02-13 14:31:20 +00:00
|
|
|
max_port_share_level: "owner",
|
2023-09-06 18:59:26 +00:00
|
|
|
};
|
2022-08-24 18:07:56 +00:00
|
|
|
|
2023-03-06 18:36:19 +00:00
|
|
|
const renderTemplateSettingsPage = async () => {
|
2023-03-23 19:43:12 +00:00
|
|
|
renderWithTemplateSettingsLayout(<TemplateSettingsPage />, {
|
2023-03-06 18:36:19 +00:00
|
|
|
route: `/templates/${MockTemplate.name}/settings`,
|
|
|
|
path: `/templates/:template/settings`,
|
2023-09-06 18:59:26 +00:00
|
|
|
});
|
|
|
|
await waitForLoaderToBeRemoved();
|
|
|
|
};
|
2023-03-06 18:36:19 +00:00
|
|
|
|
2022-08-18 19:58:01 +00:00
|
|
|
const fillAndSubmitForm = async ({
|
|
|
|
name,
|
2022-11-14 20:11:50 +00:00
|
|
|
display_name,
|
2022-08-18 19:58:01 +00:00
|
|
|
description,
|
2022-08-19 16:09:07 +00:00
|
|
|
icon,
|
2022-11-21 10:43:53 +00:00
|
|
|
allow_user_cancel_workspace_jobs,
|
2023-05-05 21:46:51 +00:00
|
|
|
}: FormValues) => {
|
2023-09-11 17:17:58 +00:00
|
|
|
const nameField = await screen.findByLabelText("Name");
|
2023-09-06 18:59:26 +00:00
|
|
|
await userEvent.clear(nameField);
|
|
|
|
await userEvent.type(nameField, name);
|
2022-08-18 19:58:01 +00:00
|
|
|
|
2023-09-11 17:17:58 +00:00
|
|
|
const displayNameField = await screen.findByLabelText("Display name");
|
2023-09-06 18:59:26 +00:00
|
|
|
await userEvent.clear(displayNameField);
|
|
|
|
await userEvent.type(displayNameField, display_name);
|
2022-11-14 20:11:50 +00:00
|
|
|
|
2023-09-11 17:17:58 +00:00
|
|
|
const descriptionField = await screen.findByLabelText("Description");
|
2023-09-06 18:59:26 +00:00
|
|
|
await userEvent.clear(descriptionField);
|
|
|
|
await userEvent.type(descriptionField, description);
|
2022-08-18 19:58:01 +00:00
|
|
|
|
2023-09-11 17:17:58 +00:00
|
|
|
const iconField = await screen.findByLabelText("Icon");
|
2023-09-06 18:59:26 +00:00
|
|
|
await userEvent.clear(iconField);
|
|
|
|
await userEvent.type(iconField, icon);
|
2022-08-19 16:09:07 +00:00
|
|
|
|
2023-12-21 19:39:33 +00:00
|
|
|
const allowCancelJobsField = screen.getByRole("checkbox", {
|
2024-01-11 19:15:43 +00:00
|
|
|
name: /allow users to cancel in-progress workspace jobs/i,
|
2023-12-21 19:39:33 +00:00
|
|
|
});
|
2022-11-21 10:43:53 +00:00
|
|
|
// checkbox is checked by default, so it must be clicked to get unchecked
|
|
|
|
if (!allow_user_cancel_workspace_jobs) {
|
2023-09-06 18:59:26 +00:00
|
|
|
await userEvent.click(allowCancelJobsField);
|
2022-11-21 10:43:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 17:33:35 +00:00
|
|
|
const submitButton = await screen.findByText(
|
|
|
|
FooterFormLanguage.defaultSubmitLabel,
|
2023-09-06 18:59:26 +00:00
|
|
|
);
|
|
|
|
await userEvent.click(submitButton);
|
|
|
|
};
|
2022-08-18 19:58:01 +00:00
|
|
|
|
|
|
|
describe("TemplateSettingsPage", () => {
|
|
|
|
it("succeeds", async () => {
|
2023-09-06 18:59:26 +00:00
|
|
|
await renderTemplateSettingsPage();
|
2022-08-24 18:07:56 +00:00
|
|
|
jest.spyOn(API, "updateTemplateMeta").mockResolvedValueOnce({
|
|
|
|
...MockTemplate,
|
|
|
|
...validFormValues,
|
2023-09-06 18:59:26 +00:00
|
|
|
});
|
|
|
|
await fillAndSubmitForm(validFormValues);
|
|
|
|
await waitFor(() => expect(API.updateTemplateMeta).toBeCalledTimes(1));
|
|
|
|
});
|
2022-08-24 18:07:56 +00:00
|
|
|
|
|
|
|
it("allows a description of 128 chars", () => {
|
|
|
|
const values: UpdateTemplateMeta = {
|
|
|
|
...validFormValues,
|
|
|
|
description:
|
|
|
|
"Nam quis nulla. Integer malesuada. In in enim a arcu imperdiet malesuada. Sed vel lectus. Donec odio urna, tempus molestie, port",
|
2023-09-06 18:59:26 +00:00
|
|
|
};
|
|
|
|
const validate = () => getValidationSchema().validateSync(values);
|
|
|
|
expect(validate).not.toThrowError();
|
|
|
|
});
|
2022-08-24 18:07:56 +00:00
|
|
|
|
|
|
|
it("disallows a description of 128 + 1 chars", () => {
|
|
|
|
const values: UpdateTemplateMeta = {
|
|
|
|
...validFormValues,
|
|
|
|
description:
|
|
|
|
"Nam quis nulla. Integer malesuada. In in enim a arcu imperdiet malesuada. Sed vel lectus. Donec odio urna, tempus molestie, port a",
|
2023-09-06 18:59:26 +00:00
|
|
|
};
|
|
|
|
const validate = () => getValidationSchema().validateSync(values);
|
2024-01-11 19:15:43 +00:00
|
|
|
expect(validate).toThrowError();
|
2023-09-06 18:59:26 +00:00
|
|
|
});
|
|
|
|
});
|