Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/feat-card-data-selected.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clickhouse/click-ui': minor
---

`CardPrimary` and `CardHorizontal` now set `data-selected="true"` when `isSelected` is on, so tests can tell a selected card without relying on class names.
12 changes: 12 additions & 0 deletions src/components/CardHorizontal/CardHorizontal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ describe('CardHorizontal Component', () => {
vitest.restoreAllMocks();
});

it('should expose selection through data-selected', () => {
const { container, rerender } = renderCard({
title: 'Card',
isSelectable: true,
isSelected: true,
});
expect(container.querySelector('[data-selected="true"]')).not.toBeNull();

rerender(<CardHorizontal title="Card" />);
expect(container.querySelector('[data-selected]')).toBeNull();
});

it('should render the title', () => {
const title = 'Test card component';
renderCard({
Expand Down
1 change: 1 addition & 0 deletions src/components/CardHorizontal/CardHorizontal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const CardHorizontal = ({
<div
tabIndex={disabled ? -1 : 0}
aria-disabled={disabled}
data-selected={isSelected ? 'true' : undefined}
role={isButton ? 'button' : undefined}
onClick={handleClick}
onKeyDown={handleKeyDown}
Expand Down
8 changes: 8 additions & 0 deletions src/components/CardPrimary/CardPrimary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ describe('CardPrimary Component', () => {
vitest.restoreAllMocks();
});

it('should expose selection through data-selected', () => {
const { container, rerender } = renderCard({ title: 'Card', isSelected: true });
expect(container.querySelector('[data-selected="true"]')).not.toBeNull();

rerender(<CardPrimary title="Card" />);
expect(container.querySelector('[data-selected]')).toBeNull();
});

it('should render the title', () => {
const title = 'Test card component';
renderCard({
Expand Down
1 change: 1 addition & 0 deletions src/components/CardPrimary/CardPrimary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const Card = ({
return (
<div
aria-disabled={disabled}
data-selected={isSelected ? 'true' : undefined}
tabIndex={0}
{...props}
className={cn(
Expand Down