Skip to content

chore: enhance tests for TemplateSchedulePage #9801

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 8 commits into from
Sep 21, 2023
Merged
Changes from 1 commit
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
Next Next commit
chore: Add benchmark logs to test
  • Loading branch information
Parkreiner committed Sep 20, 2023
commit cfc53fb4d3f7ca6e0eb6338a4262f75ce4304b47
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*eslint eslint-comments/disable-enable-pair: error -- Remove after bottlenecks or causes of timeouts for testing have been figured out */
import { screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import * as API from "api/api";
Expand Down Expand Up @@ -36,49 +37,64 @@ const renderTemplateSchedulePage = async () => {
await waitForLoaderToBeRemoved();
};

// Extracts all properties from TemplateScheduleFormValues that have a key that
// ends in _ms, and makes those properties optional. Defined as mapped type to
// ensure this stays in sync as TemplateScheduleFormValues changes
type FillAndSubmitConfig = {
[Key in keyof TemplateScheduleFormValues as Key extends `${string}_ms`
? Key
: never]?: TemplateScheduleFormValues[Key] | undefined;
};

/*eslint-disable no-console -- Start benchmarks */
const fillAndSubmitForm = async ({
default_ttl_ms,
max_ttl_ms,
failure_ttl_ms,
time_til_dormant_ms,
time_til_dormant_autodelete_ms,
}: {
default_ttl_ms?: number;
max_ttl_ms?: number;
failure_ttl_ms?: number;
time_til_dormant_ms?: number;
time_til_dormant_autodelete_ms?: number;
}) => {
}: FillAndSubmitConfig) => {
console.time("form - full function");
const user = userEvent.setup();

console.time("form - ttl");
if (default_ttl_ms) {
const defaultTtlField = await screen.findByLabelText(
"Default autostop (hours)",
);
await user.clear(defaultTtlField);
await user.type(defaultTtlField, default_ttl_ms.toString());
}
console.timeEnd("form - ttl");

console.time("form - max_ttl");
if (max_ttl_ms) {
const maxTtlField = await screen.findByLabelText("Max lifetime (hours)");

await user.clear(maxTtlField);
await user.type(maxTtlField, max_ttl_ms.toString());
}
console.timeEnd("form - max_ttl");

console.time("form - failure_ttl");
if (failure_ttl_ms) {
const failureTtlField = screen.getByRole("checkbox", {
name: /Failure Cleanup/i,
});
await user.type(failureTtlField, failure_ttl_ms.toString());
}
console.timeEnd("form - failure_ttl");

console.time("form - dormant");
if (time_til_dormant_ms) {
const inactivityTtlField = screen.getByRole("checkbox", {
name: /Dormancy Threshold/i,
});
await user.type(inactivityTtlField, time_til_dormant_ms.toString());
}
console.timeEnd("form - dormant");

console.time("form - auto-delete");
if (time_til_dormant_autodelete_ms) {
const dormancyAutoDeletionField = screen.getByRole("checkbox", {
name: /Dormancy Auto-Deletion/i,
Expand All @@ -88,16 +104,24 @@ const fillAndSubmitForm = async ({
time_til_dormant_autodelete_ms.toString(),
);
}
console.timeEnd("form - auto-delete");

console.time("form - submit");
const submitButton = await screen.findByText(
FooterFormLanguage.defaultSubmitLabel,
);
await user.click(submitButton);
console.timeEnd("form - submit");

// User needs to confirm dormancy and autodeletion fields.
console.time("form - confirm");
// User needs to confirm dormancy and auto-deletion fields.
const confirmButton = await screen.findByTestId("confirm-button");
await user.click(confirmButton);
console.timeEnd("form - confirm");

console.timeEnd("form - full function");
};
/*eslint-enable no-console -- End benchmarks */

describe("TemplateSchedulePage", () => {
beforeEach(() => {
Expand All @@ -109,15 +133,16 @@ describe("TemplateSchedulePage", () => {
jest.spyOn(API, "getExperiments").mockResolvedValue(["workspace_actions"]);
});

it("succeeds", async () => {
it("Calls the API when user fills in and submits a form", async () => {
await renderTemplateSchedulePage();
jest.spyOn(API, "updateTemplateMeta").mockResolvedValueOnce({
...MockTemplate,
...validFormValues,
});

await fillAndSubmitForm(validFormValues);
await waitFor(() => expect(API.updateTemplateMeta).toBeCalledTimes(1));
});
}, 15_000);

test("default and max ttl is converted to and from hours", async () => {
await renderTemplateSchedulePage();
Expand Down