Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(definitiontooltip): increase test coverage to 100% #17434

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import userEvent from '@testing-library/user-event';
import React from 'react';
import { DefinitionTooltip } from '../DefinitionTooltip';

describe('DefintiionTooltip', () => {
describe('DefinitionTooltip', () => {
it('should display onClick a defintion provided via prop', async () => {
tay1orjones marked this conversation as resolved.
Show resolved Hide resolved
const definition = 'Uniform Resource Locator';
render(<DefinitionTooltip definition={definition}>URL</DefinitionTooltip>);
Expand All @@ -29,6 +29,123 @@ describe('DefintiionTooltip', () => {
});

describe('Component API', () => {
it('should open onKeyDown', async () => {
const user = userEvent.setup();
const definition = 'Uniform Resource Locator';
render(
<DefinitionTooltip
data-testid="test"
definition={definition}
className="tooltip-class">
URL
</DefinitionTooltip>
);

const button = screen.getByRole('button');

await user.tab();
expect(button).toHaveAttribute('aria-expanded', 'true');

await user.keyboard('[Escape]');
expect(button).toHaveAttribute('aria-expanded', 'false');
});
it('should close when trigger is blurred', async () => {
const user = userEvent.setup();
const definition = 'Uniform Resource Locator';
render(
<DefinitionTooltip
data-testid="test"
definition={definition}
className="tooltip-class">
URL
</DefinitionTooltip>
);

const button = screen.getByRole('button');

await user.tab();
expect(button).toHaveAttribute('aria-expanded', 'true');
await user.tab();
expect(button).toHaveAttribute('aria-expanded', 'false');
});
it('should close on unhover/mouseout when openOnHover is false', async () => {
const user = userEvent.setup();
const definition = 'Uniform Resource Locator';
render(
<DefinitionTooltip
data-testid="test"
definition={definition}
className="tooltip-class"
defaultOpen>
URL
</DefinitionTooltip>
);

const content = screen.getByText(definition);

expect(content).toBeVisible();
await userEvent.unhover(content);
expect(screen.getByRole('button')).toHaveAttribute(
'aria-expanded',
'false'
);
});
it('should open on hover when openOnHover', async () => {
const user = userEvent.setup();
const definition = 'Uniform Resource Locator';
render(
<DefinitionTooltip
data-testid="test"
definition={definition}
className="tooltip-class"
openOnHover>
URL
</DefinitionTooltip>
);

const content = screen.getByText(definition);
tay1orjones marked this conversation as resolved.
Show resolved Hide resolved
tay1orjones marked this conversation as resolved.
Show resolved Hide resolved
const trigger = screen.getByRole('button');

expect(screen.getByRole('button')).toHaveAttribute(
'aria-expanded',
'false'
);
await user.hover(trigger);
expect(screen.getByRole('button')).toHaveAttribute(
'aria-expanded',
'true'
);
await user.unhover(trigger);
expect(screen.getByRole('button')).toHaveAttribute(
'aria-expanded',
'false'
);
});
it('should not open on hover by default', async () => {
const user = userEvent.setup();
const definition = 'Uniform Resource Locator';
render(
<DefinitionTooltip
data-testid="test"
definition={definition}
className="tooltip-class">
URL
</DefinitionTooltip>
);

const content = screen.getByText(definition);
tay1orjones marked this conversation as resolved.
Show resolved Hide resolved
tay1orjones marked this conversation as resolved.
Show resolved Hide resolved
const trigger = screen.getByRole('button');

expect(screen.getByRole('button')).toHaveAttribute(
'aria-expanded',
'false'
);
await user.hover(trigger);
expect(screen.getByRole('button')).toHaveAttribute(
'aria-expanded',
'false'
);
});
it('should apply additional props to the underlying button element', () => {
const definition = 'Uniform Resource Locator';
render(
Expand Down
Loading