|
| 1 | +import { forwardRef, useImperativeHandle } from "react" |
| 2 | +import { describe, it, expect, vi, beforeEach } from "vitest" |
| 3 | +import { render, screen } from "@testing-library/react" |
| 4 | +import userEvent from "@testing-library/user-event" |
| 5 | +import { MemoryRouter } from "react-router" |
| 6 | + |
| 7 | +// The Deploy button bypasses RJSF; in form mode the gate calls |
| 8 | +// SchemaForm.validate(), in YAML mode it is skipped. Mock SchemaForm so the |
| 9 | +// test drives that boolean directly, and YamlEditor so Monaco stays out. |
| 10 | +const h = vi.hoisted(() => ({ |
| 11 | + validateReturn: true, |
| 12 | + createMutateAsync: vi.fn(), |
| 13 | + updateMutateAsync: vi.fn(), |
| 14 | + ad: { |
| 15 | + spec: { |
| 16 | + application: { |
| 17 | + kind: "VirtualMachine", |
| 18 | + plural: "virtualmachines", |
| 19 | + singular: "virtualmachine", |
| 20 | + openAPISchema: JSON.stringify({ type: "object", properties: {} }), |
| 21 | + }, |
| 22 | + dashboard: {}, |
| 23 | + }, |
| 24 | + }, |
| 25 | +})) |
| 26 | + |
| 27 | +vi.mock("../lib/tenant-context.tsx", () => ({ |
| 28 | + useTenantContext: () => ({ tenantNamespace: "tenant-test" }), |
| 29 | +})) |
| 30 | +vi.mock("../lib/app-definitions.ts", () => ({ |
| 31 | + useApplicationDefinition: () => ({ data: h.ad, isLoading: false, error: null }), |
| 32 | + appDisplayName: () => "Virtual Machine", |
| 33 | + iconDataUrl: () => null, |
| 34 | +})) |
| 35 | +vi.mock("@cozystack/k8s-client", () => ({ |
| 36 | + useK8sCreate: () => ({ mutateAsync: h.createMutateAsync, isPending: false }), |
| 37 | + useK8sUpdate: () => ({ mutateAsync: h.updateMutateAsync, isPending: false }), |
| 38 | + K8sApiError: class K8sApiError extends Error {}, |
| 39 | +})) |
| 40 | +vi.mock("../components/SchemaForm.tsx", () => ({ |
| 41 | + SchemaForm: forwardRef<{ validate: () => boolean }, Record<string, unknown>>( |
| 42 | + function MockSchemaForm(_props, ref) { |
| 43 | + useImperativeHandle(ref, () => ({ validate: () => h.validateReturn })) |
| 44 | + return null |
| 45 | + }, |
| 46 | + ), |
| 47 | +})) |
| 48 | +vi.mock("../components/YamlEditor.tsx", () => ({ |
| 49 | + YamlEditor: ({ value }: { value: string }) => <textarea data-testid="yaml" readOnly value={value} />, |
| 50 | +})) |
| 51 | + |
| 52 | +const { ApplicationOrderPage } = await import("./ApplicationOrderPage.tsx") |
| 53 | + |
| 54 | +function renderPage() { |
| 55 | + return render( |
| 56 | + <MemoryRouter> |
| 57 | + <ApplicationOrderPage appNameOverride="virtualmachine" /> |
| 58 | + </MemoryRouter>, |
| 59 | + ) |
| 60 | +} |
| 61 | + |
| 62 | +describe("ApplicationOrderPage submit validation gate", () => { |
| 63 | + beforeEach(() => { |
| 64 | + h.createMutateAsync.mockReset() |
| 65 | + h.createMutateAsync.mockResolvedValue({}) |
| 66 | + h.updateMutateAsync.mockReset() |
| 67 | + h.updateMutateAsync.mockResolvedValue({}) |
| 68 | + h.validateReturn = true |
| 69 | + }) |
| 70 | + |
| 71 | + it("does not POST in form mode when the form fails RJSF validation", async () => { |
| 72 | + h.validateReturn = false |
| 73 | + const user = userEvent.setup() |
| 74 | + renderPage() |
| 75 | + |
| 76 | + await user.type(screen.getByRole("textbox"), "demo-vm") |
| 77 | + await user.click(screen.getByRole("button", { name: /^deploy$/i })) |
| 78 | + |
| 79 | + expect(h.createMutateAsync).not.toHaveBeenCalled() |
| 80 | + }) |
| 81 | + |
| 82 | + it("POSTs in form mode when the form passes validation", async () => { |
| 83 | + h.validateReturn = true |
| 84 | + const user = userEvent.setup() |
| 85 | + renderPage() |
| 86 | + |
| 87 | + await user.type(screen.getByRole("textbox"), "demo-vm") |
| 88 | + await user.click(screen.getByRole("button", { name: /^deploy$/i })) |
| 89 | + |
| 90 | + expect(h.createMutateAsync).toHaveBeenCalledTimes(1) |
| 91 | + }) |
| 92 | + |
| 93 | + it("skips the gate in YAML mode even when validation would fail", async () => { |
| 94 | + // In YAML mode the spec is hand-authored, so the gate must not block it. |
| 95 | + h.validateReturn = false |
| 96 | + const user = userEvent.setup() |
| 97 | + renderPage() |
| 98 | + |
| 99 | + await user.type(screen.getByRole("textbox"), "demo-vm") |
| 100 | + await user.click(screen.getByRole("button", { name: /yaml/i })) |
| 101 | + await user.click(screen.getByRole("button", { name: /^deploy$/i })) |
| 102 | + |
| 103 | + expect(h.createMutateAsync).toHaveBeenCalledTimes(1) |
| 104 | + }) |
| 105 | +}) |
0 commit comments