Skip to content

fix: redirect to new url after template name update #10926

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
Nov 29, 2023
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
30 changes: 29 additions & 1 deletion site/e2e/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ import { prometheusPort, pprofPort } from "./constants";
import { port } from "./playwright.config";
import * as ssh from "ssh2";
import { Duplex } from "stream";
import { WorkspaceBuildParameter } from "api/typesGenerated";
import {
WorkspaceBuildParameter,
UpdateTemplateMeta,
} from "api/typesGenerated";
import axios from "axios";
import capitalize from "lodash/capitalize";

// createWorkspace creates a workspace for a template.
// It does not wait for it to be running, but it does navigate to the page.
Expand Down Expand Up @@ -709,6 +713,30 @@ export const updateTemplate = async (
await uploaded.wait();
};

export const updateTemplateSettings = async (
page: Page,
templateName: string,
templateSettingValues: Pick<
UpdateTemplateMeta,
"name" | "display_name" | "description"
>,
) => {
await page.goto(`/templates/${templateName}/settings`, {
waitUntil: "domcontentloaded",
});
await expect(page).toHaveURL(`/templates/${templateName}/settings`);

for (const [key, value] of Object.entries(templateSettingValues)) {
Copy link
Member

@Parkreiner Parkreiner Nov 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's nothing wrong with the code right now, but there is a risk in the future from runtime/compile-time mismatches:

const myObj = {
  name: "whoops",
  obviouslyWrongValue: true
};

// No error
updateTemplateSettings(page, templateName, myObj);

Just to be on the safe side, and because this is a general test helper, I'm wondering if there should be a runtime check on the key to make sure that you're only ever processing name/display_name/description

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edited the message because I didn't like the way my tone sounded. Sorry about that

const labelText = capitalize(key).replace("_", " ");
await page.getByLabel(labelText, { exact: true }).fill(value);
}

await page.getByTestId("form-submit").click();

const name = templateSettingValues.name ?? templateName;
await expect(page).toHaveURL(`/templates/${name}`);
};

export const updateWorkspace = async (
page: Page,
workspaceName: string,
Expand Down
12 changes: 12 additions & 0 deletions site/e2e/tests/updateTemplate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { test } from "@playwright/test";
import { createTemplate, updateTemplateSettings } from "../helpers";

test("template update with new name redirects on successful submit", async ({
page,
}) => {
const templateName = await createTemplate(page);

await updateTemplateSettings(page, templateName, {
name: "new-name",
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ export const TemplateSettingsPage: FC = () => {
} = useMutation(
(data: UpdateTemplateMeta) => updateTemplateMeta(template.id, data),
{
onSuccess: async () => {
onSuccess: async (data) => {
// we use data.name because an admin may have updated templateName to something new
await queryClient.invalidateQueries(
templateByNameKey(orgId, templateName),
templateByNameKey(orgId, data.name),
);
displaySuccess("Template updated successfully");
navigate(`/templates/${data.name}`);
},
},
);
Expand Down