Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
test(form): Added new for menu item tests
- Loading branch information
Showing
10 changed files
with
1,035 additions
and
5 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
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,47 @@ | ||
import React, { useState } from "react"; | ||
import { fireEvent, render, waitFor } from "@testing-library/react"; | ||
import { DropdownMenu } from "@react-md/menu"; | ||
|
||
import { MenuItemCheckbox } from "../MenuItemCheckbox"; | ||
|
||
describe("MenuItemCheckbox", () => { | ||
it("should update the state and close the menu once clicked", () => { | ||
function Test() { | ||
const [checked, setChecked] = useState(false); | ||
|
||
return ( | ||
<DropdownMenu | ||
id="menu-id" | ||
items={[ | ||
<MenuItemCheckbox | ||
key="item-1" | ||
id="menu-id-1" | ||
checked={checked} | ||
onCheckedChange={(nextChecked) => setChecked(nextChecked)} | ||
> | ||
Checkbox | ||
</MenuItemCheckbox>, | ||
]} | ||
> | ||
Button | ||
</DropdownMenu> | ||
); | ||
} | ||
const { getByRole } = render(<Test />); | ||
const button = getByRole("button", { name: "Button" }); | ||
|
||
fireEvent.click(button); | ||
|
||
let checkbox = getByRole("menuitemcheckbox", { name: "Checkbox" }); | ||
expect(checkbox).toHaveAttribute("aria-checked", "false"); | ||
expect(checkbox).toMatchSnapshot(); | ||
|
||
fireEvent.click(checkbox); | ||
waitFor(() => expect(checkbox).not.toBeInTheDocument()); | ||
|
||
fireEvent.click(button); | ||
checkbox = getByRole("menuitemcheckbox", { name: "Checkbox" }); | ||
expect(checkbox).toHaveAttribute("aria-checked", "true"); | ||
expect(checkbox).toMatchSnapshot(); | ||
}); | ||
}); |
164 changes: 164 additions & 0 deletions
164
packages/form/src/menu/__tests__/MenuItemInputToggle.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,164 @@ | ||
import React, { useState } from "react"; | ||
import { fireEvent, render, waitFor } from "@testing-library/react"; | ||
import { DropdownMenu } from "@react-md/menu"; | ||
|
||
import { | ||
BaseMenuItemInputToggleProps, | ||
MenuItemInputToggle, | ||
} from "../MenuItemInputToggle"; | ||
|
||
function Test({ | ||
iconAfter, | ||
addon, | ||
addonType, | ||
addonPosition, | ||
onClick, | ||
}: Partial<BaseMenuItemInputToggleProps>) { | ||
const [checkboxChecked, setCheckboxChecked] = useState(false); | ||
const [switchChecked, setSwitchChecked] = useState(false); | ||
const [selectedIndex, setSelectedIndex] = useState(0); | ||
|
||
return ( | ||
<DropdownMenu | ||
id="menu-id" | ||
items={[ | ||
<MenuItemInputToggle | ||
key="checkbox-1" | ||
id="menu-id-1" | ||
type="checkbox" | ||
checked={checkboxChecked} | ||
onCheckedChange={(nextChecked) => setCheckboxChecked(nextChecked)} | ||
onClick={onClick} | ||
iconAfter={iconAfter} | ||
addon={addon} | ||
addonType={addonType} | ||
addonPosition={addonPosition} | ||
> | ||
Checkbox | ||
</MenuItemInputToggle>, | ||
<MenuItemInputToggle | ||
type="radio" | ||
key="radio-1" | ||
id="menu-id-2" | ||
onClick={onClick} | ||
checked={selectedIndex === 0} | ||
onCheckedChange={() => { | ||
setSelectedIndex(0); | ||
}} | ||
iconAfter={iconAfter} | ||
addon={addon} | ||
addonType={addonType} | ||
addonPosition={addonPosition} | ||
> | ||
Radio 1 | ||
</MenuItemInputToggle>, | ||
<MenuItemInputToggle | ||
type="radio" | ||
key="radio-2" | ||
id="menu-id-3" | ||
onClick={onClick} | ||
checked={selectedIndex === 1} | ||
onCheckedChange={() => { | ||
setSelectedIndex(1); | ||
}} | ||
iconAfter={iconAfter} | ||
addon={addon} | ||
addonType={addonType} | ||
addonPosition={addonPosition} | ||
> | ||
Radio 2 | ||
</MenuItemInputToggle>, | ||
<MenuItemInputToggle | ||
key="switch" | ||
type="switch" | ||
id="menu-id-4" | ||
checked={switchChecked} | ||
onCheckedChange={(nextChecked) => setSwitchChecked(nextChecked)} | ||
onClick={onClick} | ||
iconAfter={iconAfter} | ||
addon={addon} | ||
addonType={addonType} | ||
addonPosition={addonPosition} | ||
> | ||
Switch | ||
</MenuItemInputToggle>, | ||
]} | ||
> | ||
Button | ||
</DropdownMenu> | ||
); | ||
} | ||
|
||
describe("MenuItemInputToggle", () => { | ||
it("should render correctly", () => { | ||
const { getByRole, rerender } = render(<Test />); | ||
const button = getByRole("button", { name: "Button" }); | ||
fireEvent.click(button); | ||
|
||
let checkbox = getByRole("menuitemcheckbox", { name: "Checkbox" }); | ||
let radio1 = getByRole("menuitemradio", { name: "Radio 1" }); | ||
let radio2 = getByRole("menuitemradio", { name: "Radio 2" }); | ||
let switchEl = getByRole("menuitemcheckbox", { name: "Switch" }); | ||
expect(checkbox).toMatchSnapshot(); | ||
expect(checkbox).toHaveAttribute("aria-checked", "false"); | ||
expect(radio1).toMatchSnapshot(); | ||
expect(radio1).toHaveAttribute("aria-checked", "true"); | ||
expect(radio2).toMatchSnapshot(); | ||
expect(radio2).toHaveAttribute("aria-checked", "false"); | ||
expect(switchEl).toMatchSnapshot(); | ||
expect(switchEl).toHaveAttribute("aria-checked", "false"); | ||
|
||
fireEvent.click(checkbox); | ||
waitFor(() => expect(checkbox).not.toBeInTheDocument()); | ||
|
||
fireEvent.click(button); | ||
checkbox = getByRole("menuitemcheckbox", { name: "Checkbox" }); | ||
radio1 = getByRole("menuitemradio", { name: "Radio 1" }); | ||
radio2 = getByRole("menuitemradio", { name: "Radio 2" }); | ||
switchEl = getByRole("menuitemcheckbox", { name: "Switch" }); | ||
expect(checkbox).toMatchSnapshot(); | ||
expect(checkbox).toHaveAttribute("aria-checked", "true"); | ||
expect(radio1).toMatchSnapshot(); | ||
expect(radio1).toHaveAttribute("aria-checked", "true"); | ||
expect(radio2).toMatchSnapshot(); | ||
expect(radio2).toHaveAttribute("aria-checked", "false"); | ||
expect(switchEl).toMatchSnapshot(); | ||
expect(switchEl).toHaveAttribute("aria-checked", "false"); | ||
|
||
rerender(<Test iconAfter />); | ||
fireEvent.click(button); | ||
checkbox = getByRole("menuitemcheckbox", { name: "Checkbox" }); | ||
radio1 = getByRole("menuitemradio", { name: "Radio 1" }); | ||
radio2 = getByRole("menuitemradio", { name: "Radio 2" }); | ||
switchEl = getByRole("menuitemcheckbox", { name: "Switch" }); | ||
expect(checkbox).toMatchSnapshot(); | ||
expect(checkbox).toHaveAttribute("aria-checked", "true"); | ||
expect(radio1).toMatchSnapshot(); | ||
expect(radio1).toHaveAttribute("aria-checked", "true"); | ||
expect(radio2).toMatchSnapshot(); | ||
expect(radio2).toHaveAttribute("aria-checked", "false"); | ||
expect(switchEl).toMatchSnapshot(); | ||
expect(switchEl).toHaveAttribute("aria-checked", "false"); | ||
|
||
rerender( | ||
<Test | ||
addon={<span data-testid="addon" />} | ||
addonType="media" | ||
addonPosition="bottom" | ||
/> | ||
); | ||
fireEvent.click(button); | ||
checkbox = getByRole("menuitemcheckbox", { name: "Checkbox" }); | ||
radio1 = getByRole("menuitemradio", { name: "Radio 1" }); | ||
radio2 = getByRole("menuitemradio", { name: "Radio 2" }); | ||
switchEl = getByRole("menuitemcheckbox", { name: "Switch" }); | ||
expect(checkbox).toMatchSnapshot(); | ||
expect(checkbox).toHaveAttribute("aria-checked", "true"); | ||
expect(radio1).toMatchSnapshot(); | ||
expect(radio1).toHaveAttribute("aria-checked", "true"); | ||
expect(radio2).toMatchSnapshot(); | ||
expect(radio2).toHaveAttribute("aria-checked", "false"); | ||
expect(switchEl).toMatchSnapshot(); | ||
expect(switchEl).toHaveAttribute("aria-checked", "false"); | ||
}); | ||
}); |
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,65 @@ | ||
import React, { useState } from "react"; | ||
import { fireEvent, render, waitFor } from "@testing-library/react"; | ||
import { DropdownMenu } from "@react-md/menu"; | ||
|
||
import { MenuItemRadio } from "../MenuItemRadio"; | ||
|
||
describe("MenuItemRadio", () => { | ||
it("should update the state and close the menu once clicked", () => { | ||
function Test() { | ||
const [selectedIndex, setSelectedIndex] = useState(0); | ||
|
||
return ( | ||
<DropdownMenu | ||
id="menu-id" | ||
items={[ | ||
<MenuItemRadio | ||
key="item-1" | ||
id="menu-id-1" | ||
checked={selectedIndex === 0} | ||
onCheckedChange={() => { | ||
setSelectedIndex(0); | ||
}} | ||
> | ||
Radio 1 | ||
</MenuItemRadio>, | ||
<MenuItemRadio | ||
key="item-2" | ||
id="menu-id-2" | ||
checked={selectedIndex === 1} | ||
onCheckedChange={() => { | ||
setSelectedIndex(1); | ||
}} | ||
> | ||
Radio 2 | ||
</MenuItemRadio>, | ||
]} | ||
> | ||
Button | ||
</DropdownMenu> | ||
); | ||
} | ||
const { getByRole } = render(<Test />); | ||
const button = getByRole("button", { name: "Button" }); | ||
|
||
fireEvent.click(button); | ||
|
||
let radio1 = getByRole("menuitemradio", { name: "Radio 1" }); | ||
let radio2 = getByRole("menuitemradio", { name: "Radio 2" }); | ||
expect(radio1).toHaveAttribute("aria-checked", "true"); | ||
expect(radio2).toHaveAttribute("aria-checked", "false"); | ||
expect(radio1).toMatchSnapshot(); | ||
expect(radio2).toMatchSnapshot(); | ||
|
||
fireEvent.click(radio2); | ||
waitFor(() => expect(radio1).not.toBeInTheDocument()); | ||
|
||
fireEvent.click(button); | ||
radio1 = getByRole("menuitemradio", { name: "Radio 1" }); | ||
radio2 = getByRole("menuitemradio", { name: "Radio 2" }); | ||
expect(radio1).toHaveAttribute("aria-checked", "false"); | ||
expect(radio2).toHaveAttribute("aria-checked", "true"); | ||
expect(radio1).toMatchSnapshot(); | ||
expect(radio2).toMatchSnapshot(); | ||
}); | ||
}); |
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,47 @@ | ||
import React, { useState } from "react"; | ||
import { fireEvent, render, waitFor } from "@testing-library/react"; | ||
import { DropdownMenu } from "@react-md/menu"; | ||
|
||
import { MenuItemSwitch } from "../MenuItemSwitch"; | ||
|
||
describe("MenuItemSwitch", () => { | ||
it("should update the state and close the menu once clicked", () => { | ||
function Test() { | ||
const [checked, setChecked] = useState(false); | ||
|
||
return ( | ||
<DropdownMenu | ||
id="menu-id" | ||
items={[ | ||
<MenuItemSwitch | ||
key="item-1" | ||
id="menu-id-1" | ||
checked={checked} | ||
onCheckedChange={(nextChecked) => setChecked(nextChecked)} | ||
> | ||
Switch | ||
</MenuItemSwitch>, | ||
]} | ||
> | ||
Button | ||
</DropdownMenu> | ||
); | ||
} | ||
const { getByRole } = render(<Test />); | ||
const button = getByRole("button", { name: "Button" }); | ||
|
||
fireEvent.click(button); | ||
|
||
let switchEl = getByRole("menuitemcheckbox", { name: "Switch" }); | ||
expect(switchEl).toHaveAttribute("aria-checked", "false"); | ||
expect(switchEl).toMatchSnapshot(); | ||
|
||
fireEvent.click(switchEl); | ||
waitFor(() => expect(switchEl).not.toBeInTheDocument()); | ||
|
||
fireEvent.click(button); | ||
switchEl = getByRole("menuitemcheckbox", { name: "Switch" }); | ||
expect(switchEl).toHaveAttribute("aria-checked", "true"); | ||
expect(switchEl).toMatchSnapshot(); | ||
}); | ||
}); |
Oops, something went wrong.