-
Notifications
You must be signed in to change notification settings - Fork 1
fix(forms): stop VM disk selection being dropped on deploy #25
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
Aleksei Sviridkin (lexfrei)
merged 4 commits into
main
from
fix/vminstance-disk-select-race
Jun 1, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
29f8e03
fix(forms): stop VMDiskWidget dropping the selected disk on submit
lexfrei 5bb3f20
fix(forms): give StorageClassWidget an honest placeholder and stable …
lexfrei a0ec2ee
fix(forms): render an explicit placeholder in BackupClassWidget when …
lexfrei 3803066
fix(forms): validate every SchemaForm-backed form before submit
lexfrei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { describe, it, expect, vi } from "vitest" | ||
| import { screen, waitFor } from "@testing-library/react" | ||
| import userEvent from "@testing-library/user-event" | ||
| import type { WidgetProps } from "@rjsf/utils" | ||
| import type { K8sList } from "@cozystack/k8s-client" | ||
| import { BackupClassWidget } from "./BackupClassWidget.tsx" | ||
| import { createMockK8sClient } from "../test-utils/mock-k8s-client.ts" | ||
| import { renderWithK8sProvider } from "../test-utils/render.tsx" | ||
|
|
||
| interface BackupClass { | ||
| apiVersion: string | ||
| kind: string | ||
| metadata: { name: string } | ||
| } | ||
|
|
||
| function bc(name: string): BackupClass { | ||
| return { apiVersion: "backups.cozystack.io/v1alpha1", kind: "BackupClass", metadata: { name } } | ||
| } | ||
|
|
||
| function list(...items: BackupClass[]): K8sList<BackupClass> { | ||
| return { | ||
| apiVersion: "backups.cozystack.io/v1alpha1", | ||
| kind: "BackupClassList", | ||
| metadata: { resourceVersion: "1" }, | ||
| items, | ||
| } | ||
| } | ||
|
|
||
| type ListResult = | ||
| | K8sList<BackupClass> | ||
| | (() => K8sList<BackupClass> | Promise<K8sList<BackupClass>>) | ||
|
|
||
| function clientWith(result: ListResult) { | ||
| return createMockK8sClient({ | ||
| lists: [ | ||
| { apiGroup: "backups.cozystack.io", apiVersion: "v1alpha1", plural: "backupclasses", result }, | ||
| ], | ||
| }) | ||
| } | ||
|
|
||
| const NEVER_RESOLVES = () => new Promise<K8sList<BackupClass>>(() => {}) | ||
|
|
||
| function makeProps(overrides: Partial<WidgetProps> = {}): WidgetProps { | ||
| const base = { | ||
| id: "backupClassName", | ||
| name: "backupClassName", | ||
| label: "backupClassName", | ||
| value: undefined as unknown, | ||
| onChange: vi.fn(), | ||
| onBlur: vi.fn(), | ||
| onFocus: vi.fn(), | ||
| required: false, | ||
| disabled: false, | ||
| readonly: false, | ||
| autofocus: false, | ||
| placeholder: "", | ||
| options: {}, | ||
| schema: { type: "string" }, | ||
| uiSchema: {}, | ||
| formContext: {}, | ||
| rawErrors: [], | ||
| hideError: false, | ||
| multiple: false, | ||
| registry: {}, | ||
| } | ||
| return { ...base, ...overrides } as unknown as WidgetProps | ||
| } | ||
|
|
||
| describe("BackupClassWidget", () => { | ||
| it("shows an explicit placeholder instead of the first class when required and nothing is chosen", async () => { | ||
| renderWithK8sProvider( | ||
| <BackupClassWidget {...makeProps({ required: true })} />, | ||
| { client: clientWith(list(bc("s3"), bc("gcs"))) }, | ||
| ) | ||
|
|
||
| await screen.findByRole("option", { name: /^s3$/i }) | ||
|
|
||
| const select = screen.getByRole("combobox") as HTMLSelectElement | ||
| expect(select.value).toBe("") | ||
| expect(screen.getByRole("option", { name: /select a backup class/i })).toBeInTheDocument() | ||
| expect((screen.getByRole("option", { name: /^s3$/i }) as HTMLOptionElement).selected).toBe( | ||
| false, | ||
| ) | ||
| }) | ||
|
|
||
| it("keeps a committed value visible while the list is still loading", () => { | ||
| renderWithK8sProvider( | ||
| <BackupClassWidget {...makeProps({ required: true, value: "custom-bc" })} />, | ||
| { client: clientWith(NEVER_RESOLVES) }, | ||
| ) | ||
|
|
||
| const select = screen.getByRole("combobox") as HTMLSelectElement | ||
| expect(select.value).toBe("custom-bc") | ||
| expect(screen.getByRole("option", { name: /custom-bc/i })).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it("emits undefined when an optional selection is cleared", async () => { | ||
| const user = userEvent.setup() | ||
| const onChange = vi.fn() | ||
| renderWithK8sProvider( | ||
| <BackupClassWidget {...makeProps({ required: false, value: "s3", onChange })} />, | ||
| { client: clientWith(list(bc("s3"))) }, | ||
| ) | ||
|
|
||
| await screen.findByRole("option", { name: /^s3$/i }) | ||
| await user.selectOptions(screen.getByRole("combobox"), "") | ||
|
|
||
| await waitFor(() => expect(onChange).toHaveBeenLastCalledWith(undefined)) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
apps/console/src/components/StorageClassWidget.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { describe, it, expect, vi } from "vitest" | ||
| import { screen } from "@testing-library/react" | ||
| import type { WidgetProps } from "@rjsf/utils" | ||
| import type { K8sList } from "@cozystack/k8s-client" | ||
| import { StorageClassWidget } from "./StorageClassWidget.tsx" | ||
| import { createMockK8sClient } from "../test-utils/mock-k8s-client.ts" | ||
| import { renderWithK8sProvider } from "../test-utils/render.tsx" | ||
|
|
||
| const DEFAULT_ANNOTATION = "storageclass.kubernetes.io/is-default-class" | ||
|
|
||
| interface StorageClass { | ||
| apiVersion: string | ||
| kind: string | ||
| metadata: { name: string; annotations?: Record<string, string> } | ||
| provisioner: string | ||
| } | ||
|
|
||
| function sc(name: string, isDefault = false): StorageClass { | ||
| return { | ||
| apiVersion: "storage.k8s.io/v1", | ||
| kind: "StorageClass", | ||
| metadata: { | ||
| name, | ||
| annotations: isDefault ? { [DEFAULT_ANNOTATION]: "true" } : undefined, | ||
| }, | ||
| provisioner: "example.com/provisioner", | ||
| } | ||
| } | ||
|
|
||
| function list(...items: StorageClass[]): K8sList<StorageClass> { | ||
| return { | ||
| apiVersion: "storage.k8s.io/v1", | ||
| kind: "StorageClassList", | ||
| metadata: { resourceVersion: "1" }, | ||
| items, | ||
| } | ||
| } | ||
|
|
||
| type ListResult = | ||
| | K8sList<StorageClass> | ||
| | (() => K8sList<StorageClass> | Promise<K8sList<StorageClass>>) | ||
|
|
||
| function clientWith(result: ListResult) { | ||
| return createMockK8sClient({ | ||
| lists: [{ apiGroup: "storage.k8s.io", apiVersion: "v1", plural: "storageclasses", result }], | ||
| }) | ||
| } | ||
|
|
||
| const NEVER_RESOLVES = () => new Promise<K8sList<StorageClass>>(() => {}) | ||
|
|
||
| function makeProps(overrides: Partial<WidgetProps> = {}): WidgetProps { | ||
| const base = { | ||
| id: "storageClass", | ||
| name: "storageClass", | ||
| label: "storageClass", | ||
| value: undefined as unknown, | ||
| onChange: vi.fn(), | ||
| onBlur: vi.fn(), | ||
| onFocus: vi.fn(), | ||
| required: false, | ||
| disabled: false, | ||
| readonly: false, | ||
| autofocus: false, | ||
| placeholder: "", | ||
| options: {}, | ||
| schema: { type: "string" }, | ||
| uiSchema: {}, | ||
| formContext: {}, | ||
| rawErrors: [], | ||
| hideError: false, | ||
| multiple: false, | ||
| registry: {}, | ||
| } | ||
| return { ...base, ...overrides } as unknown as WidgetProps | ||
| } | ||
|
|
||
| describe("StorageClassWidget", () => { | ||
| it("shows an explicit placeholder instead of the first class when required and nothing is chosen", async () => { | ||
| const onChange = vi.fn() | ||
| renderWithK8sProvider( | ||
| <StorageClassWidget {...makeProps({ required: true, onChange })} />, | ||
| // No default-class annotation, so the auto-default effect stays idle and | ||
| // the value-less required state is observable. | ||
| { client: clientWith(list(sc("fast"), sc("slow"))) }, | ||
| ) | ||
|
|
||
| await screen.findByRole("option", { name: /^fast$/i }) | ||
|
|
||
| const select = screen.getByRole("combobox") as HTMLSelectElement | ||
| expect(select.value).toBe("") | ||
| expect(screen.getByRole("option", { name: /select a storage class/i })).toBeInTheDocument() | ||
| expect((screen.getByRole("option", { name: /^fast$/i }) as HTMLOptionElement).selected).toBe( | ||
| false, | ||
| ) | ||
| }) | ||
|
|
||
| it("keeps a committed value visible while the list is still loading", () => { | ||
| renderWithK8sProvider( | ||
| <StorageClassWidget {...makeProps({ required: true, value: "custom-sc" })} />, | ||
| { client: clientWith(NEVER_RESOLVES) }, | ||
| ) | ||
|
|
||
| const select = screen.getByRole("combobox") as HTMLSelectElement | ||
| expect(select.value).toBe("custom-sc") | ||
| expect(screen.getByRole("option", { name: /custom-sc/i })).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it("still auto-selects the cluster-default class on load when no value is set", async () => { | ||
| const onChange = vi.fn() | ||
| renderWithK8sProvider( | ||
| <StorageClassWidget {...makeProps({ onChange })} />, | ||
| { client: clientWith(list(sc("fast"), sc("standard", true))) }, | ||
| ) | ||
|
|
||
| await screen.findByRole("option", { name: /standard/i }) | ||
|
|
||
| expect(onChange).toHaveBeenCalledWith("standard") | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make
validate()fail closed when the RJSF ref is missing.This currently returns
trueonformRef.current === null, which means every caller will treat the form as valid and still submit if the inner ref ever fails to bind. Since this handle is now the submit gate, the safer fallback isfalse.Suggested fix
useImperativeHandle( ref, - () => ({ validate: () => formRef.current?.validateForm() ?? true }), + () => ({ validate: () => formRef.current?.validateForm() ?? false }), [], )🤖 Prompt for AI Agents