/** * @file AlertsNotifications.test.tsx * @description Alert rules are editable through the create form (PATCH, type * fixed), and an invalid cooldown blocks saving instead of silently becoming 300. * @author Nguyễn Ngọc Trí Vĩ */ import { describe, it, expect, vi } from "vitest"; import { render, screen, waitFor } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { MemoryRouter } from "react-router-dom"; const update = vi.fn(async () => ({ rule: {} })); const create = vi.fn(async () => ({ rule: {} })); vi.mock("../../lib/api", () => ({ api: { alerts: { rules: { list: vi.fn(async () => ({ rules: [ { id: "r1", name: "Idle", rule_type: "inactivity", config: { minutes: 30 }, enabled: true, cooldown_seconds: 60, }, ], })), update: (...a: unknown[]) => update(...(a as [])), create: (...a: unknown[]) => create(...(a as [])), remove: vi.fn(), }, list: vi.fn(async () => ({ alerts: [], total: 0, unacked: 0 })), ack: vi.fn(), ackAll: vi.fn(), }, webhooks: { list: vi.fn(async () => ({ targets: [] })), providers: vi.fn(async () => ({ providers: [] })), }, }, })); import { AlertsNotifications } from "../AlertsNotifications"; const renderIt = () => render( ); describe("AlertsNotifications rules", () => { it("edits a rule in place and validates the cooldown", async () => { renderIt(); await userEvent.click(await screen.findByRole("button", { name: /edit rule/i })); const name = screen.getByDisplayValue("Idle"); const cooldown = screen.getByDisplayValue("60"); await userEvent.clear(cooldown); await userEvent.type(cooldown, "-5"); const save = screen.getByRole("button", { name: /save rule/i }); expect(save).toBeDisabled(); await userEvent.clear(cooldown); await userEvent.type(cooldown, "120"); await userEvent.clear(name); await userEvent.type(name, "Idle long"); await userEvent.click(save); await waitFor(() => expect(update).toHaveBeenCalledWith("r1", { name: "Idle long", config: { minutes: 30 }, cooldown_seconds: 120, }) ); expect(create).not.toHaveBeenCalled(); }, 15_000); // many typed fields: slow under a parallel run });