forked from fleetdm/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fleet UI: Update Dropdown to use react-select 5.4 and other cleanup (f…
- Loading branch information
1 parent
9b4df20
commit 6b26b4d
Showing
15 changed files
with
568 additions
and
98 deletions.
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
5 changes: 3 additions & 2 deletions
5
frontend/components/forms/fields/Dropdown/DropdownOptionTooltipWrapper/_styles.scss
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
62 changes: 62 additions & 0 deletions
62
frontend/components/forms/fields/DropdownWrapper/DropdownWrapper.stories.tsx
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,62 @@ | ||
// stories/DropdownWrapper.stories.tsx | ||
|
||
import React from "react"; | ||
import { Meta, Story } from "@storybook/react"; | ||
import DropdownWrapper, { | ||
IDropdownWrapper, | ||
CustomOptionType, | ||
} from "./DropdownWrapper"; | ||
|
||
// Define metadata for the story | ||
export default { | ||
title: "Components/DropdownWrapper", | ||
component: DropdownWrapper, | ||
argTypes: { | ||
onChange: { action: "changed" }, | ||
}, | ||
} as Meta; | ||
|
||
// Define a template for the stories | ||
const Template: Story<IDropdownWrapper> = (args) => ( | ||
<DropdownWrapper {...args} /> | ||
); | ||
|
||
// Sample options to be used in the dropdown | ||
const sampleOptions: CustomOptionType[] = [ | ||
{ label: "Option 1", value: "option1", helpText: "Help text for option 1" }, | ||
{ | ||
label: "Option 2", | ||
value: "option2", | ||
tooltipContent: "Tooltip for option 2", | ||
}, | ||
{ label: "Option 3", value: "option3", isDisabled: true }, | ||
]; | ||
|
||
// Default story | ||
export const Default = Template.bind({}); | ||
Default.args = { | ||
options: sampleOptions, | ||
name: "dropdown-example", | ||
label: "Select an option", | ||
}; | ||
|
||
// Disabled story | ||
export const Disabled = Template.bind({}); | ||
Disabled.args = { | ||
...Default.args, | ||
isDisabled: true, | ||
}; | ||
|
||
// With Help Text story | ||
export const WithHelpText = Template.bind({}); | ||
WithHelpText.args = { | ||
...Default.args, | ||
helpText: "This is some help text for the dropdown", | ||
}; | ||
|
||
// With Error story | ||
export const WithError = Template.bind({}); | ||
WithError.args = { | ||
...Default.args, | ||
error: "This is an error message", | ||
}; |
102 changes: 102 additions & 0 deletions
102
frontend/components/forms/fields/DropdownWrapper/DropdownWrapper.tests.tsx
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,102 @@ | ||
import React from "react"; | ||
import { render, screen } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import DropdownWrapper, { CustomOptionType } from "./DropdownWrapper"; | ||
|
||
const sampleOptions: CustomOptionType[] = [ | ||
{ | ||
label: "Option 1", | ||
value: "option1", | ||
tooltipContent: "Tooltip 1", | ||
helpText: "Help text 1", | ||
}, | ||
{ | ||
label: "Option 2", | ||
value: "option2", | ||
tooltipContent: "Tooltip 2", | ||
helpText: "Help text 2", | ||
}, | ||
]; | ||
|
||
describe("DropdownWrapper Component", () => { | ||
const mockOnChange = jest.fn(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("renders with help text", () => { | ||
render( | ||
<DropdownWrapper | ||
options={sampleOptions} | ||
value="option1" | ||
onChange={mockOnChange} | ||
name="test-dropdown" | ||
label="Test Dropdown" | ||
helpText="This is a help text." | ||
/> | ||
); | ||
|
||
expect(screen.getByText(/test dropdown/i)).toBeInTheDocument(); | ||
expect(screen.getByText(/this is a help text/i)).toBeInTheDocument(); | ||
}); | ||
|
||
test("calls onChange when an option is selected", async () => { | ||
render( | ||
<DropdownWrapper | ||
options={sampleOptions} | ||
value="option1" | ||
onChange={mockOnChange} | ||
name="test-dropdown" | ||
label="Test Dropdown" | ||
placeholder="Choose option" | ||
/> | ||
); | ||
|
||
// Open the dropdown | ||
await userEvent.click(screen.getByText(/option 1/i)); | ||
|
||
// Select Option 2 | ||
await userEvent.click(screen.getByText(/option 2/i)); | ||
|
||
expect(mockOnChange).toHaveBeenCalledWith({ | ||
helpText: "Help text 2", | ||
label: "Option 2", | ||
tooltipContent: "Tooltip 2", | ||
value: "option2", | ||
}); | ||
}); | ||
|
||
test("renders error message when provided", () => { | ||
render( | ||
<DropdownWrapper | ||
options={sampleOptions} | ||
value="option1" | ||
onChange={mockOnChange} | ||
name="test-dropdown" | ||
label="Test Dropdown" | ||
error="This is an error message." | ||
/> | ||
); | ||
|
||
expect(screen.getByText(/this is an error message/i)).toBeInTheDocument(); | ||
}); | ||
|
||
test("displays no options message when no options are available", async () => { | ||
render( | ||
<DropdownWrapper | ||
options={[]} | ||
value="" | ||
onChange={mockOnChange} | ||
name="test-dropdown" | ||
label="Test Dropdown" | ||
placeholder="Choose option" | ||
/> | ||
); | ||
|
||
// Open dropdown | ||
await userEvent.click(screen.getByText(/choose option/i)); | ||
|
||
expect(screen.getByText(/no results found/i)).toBeInTheDocument(); | ||
}); | ||
}); |
Oops, something went wrong.