Files
prowler/ui/components/scans/schedule/scan-schedule-fields.test.tsx
T

77 lines
2.1 KiB
TypeScript

import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { useForm } from "react-hook-form";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { getScheduleFormDefaults } from "@/lib/schedules";
import type { ScheduleFormValues } from "@/types/schedules";
import { ScanScheduleFields } from "./scan-schedule-fields";
beforeAll(() => {
Object.defineProperty(HTMLElement.prototype, "hasPointerCapture", {
configurable: true,
value: vi.fn(() => false),
});
Object.defineProperty(HTMLElement.prototype, "setPointerCapture", {
configurable: true,
value: vi.fn(),
});
Object.defineProperty(HTMLElement.prototype, "releasePointerCapture", {
configurable: true,
value: vi.fn(),
});
Object.defineProperty(HTMLElement.prototype, "scrollIntoView", {
configurable: true,
value: vi.fn(),
});
});
function ScheduleFieldsHarness() {
const form = useForm<ScheduleFormValues>({
defaultValues: getScheduleFormDefaults(),
});
return (
<ScanScheduleFields
form={form}
showNextScheduledCopy
canUseAdvancedSchedule
/>
);
}
function getHelperCopy(text: RegExp) {
return screen.getByText((_, element) => {
return (
element?.tagName.toLowerCase() === "p" &&
text.test(element.textContent ?? "")
);
});
}
describe("ScanScheduleFields", () => {
it("updates the helper copy when the cadence changes to interval", async () => {
// Given
const user = userEvent.setup();
render(<ScheduleFieldsHarness />);
expect(getHelperCopy(/Daily/)).toBeInTheDocument();
// When
await user.click(screen.getByRole("combobox", { name: /repeats/i }));
await user.click(screen.getByRole("option", { name: /every 48 hours/i }));
// Then
expect(getHelperCopy(/Every 48 hours/)).toBeInTheDocument();
expect(
screen.queryByText((_, element) => {
return (
element?.tagName.toLowerCase() === "p" &&
/Daily/.test(element.textContent ?? "")
);
}),
).not.toBeInTheDocument();
});
});