-
Notifications
You must be signed in to change notification settings - Fork 22
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
#2870 Add ability to pass in an array variable or template for dropdown field options #2915
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2daaddc
2870 using ArrayWidget for dropdown options in the Form Builder
BALEHOK e96fa49
2870 fixing preview
BALEHOK e16575b
2870 Pull fields and widgets from BS4 theme
BALEHOK 431b87b
Merge remote-tracking branch 'origin/main' into feature/2870-dd-varia…
BALEHOK 37f5531
Merge remote-tracking branch 'origin/main' into feature/2870-dd-varia…
BALEHOK de665d2
2870 Dropdown with labels
BALEHOK 5be51ed
2870 tests
BALEHOK 16deb54
2870 fixing types
BALEHOK d82da8c
2870 fixing types
BALEHOK d335cb8
2870 Fixing default and no option errors
BALEHOK cb408b7
Merge remote-tracking branch 'origin/main' into feature/2870-dd-varia…
BALEHOK 5761b04
2870 Dropdown text option tests
BALEHOK 573b231
2870 Enable var in Dropdown with labels
BALEHOK d9ba8b3
2870 removing options mapping
BALEHOK 8e0975c
Merge remote-tracking branch 'origin/main' into feature/2870-dd-varia…
BALEHOK f145e93
2870 Fixing merge conflicts
BALEHOK da2c141
2870 linting
BALEHOK 02b25da
Merge remote-tracking branch 'origin/main' into feature/2870-dd-varia…
BALEHOK 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 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 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,167 @@ | ||
/* | ||
* Copyright (C) 2022 PixieBrix, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { IBlock } from "@/core"; | ||
import { getExampleBlockConfig } from "@/pageEditor/tabs/editTab/exampleBlockConfigs"; | ||
import { | ||
createFormikTemplate, | ||
fireTextInput, | ||
selectSchemaFieldType, | ||
} from "@/tests/formHelpers"; | ||
import { waitForEffect } from "@/tests/testHelpers"; | ||
import { validateRegistryId } from "@/types/helpers"; | ||
import { render, RenderResult, screen } from "@testing-library/react"; | ||
import React from "react"; | ||
import { act } from "react-dom/test-utils"; | ||
import selectEvent from "react-select-event"; | ||
import registerDefaultWidgets from "@/components/fields/schemaFields/widgets/registerDefaultWidgets"; | ||
import FormBuilder from "./FormBuilder"; | ||
import { RJSFSchema } from "./formBuilderTypes"; | ||
|
||
let exampleFormSchema: RJSFSchema; | ||
let defaultFieldName: string; | ||
|
||
beforeAll(() => { | ||
registerDefaultWidgets(); | ||
const { schema, uiSchema } = getExampleBlockConfig({ | ||
id: validateRegistryId("@pixiebrix/form"), | ||
} as IBlock); | ||
exampleFormSchema = { | ||
schema, | ||
uiSchema, | ||
}; | ||
defaultFieldName = "notes"; | ||
}); | ||
|
||
function renderFormBuilder( | ||
formBuilderSchema: RJSFSchema = exampleFormSchema, | ||
activeField = defaultFieldName | ||
) { | ||
const initialValues = { | ||
form: formBuilderSchema, | ||
}; | ||
const FormikTemplate = createFormikTemplate(initialValues); | ||
return render( | ||
<FormikTemplate> | ||
<FormBuilder name="form" initialActiveField={activeField} /> | ||
</FormikTemplate> | ||
); | ||
} | ||
|
||
async function selectUiType(uiType: string) { | ||
await act(async () => | ||
selectEvent.select(screen.getByLabelText("Input Type"), uiType) | ||
); | ||
} | ||
|
||
describe("Dropdown field", () => { | ||
let rendered: RenderResult; | ||
beforeEach(async () => { | ||
rendered = renderFormBuilder(); | ||
|
||
// Switch to Dropdown widget | ||
await selectUiType("Dropdown"); | ||
}); | ||
|
||
test("doesn't fail when field type changed to Dropdown", async () => { | ||
// Expect the dropdown rendered in the preview | ||
expect( | ||
rendered.container.querySelector(`select#root_${defaultFieldName}`) | ||
).not.toBeNull(); | ||
}); | ||
|
||
test("can add an option", async () => { | ||
// Add a text option | ||
screen.getByText("Add Item").click(); | ||
const firstOption = rendered.container.querySelector( | ||
`[name="form.schema.properties.${defaultFieldName}.enum.0"]` | ||
); | ||
fireTextInput(firstOption, "Test option"); | ||
await waitForEffect(); | ||
|
||
// Expect the dropdown option rendered in the preview | ||
expect( | ||
screen.queryByRole("option", { name: "Test option" }) | ||
).not.toBeNull(); | ||
}); | ||
|
||
test("can use @var", async () => { | ||
// Switch to @var and inset "@data" | ||
await selectSchemaFieldType( | ||
`form.schema.properties.${defaultFieldName}.enum`, | ||
"var" | ||
); | ||
fireTextInput(screen.getByLabelText("Options"), "@data"); | ||
await waitForEffect(); | ||
|
||
// Expect the dropdown option rendered in the preview | ||
expect(screen.queryByRole("option", { name: "@data" })).not.toBeNull(); | ||
}); | ||
}); | ||
|
||
describe("Dropdown with labels field", () => { | ||
let rendered: RenderResult; | ||
beforeEach(async () => { | ||
rendered = renderFormBuilder(); | ||
|
||
// Switch to Dropdown widget | ||
await selectUiType("Dropdown with labels"); | ||
}); | ||
test("doesn't fail when field type changed to Dropdown with labels", async () => { | ||
// Expect the dropdown rendered in the preview | ||
expect( | ||
rendered.container.querySelector(`select#root_${defaultFieldName}`) | ||
).not.toBeNull(); | ||
}); | ||
|
||
test("can add an option", async () => { | ||
// Add a text option | ||
screen.getByText("Add Item").click(); | ||
|
||
// Set option value | ||
const firstOptionValueInput = rendered.container.querySelector( | ||
`[name="form.schema.properties.${defaultFieldName}.oneOf.0.const"]` | ||
); | ||
fireTextInput(firstOptionValueInput, "1"); | ||
await waitForEffect(); | ||
|
||
// Set option label | ||
const firstOptionLabelInput = rendered.container.querySelector( | ||
`[name="form.schema.properties.${defaultFieldName}.oneOf.0.title"]` | ||
); | ||
fireTextInput(firstOptionLabelInput, "Test option"); | ||
await waitForEffect(); | ||
|
||
// Validate the rendered option | ||
const optionElement = screen.queryByRole("option", { name: "Test option" }); | ||
expect(optionElement).not.toBeNull(); | ||
expect(optionElement).toHaveValue("1"); | ||
}); | ||
|
||
test("can use @var in Dropdown", async () => { | ||
// Switch to @var and inset "@data" | ||
await selectSchemaFieldType( | ||
`form.schema.properties.${defaultFieldName}.oneOf`, | ||
"var" | ||
); | ||
fireTextInput(screen.getByLabelText("Options"), "@data"); | ||
await waitForEffect(); | ||
|
||
// Expect the dropdown option rendered in the preview | ||
expect(screen.queryByRole("option", { name: "@data" })).not.toBeNull(); | ||
}); | ||
}); |
This file contains 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
File renamed without changes.
This file contains 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
File renamed without changes.
This file contains 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 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 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 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
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.
This should probably be be using joinName to correctly handle spaces/special characters in field names (unless we block them on the entry side)
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.
Hmm, this is a test file, do you think it can be a problem in the test?
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.
Oops, didn't notice the file name! This is perfectly ok here