forked from Workday/canvas-kit
-
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.
test(tooltip): Add tooltip specifications
* Add unit tests * Add Cypress specifications * Add visual tests * Upgrade Popper to v2 and use canvas-kit Popper component instead of materia-ui's Tooltip portion of Workday#286
- Loading branch information
1 parent
2e6db84
commit 83385ff
Showing
9 changed files
with
260 additions
and
278 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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import * as h from '../helpers'; | ||
|
||
describe('Tooltip', () => { | ||
before(() => { | ||
h.stories.visit(); | ||
}); | ||
|
||
context('given Tooltip Default is rendered', () => { | ||
beforeEach(() => { | ||
h.stories.load('Tooltip', 'Default'); | ||
}); | ||
|
||
it('should not have any axe errors', () => { | ||
cy.checkA11y(); | ||
}); | ||
|
||
it('should have an aria-label of "Close"', () => { | ||
cy.get('button').should('have.attr', 'aria-label', 'Close'); | ||
}); | ||
|
||
context('when close icon is hovered', () => { | ||
beforeEach(() => { | ||
cy.get('button').trigger('mouseover'); | ||
}); | ||
|
||
it('should open the tooltip', () => { | ||
cy.get('[role=tooltip]').should('be.visible'); | ||
}); | ||
|
||
it('should not have any axe errors', () => { | ||
cy.checkA11y(); | ||
}); | ||
|
||
it('the button should be aria-describedby the tooltip', () => { | ||
cy.get('[role=tooltip]', {log: false}).should($tooltip => { | ||
const id = $tooltip.attr('id'); | ||
expect(Cypress.$('button')).to.have.attr('aria-describedby', id); | ||
expect($tooltip).to.have.attr('id', id); | ||
}); | ||
}); | ||
}); | ||
|
||
context('when close icon gains focus', () => { | ||
beforeEach(() => { | ||
cy.get('button').focus(); | ||
}); | ||
|
||
it('should open the tooltip', () => { | ||
cy.get('[role=tooltip]').should('be.visible'); | ||
}); | ||
|
||
context('then the close icon loses focus', () => { | ||
beforeEach(() => { | ||
cy.get('button').blur(); | ||
}); | ||
|
||
it('should close the tooltip', () => { | ||
cy.get('[role=tooltip]').should('not.be.visible'); | ||
}); | ||
}); | ||
|
||
context('then Escape key is pressed', () => { | ||
beforeEach(() => { | ||
cy.get('button').trigger('keydown', { | ||
key: 'Escape', | ||
}); | ||
}); | ||
|
||
it('should not remove focus from the close icon button', () => { | ||
cy.get('button').should('have.focus'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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,12 +1,43 @@ | ||
/// <reference types="@testing-library/jest-dom/extend-expect" /> | ||
|
||
import * as React from 'react'; | ||
import {mount} from 'enzyme'; | ||
import Tooltip from '../lib/Tooltip'; | ||
import {render, cleanup, fireEvent} from '@testing-library/react'; | ||
|
||
import Tooltip, {useTooltip} from '../lib/Tooltip'; | ||
|
||
const TooltipWithHook = () => { | ||
const {targetProps, tooltipProps} = useTooltip(); | ||
|
||
return ( | ||
<> | ||
<button {...targetProps}>Hover</button> | ||
<Tooltip {...tooltipProps}>Tooltip Content</Tooltip> | ||
</> | ||
); | ||
}; | ||
|
||
describe('Tooltip', () => { | ||
test('Tooltip should spread extra props', () => { | ||
const component = mount(<Tooltip data-propspread="test" />); | ||
const container = component.at(0).getDOMNode(); | ||
expect(container.getAttribute('data-propspread')).toBe('test'); | ||
component.unmount(); | ||
afterEach(cleanup); | ||
|
||
it('should spread extra props on the element', async () => { | ||
const {container} = render(<Tooltip data-propspread="test" />); | ||
const tooltip = container.firstElementChild; | ||
|
||
expect(tooltip).toHaveAttribute('role', 'tooltip'); | ||
}); | ||
|
||
describe('useToolip', () => { | ||
it('should add aria attributes to correlate the target and the tooltip', async () => { | ||
const rendered = render(<TooltipWithHook />); | ||
|
||
const target = await rendered.findByText('Hover'); | ||
const tooltip = await rendered.findByRole('tooltip'); | ||
|
||
await fireEvent.mouseOver(target); // assign the ID to the tooltip | ||
|
||
expect(tooltip).toHaveAttribute('id'); | ||
const id = tooltip.getAttribute('id'); | ||
expect(target).toHaveAttribute('aria-describedby', id); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.