Skip to content

chore(site): add tests for deprecate template flow #12685

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 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -236,24 +236,11 @@ export const TemplateSettingsForm: FC<TemplateSettingsForm> = ({
description="Deprecating a template prevents any new workspaces from being created. Existing workspaces will continue to function."
>
<FormFields>
<Stack direction="column" spacing={0.5}>
<Stack
direction="row"
alignItems="center"
spacing={0.5}
css={styles.optionText}
>
Deprecation Message
</Stack>
<span css={styles.optionHelperText}>
Leave the message empty to keep the template active. Any message
provided will mark the template as deprecated. Use this message to
inform users of the deprecation and how to migrate to a new
template.
</span>
</Stack>
<TextField
{...getFieldHelpers("deprecation_message")}
{...getFieldHelpers("deprecation_message", {
helperText:
"Leave the message empty to keep the template active. Any message provided will mark the template as deprecated. Use this message to inform users of the deprecation and how to migrate to a new template.",
})}
disabled={isSubmitting || !accessControlEnabled}
fullWidth
label="Deprecation Message"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { http, HttpResponse } from "msw";
import * as API from "api/api";
import type { UpdateTemplateMeta } from "api/typesGenerated";
import type { Template, UpdateTemplateMeta } from "api/typesGenerated";
import { Language as FooterFormLanguage } from "components/FormFooter/FormFooter";
import { MockTemplate } from "testHelpers/entities";
import { MockEntitlements, MockTemplate } from "testHelpers/entities";
import {
renderWithTemplateSettingsLayout,
waitForLoaderToBeRemoved,
} from "testHelpers/renderHelpers";
import { server } from "testHelpers/server";
import { getValidationSchema } from "./TemplateSettingsForm";
import { TemplateSettingsPage } from "./TemplateSettingsPage";

Expand Down Expand Up @@ -55,6 +57,9 @@ const renderTemplateSettingsPage = async () => {
renderWithTemplateSettingsLayout(<TemplateSettingsPage />, {
route: `/templates/${MockTemplate.name}/settings`,
path: `/templates/:template/settings`,
extraRoutes: [
{ path: "/templates/:template", element: <div>Template</div> },
],
});
await waitForLoaderToBeRemoved();
};
Expand Down Expand Up @@ -126,4 +131,68 @@ describe("TemplateSettingsPage", () => {
const validate = () => getValidationSchema().validateSync(values);
expect(validate).toThrowError();
});

describe("Deprecate template", () => {
it("deprecates a template when has access control", async () => {
server.use(
http.get("/api/v2/entitlements", () => {
return HttpResponse.json({
...MockEntitlements,
features: API.withDefaultFeatures({
access_control: { enabled: true, entitlement: "entitled" },
}),
});
}),
);
const updateTemplateMetaSpy = jest.spyOn(API, "updateTemplateMeta");
const deprecationMessage = "This template is deprecated";

await renderTemplateSettingsPage();
await deprecateTemplate(MockTemplate, deprecationMessage);

const [templateId, data] = updateTemplateMetaSpy.mock.calls[0];

expect(templateId).toEqual(MockTemplate.id);
expect(data).toEqual(
expect.objectContaining({ deprecation_message: deprecationMessage }),
);
});

it("does not deprecate a template when does not have access control", async () => {
server.use(
http.get("/api/v2/entitlements", () => {
return HttpResponse.json({
...MockEntitlements,
features: API.withDefaultFeatures({
access_control: { enabled: false, entitlement: "not_entitled" },
}),
});
}),
);
const updateTemplateMetaSpy = jest.spyOn(API, "updateTemplateMeta");

await renderTemplateSettingsPage();
await deprecateTemplate(
MockTemplate,
"This template should not be able to deprecate",
);

const [templateId, data] = updateTemplateMetaSpy.mock.calls[0];

expect(templateId).toEqual(MockTemplate.id);
expect(data).toEqual(
expect.objectContaining({ deprecation_message: "" }),
);
});
});
});

async function deprecateTemplate(template: Template, message: string) {
const deprecationField = screen.getByLabelText("Deprecation Message");
await userEvent.type(deprecationField, message);

const submitButton = await screen.findByText(
FooterFormLanguage.defaultSubmitLabel,
);
await userEvent.click(submitButton);
}