This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
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.
fix: Navbar Brand Second Iteration (#105)
* navbar item container return * initial container test * renders test * dropdown * cleanup test * initial navbar item * navbar item * initial navbar item tests * container test * no props test * custom class name test * public api * navbar item story
- Loading branch information
Showing
10 changed files
with
171 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./navbar"; | ||
export * from "./navbar-brand"; | ||
export * from "./navbar-item"; |
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,27 @@ | ||
import { screen, render } from "@testing-library/react"; | ||
import { NavbarItem } from "./NavbarItem"; | ||
|
||
it("renders a link if link props are given", () => { | ||
const link = { href: "https://www.google.com" }; | ||
render(<NavbarItem link={link}>Link</NavbarItem>); | ||
const navbarItem = screen.getByRole("link"); | ||
expect(navbarItem).toBeInTheDocument(); | ||
expect(navbarItem).toHaveAttribute("href", link.href); | ||
}); | ||
|
||
it("renders a container if container props are given", () => { | ||
const container = { dropdown: <div></div> }; | ||
const text = "Container"; | ||
render(<NavbarItem container={container}>{text}</NavbarItem>); | ||
const navbarItem = screen.getByText(text); | ||
expect(navbarItem).toBeInTheDocument(); | ||
expect(navbarItem.nodeName).toEqual("DIV"); | ||
}); | ||
|
||
it("renders a container even if no props are given at all", () => { | ||
const text = "Container"; | ||
render(<NavbarItem>{text}</NavbarItem>); | ||
const navbarItem = screen.getByText(text); | ||
expect(navbarItem).toBeInTheDocument(); | ||
expect(navbarItem.nodeName).toEqual("DIV"); | ||
}); |
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,33 @@ | ||
import { PropsWithChildren } from "react"; | ||
import { | ||
NavbarItemContainer, | ||
NavbarItemContainerProps, | ||
} from "./NavbarItemContainer"; | ||
import { NavbarItemLink, NavbarItemLinkProps } from "./NavbarItemLink"; | ||
|
||
export type NavbarItemProps = PropsWithChildren<{ | ||
link?: NavbarItemLinkProps; | ||
container?: NavbarItemContainerProps; | ||
}>; | ||
|
||
const className = "navbar-item"; | ||
|
||
export const NavbarItem = ({ | ||
link, | ||
container, | ||
children, | ||
}: NavbarItemProps): JSX.Element => { | ||
if (link) { | ||
return ( | ||
<NavbarItemLink {...link} className={className}> | ||
{children} | ||
</NavbarItemLink> | ||
); | ||
} else { | ||
return ( | ||
<NavbarItemContainer {...container} className={className}> | ||
{children} | ||
</NavbarItemContainer> | ||
); | ||
} | ||
}; |
34 changes: 34 additions & 0 deletions
34
src/components/navigation/navbar-item/NavbarItemContainer.test.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,34 @@ | ||
import { screen, render } from "@testing-library/react"; | ||
import { NavbarItemContainer } from "./NavbarItemContainer"; | ||
|
||
it("renders given children", () => { | ||
const content = "Navbar Item Container"; | ||
render(<NavbarItemContainer>{content}</NavbarItemContainer>); | ||
expect(screen.getByText(content)).toBeInTheDocument(); | ||
}); | ||
|
||
it("is rendered with a given class", () => { | ||
const className = "some-custom-class"; | ||
const content = "Navbar Item Container"; | ||
render( | ||
<NavbarItemContainer className={className}>{content}</NavbarItemContainer> | ||
); | ||
expect(screen.getByText(content)).toHaveClass(className, { exact: true }); | ||
}); | ||
|
||
it("can be rendered with a dropdown", () => { | ||
const dropdownText = "Test Dropdown"; | ||
const dropdown = <div className="navbar-dropdown">{dropdownText}</div>; | ||
const content = "Navbar Item Container"; | ||
render( | ||
<NavbarItemContainer dropdown={dropdown}>{content}</NavbarItemContainer> | ||
); | ||
expect(screen.getByText(content)).toHaveClass("has-dropdown"); | ||
expect(screen.getByText(dropdownText)).toBeInTheDocument(); | ||
}); | ||
|
||
it("can be rendered without a dropdown", () => { | ||
const content = "Navbar Item Container"; | ||
render(<NavbarItemContainer>{content}</NavbarItemContainer>); | ||
expect(screen.getByText(content)).not.toHaveClass("has-dropdown"); | ||
}); |
32 changes: 32 additions & 0 deletions
32
src/components/navigation/navbar-item/NavbarItemContainer.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,32 @@ | ||
import { PropsWithChildren, ReactNode } from "react"; | ||
import { | ||
ClassNameTransformMap, | ||
createClassNameFromProps, | ||
} from "../../../utilities"; | ||
|
||
export type NavbarItemContainerProps = PropsWithChildren<{ | ||
className?: string; | ||
dropdown?: ReactNode; | ||
}>; | ||
|
||
const classNameMapping: ClassNameTransformMap<NavbarItemContainerProps> = | ||
new Map([["dropdown", () => "has-dropdown"]]); | ||
|
||
export const NavbarItemContainer = ({ | ||
className, | ||
children, | ||
dropdown, | ||
}: NavbarItemContainerProps): JSX.Element => { | ||
const dynamicClassName = createClassNameFromProps( | ||
classNameMapping, | ||
{ dropdown } as Partial<NavbarItemContainerProps>, | ||
[className ?? ""] | ||
); | ||
return ( | ||
<div className={dynamicClassName}> | ||
{children} | ||
|
||
{dropdown} | ||
</div> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from "./NavbarItemLink"; | ||
export * from "./NavbarItem"; |
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,27 @@ | ||
import { Story, Meta } from "@storybook/react"; | ||
|
||
import { NavbarItem, NavbarItemProps } from "../../components"; | ||
|
||
export default { | ||
component: NavbarItem, | ||
title: "Navigation/NavbarItem", | ||
argTypes: { | ||
link: { | ||
control: { | ||
type: "object", | ||
}, | ||
}, | ||
children: { | ||
control: { | ||
type: "text", | ||
}, | ||
}, | ||
}, | ||
args: { | ||
children: "Navbar Item", | ||
}, | ||
} as Meta<NavbarItemProps>; | ||
|
||
const Template: Story<NavbarItemProps> = (args) => <NavbarItem {...args} />; | ||
|
||
export const InteractiveNavbarItem = Template.bind({}); |