Skip to content

Commit

Permalink
Move testid to the parent–check icon too
Browse files Browse the repository at this point in the history
  • Loading branch information
caglarturali committed Oct 13, 2024
1 parent dd76d6f commit ca6ee81
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/widgets/IconLinkWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ export default function IconLinkWidget({
}

return (
<p className="group inline-flex items-center" style={pStyles} role="link">
<p
className="group inline-flex items-center"
style={pStyles}
data-testid="icon-link"
>
<FontAwesomeIcon
icon={icon}
className="w-6 opacity-85 group-hover:opacity-70"
/>
<a {...linkProps} className="select-none" data-testid="icon-link">
<a {...linkProps} className="select-none">
<span>{text}</span>
</a>
</p>
Expand Down
17 changes: 12 additions & 5 deletions src/widgets/__tests__/IconLinkWidget.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ describe('<IconLinkWidget />', () => {
render(<IconLinkWidget text={text} target={target} icon={faEarth} />);

await screen.findByTestId('icon-link');
const iconLink = screen.getByTestId('icon-link');
const icon = iconLink.querySelector('svg');
const link = iconLink.querySelector('a');

expect(screen.getByTestId('icon-link')).toHaveTextContent(text);
expect(screen.getByTestId('icon-link')).toHaveAttribute('href', target);
expect(screen.getByTestId('icon-link')).toHaveAttribute('title', text);
expect(icon).toBeInTheDocument();
expect(icon).toHaveAttribute('data-icon', faEarth.iconName);
expect(link).toHaveTextContent(text);
expect(link).toHaveAttribute('href', target);
expect(link).toHaveAttribute('title', text);
});

test('should render with a function target and handle click', async () => {
const target = vi.fn();
render(<IconLinkWidget text="Dummy Link" target={target} icon={faEarth} />);

await screen.findByTestId('icon-link');
await userEvent.click(screen.getByTestId('icon-link'));
const iconLink = screen.getByTestId('icon-link');
const link = iconLink.querySelector('a');
await userEvent.click(link);

expect(screen.getByTestId('icon-link')).toHaveAttribute('href', '#');
expect(link).toHaveAttribute('href', '#');
expect(target).toHaveBeenCalledOnce();
});
});

0 comments on commit ca6ee81

Please sign in to comment.