-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd2300b
commit eb5e409
Showing
2 changed files
with
43 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from "react"; | ||
import { fireEvent } from "@testing-library/react"; | ||
import Dropdown from "../../components/Dropdown"; | ||
import { vitest } from "vitest"; | ||
import { renderWithProvider } from "../../testHelpers"; | ||
|
||
describe("Dropdown", () => { | ||
const items = [ | ||
{ value: 1, title: "Item 1" }, | ||
{ value: 2, title: "Item 2" }, | ||
{ value: 3, title: "Item 3" }, | ||
]; | ||
const value = 1; | ||
const onChange = vitest.fn(); | ||
|
||
it("should render the dropdown with correct items and tabIndexes", () => { | ||
const { getByTestId, getAllByText, getByText } = renderWithProvider( | ||
<Dropdown items={items} value={value} onChange={onChange} /> | ||
); | ||
|
||
const dropdownItemsList = getByTestId("dropdown-items"); | ||
|
||
const selectedItem = getAllByText(items[0].title)[1]; | ||
const nonSelectedItem = getByText(items[1].title); | ||
|
||
expect(dropdownItemsList.children.length).toEqual(items.length); | ||
expect(selectedItem.tabIndex).toEqual(-1); | ||
expect(nonSelectedItem.tabIndex).not.toEqual(-1); | ||
}); | ||
|
||
it("should call onChange function when an item is selected", () => { | ||
const { getByText } = renderWithProvider(<Dropdown items={items} value={value} onChange={onChange} />); | ||
|
||
const nonSelectedItem = getByText(items[1].title); | ||
|
||
fireEvent.click(nonSelectedItem); | ||
|
||
expect(onChange).toHaveBeenCalledTimes(1); | ||
expect(onChange).toHaveBeenCalledWith(items[1].value); | ||
}); | ||
}); |
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