-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WNMGDS-1443] updating tooltip to support close button (#1625)
* updating tooltip to support close button * updated tooltip story * unit tests for new tooltip functionality * updated tooltip tests * updated snapshot tests
- Loading branch information
Showing
7 changed files
with
385 additions
and
218 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
114 changes: 99 additions & 15 deletions
114
packages/design-system/src/components/Tooltip/Tooltip.test.jsx
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,45 +1,129 @@ | ||
import { mount, shallow } from 'enzyme'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import React from 'react'; | ||
import Tooltip from './Tooltip'; | ||
import TooltipIcon from './TooltipIcon'; | ||
|
||
jest.mock('@popperjs/core'); | ||
|
||
const triggerAriaLabelText = 'tooltip trigger'; | ||
const defaultProps = { | ||
children: <TooltipIcon />, | ||
className: 'ds-c-tooltip__trigger-icon', | ||
title: 'Tooltip body content', | ||
ariaLabel: triggerAriaLabelText, | ||
}; | ||
|
||
function render(customProps = {}, deep = false) { | ||
function renderTooltip(customProps = {}) { | ||
const props = { ...defaultProps, ...customProps }; | ||
const component = <Tooltip {...props} />; | ||
return { | ||
props: props, | ||
wrapper: deep ? mount(component) : shallow(component), | ||
}; | ||
return render(<Tooltip {...props} />); | ||
} | ||
|
||
describe('Tooltip', function () { | ||
it('renders default trigger icon', () => { | ||
const tooltip = render(); | ||
expect(tooltip.wrapper).toMatchSnapshot(); | ||
const { queryByLabelText } = renderTooltip(); | ||
const triggerEl = queryByLabelText(triggerAriaLabelText); | ||
expect(triggerEl).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders inverse tooltip', () => { | ||
const tooltip = render({ inversed: true }); | ||
expect(tooltip.wrapper).toMatchSnapshot(); | ||
const { asFragment } = renderTooltip({ inversed: true }); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders custom trigger component', () => { | ||
const tooltip = render({ | ||
const { queryByLabelText } = renderTooltip({ | ||
component: 'a', | ||
}); | ||
expect(tooltip.wrapper).toMatchSnapshot(); | ||
const triggerEl = queryByLabelText(triggerAriaLabelText); | ||
expect(triggerEl).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders dialog tooltip', () => { | ||
const tooltip = render({ dialog: true }); | ||
expect(tooltip.wrapper).toMatchSnapshot(); | ||
const { queryByRole, getByLabelText } = renderTooltip({ dialog: true }); | ||
const tooltipTrigger = getByLabelText(triggerAriaLabelText); | ||
fireEvent.click(tooltipTrigger); | ||
const contentEl = queryByRole('dialog'); | ||
expect(contentEl).not.toBeNull(); | ||
expect(contentEl).toMatchSnapshot(); | ||
}); | ||
|
||
describe('tooltip with close', () => { | ||
it('renders a close button', () => { | ||
const { getByLabelText } = renderTooltip({ dialog: true, showCloseButton: true }); | ||
const closeButton = getByLabelText('Close', { selector: 'button' }); | ||
expect(closeButton).toBeDefined(); | ||
}); | ||
|
||
it('renders heading element', () => { | ||
const { queryByRole, getByLabelText } = renderTooltip({ | ||
dialog: true, | ||
contentHeading: 'Tooltip heading content', | ||
}); | ||
const tooltipTrigger = getByLabelText(triggerAriaLabelText); | ||
fireEvent.click(tooltipTrigger); | ||
const contentEl = queryByRole('dialog'); | ||
expect(contentEl).toMatchSnapshot(); | ||
}); | ||
|
||
it('renders heading element and close button', () => { | ||
const { queryByRole, getByLabelText } = renderTooltip({ | ||
dialog: true, | ||
contentHeading: 'Tooltip heading content', | ||
showCloseButton: true, | ||
}); | ||
const tooltipTrigger = getByLabelText(triggerAriaLabelText); | ||
fireEvent.click(tooltipTrigger); | ||
const contentEl = queryByRole('dialog'); | ||
expect(contentEl).toMatchSnapshot(); | ||
}); | ||
|
||
it('should call onClose when close button is clicked', () => { | ||
const onClose = jest.fn(); | ||
const { getByLabelText } = renderTooltip({ | ||
dialog: true, | ||
showCloseButton: true, | ||
onClose, | ||
}); | ||
const tooltipTrigger = getByLabelText(triggerAriaLabelText); | ||
fireEvent.click(tooltipTrigger); | ||
const closeButton = getByLabelText('Close', { selector: 'button' }); | ||
fireEvent.click(closeButton); | ||
expect(onClose).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should close tooltip when onClose is clicked', () => { | ||
const { getByLabelText, queryByRole } = renderTooltip({ | ||
dialog: true, | ||
showCloseButton: true, | ||
}); | ||
const tooltipTrigger = getByLabelText(triggerAriaLabelText); | ||
fireEvent.click(tooltipTrigger); | ||
const closeButton = getByLabelText('Close', { selector: 'button' }); | ||
fireEvent.click(closeButton); | ||
const tooltipContent = queryByRole('dialog'); | ||
expect(tooltipContent).toBeNull(); | ||
}); | ||
|
||
it('should return focus back to trigger when closed', () => { | ||
const { getByLabelText } = renderTooltip({ | ||
dialog: true, | ||
showCloseButton: true, | ||
}); | ||
const tooltipTrigger = getByLabelText(triggerAriaLabelText); | ||
fireEvent.click(tooltipTrigger); | ||
const closeButton = getByLabelText('Close', { selector: 'button' }); | ||
fireEvent.click(closeButton); | ||
expect(tooltipTrigger).toEqual(document.activeElement); | ||
}); | ||
|
||
it('close button should take custom aria label', () => { | ||
const { queryByLabelText } = renderTooltip({ | ||
dialog: true, | ||
showCloseButton: true, | ||
closeButtonLabel: 'custom close label text', | ||
}); | ||
const closeButton = queryByLabelText('custom close label text'); | ||
expect(closeButton).not.toBeNull(); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.