-
Notifications
You must be signed in to change notification settings - Fork 17k
Fix the multi type render for params #66278
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
Open
shubhamraj-git
wants to merge
6
commits into
apache:main
Choose a base branch
from
shubhamraj-git:fix-params
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c2c6378
fix the multi type
shubhamraj-git 89b3f65
schema check
shubhamraj-git ec5b83d
reviews
shubhamraj-git dc6eb5d
add example and fix rst
shubhamraj-git 6b62659
Merge branch 'main' into fix-params
shubhamraj-git 1c2879f
fix static checks
shubhamraj-git 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
260 changes: 260 additions & 0 deletions
260
airflow-core/src/airflow/ui/src/components/FlexibleForm/FieldMultiType.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,260 @@ | ||
| /*! | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| import { fireEvent, render, screen } from "@testing-library/react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| import { Wrapper } from "src/utils/Wrapper"; | ||
|
|
||
| import { FieldMultiType } from "./FieldMultiType"; | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| const mockParamsDict: Record<string, any> = {}; | ||
| const mockSetParamsDict = vi.fn(); | ||
|
|
||
| vi.mock("src/queries/useParamStore", () => ({ | ||
| paramPlaceholder: { | ||
| schema: { type: undefined }, | ||
| value: null, | ||
| }, | ||
| useParamStore: () => ({ | ||
| disabled: false, | ||
| paramsDict: mockParamsDict, | ||
| setParamsDict: mockSetParamsDict, | ||
| }), | ||
| })); | ||
|
|
||
| describe("FieldMultiType", () => { | ||
| beforeEach(() => { | ||
| Object.keys(mockParamsDict).forEach((key) => { | ||
| // eslint-disable-next-line @typescript-eslint/no-dynamic-delete | ||
| delete mockParamsDict[key]; | ||
| }); | ||
| mockSetParamsDict.mockClear(); | ||
| }); | ||
|
|
||
| describe("display", () => { | ||
| it("renders a textarea", () => { | ||
| mockParamsDict.test_param = { | ||
| schema: { type: ["string", "object"] }, | ||
| value: "nightly", | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| expect(screen.getByRole("textbox")).toBeDefined(); | ||
| }); | ||
|
|
||
| it("displays an object default as pretty-printed JSON", () => { | ||
| const obj = { name: "my_pipeline", retries: 3 }; | ||
|
|
||
| mockParamsDict.test_param = { | ||
| schema: { type: ["string", "object"] }, | ||
| value: obj, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| expect(screen.getByRole("textbox")).toHaveProperty("value", JSON.stringify(obj, undefined, 2)); | ||
| }); | ||
|
|
||
| it("displays a string default as-is", () => { | ||
| mockParamsDict.test_param = { | ||
| schema: { type: ["string", "object"] }, | ||
| value: "my_pipeline", | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| expect(screen.getByRole("textbox")).toHaveProperty("value", "my_pipeline"); | ||
| }); | ||
|
|
||
| it("displays a number default as a string", () => { | ||
| mockParamsDict.test_param = { | ||
| schema: { type: ["integer", "string"] }, | ||
| value: 42, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| expect(screen.getByRole("textbox")).toHaveProperty("value", "42"); | ||
| }); | ||
|
|
||
| it("displays a boolean default as a string", () => { | ||
| mockParamsDict.test_param = { | ||
| schema: { type: ["boolean", "string"] }, | ||
| value: true, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| expect(screen.getByRole("textbox")).toHaveProperty("value", "true"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("type resolution on change", () => { | ||
| it("stores a valid JSON object when schema includes 'object'", () => { | ||
| mockParamsDict.test_param = { | ||
| schema: { type: ["string", "object"] }, | ||
| value: {}, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: '{"key": "val"}' } }); | ||
| expect(mockParamsDict.test_param.value).toEqual({ key: "val" }); | ||
| }); | ||
|
|
||
| it("stores a plain string when JSON parse fails", () => { | ||
| mockParamsDict.test_param = { | ||
| schema: { type: ["string", "object"] }, | ||
| value: {}, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: "nightly" } }); | ||
| expect(mockParamsDict.test_param.value).toBe("nightly"); | ||
| }); | ||
|
|
||
| it("stores '45' as a string for type=['string','object'] — number not in schema", () => { | ||
| mockParamsDict.test_param = { | ||
| schema: { type: ["string", "object"] }, | ||
| value: {}, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: "45" } }); | ||
| expect(mockParamsDict.test_param.value).toBe("45"); | ||
| expect(typeof mockParamsDict.test_param.value).toBe("string"); | ||
| }); | ||
|
|
||
| it("stores 45 as a number for type=['integer','string']", () => { | ||
| mockParamsDict.test_param = { | ||
| schema: { type: ["integer", "string"] }, | ||
| value: "nightly", | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: "45" } }); | ||
| expect(mockParamsDict.test_param.value).toBe(45); | ||
| expect(typeof mockParamsDict.test_param.value).toBe("number"); | ||
| }); | ||
|
|
||
| it("stores a string for type=['integer','string'] when input is non-numeric text", () => { | ||
| mockParamsDict.test_param = { | ||
| schema: { type: ["integer", "string"] }, | ||
| value: 0, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: "nightly" } }); | ||
| expect(mockParamsDict.test_param.value).toBe("nightly"); | ||
| }); | ||
|
|
||
| it("stores true as boolean for type=['boolean','string']", () => { | ||
| mockParamsDict.test_param = { | ||
| schema: { type: ["boolean", "string"] }, | ||
| value: "pending", | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: "true" } }); | ||
| expect(mockParamsDict.test_param.value).toBe(true); | ||
| }); | ||
|
|
||
| it("stores a string for type=['number','string'] when input is not a number", () => { | ||
| mockParamsDict.test_param = { | ||
| schema: { type: ["number", "string"] }, | ||
| value: 0, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: "nightly" } }); | ||
| expect(mockParamsDict.test_param.value).toBe("nightly"); | ||
| }); | ||
|
|
||
| it("stores null on empty input", () => { | ||
| mockParamsDict.test_param = { | ||
| schema: { type: ["string", "object"] }, | ||
| value: "something", | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: "" } }); | ||
| expect(mockParamsDict.test_param.value).toBeNull(); | ||
| }); | ||
|
|
||
| it("calls onUpdate with the raw input string", () => { | ||
| const onUpdate = vi.fn(); | ||
|
|
||
| mockParamsDict.test_param = { | ||
| schema: { type: ["string", "object"] }, | ||
| value: {}, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={onUpdate} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: "nightly" } }); | ||
| expect(onUpdate).toHaveBeenCalledWith("nightly"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("validation errors for schemas without 'string'", () => { | ||
| it("signals error and preserves old value when input doesn't match type=['integer','object']", () => { | ||
| const onUpdate = vi.fn(); | ||
|
|
||
| mockParamsDict.test_param = { | ||
| schema: { type: ["integer", "object"] }, | ||
| value: 0, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={onUpdate} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: "nightly" } }); | ||
| expect(onUpdate).toHaveBeenCalledWith("", expect.stringContaining("integer")); | ||
| expect(mockParamsDict.test_param.value).toBe(0); | ||
| }); | ||
|
|
||
| it("accepts a valid integer for type=['integer','object']", () => { | ||
| const onUpdate = vi.fn(); | ||
|
|
||
| mockParamsDict.test_param = { | ||
| schema: { type: ["integer", "object"] }, | ||
| value: 0, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={onUpdate} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: "42" } }); | ||
| expect(onUpdate).toHaveBeenCalledWith("42"); | ||
| expect(mockParamsDict.test_param.value).toBe(42); | ||
| }); | ||
|
|
||
| it("accepts a valid JSON object for type=['integer','object']", () => { | ||
| const onUpdate = vi.fn(); | ||
|
|
||
| mockParamsDict.test_param = { | ||
| schema: { type: ["integer", "object"] }, | ||
| value: 0, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={onUpdate} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: '{"k": 1}' } }); | ||
| expect(onUpdate).toHaveBeenCalledWith('{"k": 1}'); | ||
| expect(mockParamsDict.test_param.value).toEqual({ k: 1 }); | ||
| }); | ||
|
|
||
| it("signals error for type=['boolean','object'] when input is neither", () => { | ||
| const onUpdate = vi.fn(); | ||
|
|
||
| mockParamsDict.test_param = { | ||
| schema: { type: ["boolean", "object"] }, | ||
| value: true, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={onUpdate} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: "nightly" } }); | ||
| expect(onUpdate).toHaveBeenCalledWith("", expect.stringContaining("boolean")); | ||
| expect(mockParamsDict.test_param.value).toBe(true); | ||
| }); | ||
|
|
||
| it("stores '4.5' as a string for type=['integer','string'] — float is not a valid integer", () => { | ||
| mockParamsDict.test_param = { | ||
| schema: { type: ["integer", "string"] }, | ||
| value: 0, | ||
| }; | ||
| render(<FieldMultiType name="test_param" onUpdate={vi.fn()} />, { wrapper: Wrapper }); | ||
| fireEvent.change(screen.getByRole("textbox"), { target: { value: "4.5" } }); | ||
| expect(mockParamsDict.test_param.value).toBe("4.5"); | ||
| expect(typeof mockParamsDict.test_param.value).toBe("string"); | ||
| }); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.