Skip to content

Commit

Permalink
test(definitiontooltip): increase test coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
tay1orjones committed Sep 12, 2024
1 parent a6f1b23 commit 1807e6a
Showing 1 changed file with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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);
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

0 comments on commit 1807e6a

Please sign in to comment.