|
| 1 | +import { describe, it, expect, vi } from "vitest" |
| 2 | +import { screen } from "@testing-library/react" |
| 3 | +import type { FieldProps } from "@rjsf/utils" |
| 4 | +import { SourceField } from "./SourceField.tsx" |
| 5 | +import { createMockK8sClient } from "../test-utils/mock-k8s-client.ts" |
| 6 | +import { renderWithK8sProvider } from "../test-utils/render.tsx" |
| 7 | + |
| 8 | +vi.mock("../lib/tenant-context.tsx", () => ({ |
| 9 | + useTenantContext: () => ({ |
| 10 | + tenants: [], |
| 11 | + selectedTenant: "root", |
| 12 | + selectTenant: () => {}, |
| 13 | + tenantNamespace: "tenant-root", |
| 14 | + isLoading: false, |
| 15 | + error: null, |
| 16 | + }), |
| 17 | +})) |
| 18 | + |
| 19 | +const schema = { |
| 20 | + type: "object", |
| 21 | + properties: { |
| 22 | + image: { type: "object", properties: { name: { type: "string" } } }, |
| 23 | + }, |
| 24 | +} |
| 25 | + |
| 26 | +function renderImagePicker() { |
| 27 | + const client = createMockK8sClient({ |
| 28 | + lists: [ |
| 29 | + { |
| 30 | + apiGroup: "core.cozystack.io", |
| 31 | + apiVersion: "v1alpha1", |
| 32 | + plural: "options", |
| 33 | + namespace: "tenant-root", |
| 34 | + result: { |
| 35 | + apiVersion: "core.cozystack.io/v1alpha1", |
| 36 | + kind: "OptionList", |
| 37 | + metadata: { resourceVersion: "1" }, |
| 38 | + items: [ |
| 39 | + { |
| 40 | + apiVersion: "core.cozystack.io/v1alpha1", |
| 41 | + kind: "Option", |
| 42 | + metadata: { name: "image" }, |
| 43 | + spec: { items: [{ value: "fedora" }] }, |
| 44 | + }, |
| 45 | + ], |
| 46 | + }, |
| 47 | + }, |
| 48 | + // A golden whose import failed still has its PVC; the image option |
| 49 | + // source leaves it out, a raw PVC listing would not. |
| 50 | + { |
| 51 | + apiGroup: "", |
| 52 | + apiVersion: "v1", |
| 53 | + plural: "persistentvolumeclaims", |
| 54 | + namespace: "cozy-public", |
| 55 | + result: { |
| 56 | + apiVersion: "v1", |
| 57 | + kind: "PersistentVolumeClaimList", |
| 58 | + metadata: { resourceVersion: "1" }, |
| 59 | + items: [ |
| 60 | + { apiVersion: "v1", kind: "PersistentVolumeClaim", metadata: { name: "vm-default-images-ubuntu", namespace: "cozy-public" } }, |
| 61 | + ], |
| 62 | + }, |
| 63 | + }, |
| 64 | + ], |
| 65 | + }) |
| 66 | + const props = { |
| 67 | + schema, |
| 68 | + formData: { image: { name: "" } }, |
| 69 | + onChange: vi.fn(), |
| 70 | + name: "source", |
| 71 | + required: false, |
| 72 | + idSchema: { $id: "root_source" }, |
| 73 | + } as unknown as FieldProps |
| 74 | + return renderWithK8sProvider(<SourceField {...props} />, { client }) |
| 75 | +} |
| 76 | + |
| 77 | +describe("SourceField image picker", () => { |
| 78 | + it("offers the images the image option source serves", async () => { |
| 79 | + renderImagePicker() |
| 80 | + expect(await screen.findByRole("option", { name: "fedora" })).toBeInTheDocument() |
| 81 | + expect(screen.queryByRole("option", { name: "ubuntu" })).not.toBeInTheDocument() |
| 82 | + }) |
| 83 | +}) |
0 commit comments