-
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.
- Loading branch information
1 parent
e384667
commit de2d84c
Showing
3 changed files
with
81 additions
and
8 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,71 @@ | ||
import { render } from "@testing-library/react"; | ||
import userEvent from '@testing-library/user-event'; | ||
import NavBar from "./index"; | ||
|
||
describe("Navigation Bar", () => { | ||
test("navbar renders when menu is closed and menu button can be clicked", () =>{ | ||
const leftButtonHandler = jest.fn(); | ||
const onMenuToggle = jest.fn(); | ||
const { queryByText, getByText } = render( | ||
<NavBar | ||
leftButtonLabel="[Home]" | ||
leftButtonHandler={leftButtonHandler} | ||
title="Hello world" | ||
menuItems={{ "Menu Item": "/link" }} | ||
menuIsHidden={true} | ||
onMenuToggle={onMenuToggle} | ||
/> | ||
); | ||
|
||
const leftButton = getByText("[Home]") | ||
expect(leftButton).toBeInTheDocument() | ||
userEvent.click(leftButton) | ||
expect(leftButtonHandler).toHaveBeenCalled(); | ||
|
||
expect(getByText("Hello world")).toBeInTheDocument(); | ||
|
||
expect(queryByText("Menu Item")).not.toBeInTheDocument(); | ||
|
||
const rightButton = getByText("Menu") | ||
expect(rightButton).toBeInTheDocument(); | ||
userEvent.click(rightButton); | ||
expect(onMenuToggle).toHaveBeenCalled(); | ||
}) | ||
|
||
test("navbar and menu render when menu is open and close button can be clicked", () =>{ | ||
const leftButtonHandler = jest.fn(); | ||
const onMenuToggle = jest.fn(); | ||
const menuItemCallback = jest.fn(); | ||
const { getByText } = render( | ||
<NavBar | ||
leftButtonHandler={leftButtonHandler} | ||
title="Hello world" | ||
menuItems={{ "Menu Item": "/link", "Other Item": menuItemCallback }} | ||
menuIsHidden={false} | ||
onMenuToggle={onMenuToggle} | ||
/> | ||
); | ||
|
||
const leftButton = getByText("Home") | ||
expect(leftButton).toBeInTheDocument() | ||
userEvent.click(leftButton) | ||
expect(leftButtonHandler).toHaveBeenCalled(); | ||
|
||
expect(getByText("Hello world")).toBeInTheDocument(); | ||
|
||
const firstMenuItem = getByText("Menu Item"); | ||
expect(firstMenuItem).toBeInTheDocument(); | ||
expect(firstMenuItem).toHaveAttribute("href", "/link"); | ||
expect(menuItemCallback).not.toHaveBeenCalled(); | ||
|
||
const secondMenuItem = getByText("Other Item"); | ||
expect(secondMenuItem).toBeInTheDocument(); | ||
userEvent.click(secondMenuItem) | ||
expect(menuItemCallback).toHaveBeenCalled(); | ||
|
||
const rightButton = getByText("Close") | ||
expect(rightButton).toBeInTheDocument(); | ||
userEvent.click(rightButton); | ||
expect(onMenuToggle).toHaveBeenCalled(); | ||
}) | ||
}) |