-
Notifications
You must be signed in to change notification settings - Fork 208
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
e68bc09
commit d32c7ef
Showing
3 changed files
with
88 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { describe, it, expect } from 'vitest'; | ||
import { Tooltip } from './Tooltip'; | ||
|
||
describe('Tooltip', () => { | ||
it('renders the children', () => { | ||
render(<Tooltip content="Test Content" />); | ||
expect(screen.getByTestId('ockBuyApplePayInfo')).toBeInTheDocument(); | ||
}); | ||
|
||
it('shows the content on mouse enter', () => { | ||
render(<Tooltip content="Test Content" />); | ||
const triggerElement = screen.getByTestId('ockBuyApplePayInfo'); | ||
|
||
fireEvent.mouseEnter(triggerElement); | ||
expect(screen.getByText('Test Content')).toBeInTheDocument(); | ||
}); | ||
|
||
it('hides the content on mouse leave', () => { | ||
render(<Tooltip content="Test Content" />); | ||
const triggerElement = screen.getByTestId('ockBuyApplePayInfo'); | ||
|
||
fireEvent.mouseEnter(triggerElement); | ||
fireEvent.mouseLeave(triggerElement); | ||
|
||
expect(screen.queryByText('Test Content')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('renders custom children if provided', () => { | ||
const CustomChild = <div>Custom Child</div>; | ||
render(<Tooltip content="Test Content">{CustomChild}</Tooltip>); | ||
|
||
expect(screen.getByText('Custom Child')).toBeInTheDocument(); | ||
}); | ||
}); |
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 { useCallback, useState } from 'react'; | ||
import { background, border, cn, text } from '@/styles/theme'; | ||
import { infoSvg } from '@/internal/svg/infoSvg'; | ||
|
||
type TooltipReact = { | ||
children?: React.ReactNode; | ||
content: React.ReactNode; | ||
}; | ||
|
||
export function Tooltip({ children = infoSvg, content }: TooltipReact) { | ||
const [isOverlayVisible, setIsOverlayVisible] = useState(false); | ||
|
||
const showOverlay = useCallback(() => { | ||
setIsOverlayVisible(true); | ||
}, []); | ||
|
||
const hideOverlay = useCallback(() => { | ||
setIsOverlayVisible(false); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<div | ||
data-testid="ockBuyApplePayInfo" | ||
className={cn('h-2.5 w-2.5 cursor-pointer object-cover')} | ||
onMouseEnter={showOverlay} | ||
onMouseLeave={hideOverlay} | ||
> | ||
{children} | ||
</div> | ||
{isOverlayVisible && ( | ||
<div | ||
className={cn( | ||
'absolute top-0 right-0 flex translate-x-[100%] translate-y-[-100%]', | ||
'whitespace-nowrap p-2', | ||
border.radius, | ||
background.inverse, | ||
text.legal, | ||
border.lineDefault, | ||
)} | ||
> | ||
{content} | ||
</div> | ||
)} | ||
</> | ||
); | ||
} |