Skip to content

fix(site): fix infinity loading when template has no previous version #12059

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 1 commit into from
Feb 7, 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
2 changes: 1 addition & 1 deletion site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ export const getPreviousTemplateVersionByName = async (
organizationId: string,
templateName: string,
versionName: string,
): Promise<GetPreviousTemplateVersionByNameResponse> => {
) => {
try {
const response = await axios.get<TypesGen.TemplateVersion>(
`/api/v2/organizations/${organizationId}/templates/${templateName}/versions/${versionName}/previous`,
Expand Down
9 changes: 6 additions & 3 deletions site/src/api/queries/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,15 @@ export const previousTemplateVersion = (
versionName,
"previous",
],
queryFn: () =>
API.getPreviousTemplateVersionByName(
queryFn: async () => {
const result = await API.getPreviousTemplateVersionByName(
organizationId,
templateName,
versionName,
),
);

return result ?? null;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I need to do this because react-query does not allow queries to return undefined 😟

},
};
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { render, screen } from "@testing-library/react";
import { AppProviders } from "App";
import { RequireAuth } from "contexts/auth/RequireAuth";
import { RouterProvider, createMemoryRouter } from "react-router-dom";
import { TemplateLayout } from "../TemplateLayout";
import TemplateFilesPage from "./TemplateFilesPage";
import { MockTemplate } from "testHelpers/entities";
import { server } from "testHelpers/server";
import { rest } from "msw";

// Occasionally, Jest encounters HTML5 canvas errors. As the SyntaxHighlight is
// not required for these tests, we can safely mock it.
jest.mock("components/SyntaxHighlighter/SyntaxHighlighter", () => ({
SyntaxHighlighter: () => <div data-testid="syntax-highlighter" />,
}));

test("displays the template files even when there is no previous version", async () => {
server.use(
rest.get(
"/api/v2/organizations/:orgId/templates/:template/versions/:version/previous",
(req, res, ctx) => {
return res(ctx.status(404));
},
),
);

render(
<AppProviders>
<RouterProvider
router={createMemoryRouter(
[
{
element: <RequireAuth />,
children: [
{
element: <TemplateLayout />,
children: [
{
path: "/templates/:template/files",
element: <TemplateFilesPage />,
},
],
},
],
},
],
{
initialEntries: [`/templates/${MockTemplate.name}/files`],
},
)}
/>
</AppProviders>,
);

await screen.findByTestId("syntax-highlighter");
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,26 @@ const TemplateFilesPage: FC = () => {
const { data: currentFiles } = useQuery(
templateFiles(activeVersion.job.file_id),
);
const { data: previousTemplate } = useQuery(
const previousVersionQuery = useQuery(
previousTemplateVersion(orgId, template.name, activeVersion.name),
);
const previousVersion = previousVersionQuery.data;
const hasPreviousVersion =
previousVersionQuery.isSuccess && previousVersion !== null;
const { data: previousFiles } = useQuery({
...templateFiles(previousTemplate?.job.file_id ?? ""),
enabled: Boolean(previousTemplate),
...templateFiles(previousVersion?.job.file_id ?? ""),
enabled: hasPreviousVersion,
});
const shouldDisplayFiles =
currentFiles && (!hasPreviousVersion || previousFiles);

return (
<>
<Helmet>
<title>{getTemplatePageTitle("Source Code", template)}</title>
</Helmet>

{previousFiles && currentFiles ? (
{shouldDisplayFiles ? (
<TemplateFiles
currentFiles={currentFiles}
baseFiles={previousFiles}
Expand Down