fix(site): preserve file path when building template version (#20481)

Fixes issue where clicking Build in the template editor would always
redirect to main.tf instead of keeping the currently open file.

Closes #14130

<!--

If you have used AI to produce some or all of this PR, please ensure you
have read our [AI Contribution
guidelines](https://coder.com/docs/about/contributing/AI_CONTRIBUTING)
before submitting.

-->
This commit is contained in:
jesmine
2025-10-27 11:03:15 -05:00
committed by GitHub
parent d9c40d61c2
commit ed3d6fa9e3
2 changed files with 37 additions and 6 deletions
@@ -255,6 +255,39 @@ test("The file is uploaded with the correct content type", async () => {
);
});
test("Preserves the currently open file path when building a template version", async () => {
const user = userEvent.setup();
const { router } = renderWithAuth(<TemplateVersionEditorPage />, {
route: `/templates/${MockTemplate.name}/versions/${MockTemplateVersion.name}/edit?path=myfile.tf`,
path: "/templates/:template/versions/:version/edit",
extraRoutes: [
{
path: "/templates/:templateId",
element: <div></div>,
},
],
});
const topbar = await screen.findByTestId("topbar");
const newTemplateVersion: TemplateVersion = {
...MockTemplateVersion,
id: "new-version-id",
name: "new-version",
};
await typeOnEditor("new content", user);
await buildTemplateVersion(newTemplateVersion, user, topbar);
// Verify that the path query parameter is preserved in the URL
await waitFor(() => {
expect(router.state.location.pathname).toBe(
`/templates/${MockTemplate.name}/versions/new-version/edit`,
);
});
expect(router.state.location.search).toBe("?path=myfile.tf");
});
describe.each([
{
testName: "Do not ask when template version has no errors",
@@ -104,12 +104,10 @@ const TemplateVersionEditorPage: FC = () => {
};
const navigateToVersion = (version: TemplateVersion) => {
return navigate(
`${getLink(linkToTemplate(organizationName, templateName))}/versions/${
version.name
}/edit`,
{ replace: true },
);
const url = `${getLink(linkToTemplate(organizationName, templateName))}/versions/${
version.name
}/edit?${searchParams.toString()}`;
return navigate(url, { replace: true });
};
const onBuildEnds = (newVersion: TemplateVersion) => {