Skip to content

Commit b906268

Browse files
authored
data component adr part 7 (#7991)
1 parent 956fbd8 commit b906268

25 files changed

Lines changed: 254 additions & 13 deletions

.changeset/public-bags-retire.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
"@primer/react": minor
3+
---
4+
5+
Add data-component attributes and associated tests for:
6+
7+
Radio
8+
RadioGroup
9+
RelativeTime
10+
ScrollableRegion
11+
SegmentedControl
12+
Select
13+
SideNav
14+
SkeletonBox
15+
SkeletonAvatar
16+
SkeletonText
17+
Spinner

packages/react/src/Radio/Radio.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ describe('Radio', () => {
2323
expect(radio).toBeDefined()
2424
})
2525

26+
it('renders data-component attribute', () => {
27+
const {getByRole} = render(<Radio {...defaultProps} />)
28+
29+
expect(getByRole('radio')).toHaveAttribute('data-component', 'Radio')
30+
})
31+
2632
it('renders an unchecked radio by default', () => {
2733
const {getByRole} = render(<Radio {...defaultProps} />)
2834

packages/react/src/Radio/Radio.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ const Radio = React.forwardRef<HTMLInputElement, RadioProps>(
8080
onChange={handleOnChange}
8181
className={clsx(className, sharedClasses.Input, classes.Radio)}
8282
{...rest}
83+
data-component="Radio"
8384
/>
8485
)
8586
},

packages/react/src/RadioGroup/RadioGroup.test.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ describe('RadioGroup', () => {
2828
vi.clearAllMocks()
2929
})
3030

31+
it('renders data-component attributes', () => {
32+
const {getByRole, getByText} = render(
33+
<RadioGroup name="choices">
34+
<RadioGroup.Label>Choices</RadioGroup.Label>
35+
<RadioGroup.Caption>Pick one</RadioGroup.Caption>
36+
<RadioGroup.Validation variant="error">Selection required</RadioGroup.Validation>
37+
<FormControl>
38+
<Radio value="one" />
39+
<FormControl.Label>Choice one</FormControl.Label>
40+
</FormControl>
41+
</RadioGroup>,
42+
)
43+
44+
expect(getByRole('group')).toHaveAttribute('data-component', 'RadioGroup')
45+
expect(getByText('Choices')).toHaveAttribute('data-component', 'RadioGroup.Label')
46+
expect(getByText('Pick one')).toHaveAttribute('data-component', 'RadioGroup.Caption')
47+
expect(document.querySelector('[data-component="RadioGroup.Validation"]')).toHaveTextContent('Selection required')
48+
})
49+
3150
it('renders a disabled group of inputs', () => {
3251
const {getAllByRole, getByRole} = render(
3352
<RadioGroup name="choices" disabled>

packages/react/src/RadioGroup/RadioGroup.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const RadioGroup: FC<React.PropsWithChildren<RadioGroupProps>> = ({children, dis
4444
},
4545
}}
4646
>
47-
<CheckboxOrRadioGroup disabled={disabled} {...rest}>
47+
<CheckboxOrRadioGroup disabled={disabled} data-component="RadioGroup" {...rest}>
4848
{children}
4949
</CheckboxOrRadioGroup>
5050
</RadioGroupContext.Provider>

packages/react/src/RelativeTime/RelativeTime.test.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ describe('RelativeTime', () => {
1010
expect(container.firstChild?.nodeName.toLowerCase()).toEqual('relative-time')
1111
})
1212

13+
it('renders data-component attribute', () => {
14+
const date = new Date('2024-03-07T12:22:48.123Z')
15+
const {container} = render(<RelativeTime date={date} />)
16+
17+
expect(container.firstChild).toHaveAttribute('data-component', 'RelativeTime')
18+
})
19+
1320
it('renders a date inside', () => {
1421
const date = new Date('2024-03-07T12:22:48.123Z')
1522
const {container} = render(<RelativeTime date={date} />)

packages/react/src/RelativeTime/RelativeTime.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const localeOptions: Intl.DateTimeFormatOptions = {month: 'short', day: 'numeric
88
function RelativeTime({date, datetime, children, noTitle, ...props}: RelativeTimeProps) {
99
if (datetime) date = new Date(datetime)
1010
return (
11-
<RelativeTimeComponent {...props} date={date} no-title={noTitle ? '' : undefined}>
11+
<RelativeTimeComponent {...props} date={date} no-title={noTitle ? '' : undefined} data-component="RelativeTime">
1212
{children || date?.toLocaleDateString('en', localeOptions) || ''}
1313
</RelativeTimeComponent>
1414
)

packages/react/src/ScrollableRegion/ScrollableRegion.test.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ describe('ScrollableRegion', () => {
3030
window.ResizeObserver = originalResizeObserver
3131
})
3232

33+
test('renders data-component attribute', () => {
34+
render(
35+
<ScrollableRegion aria-label="Example label" data-testid="container">
36+
Example content
37+
</ScrollableRegion>,
38+
)
39+
40+
expect(screen.getByTestId('container')).toHaveAttribute('data-component', 'ScrollableRegion')
41+
})
42+
3343
test('does not render with region props by default', () => {
3444
render(
3545
<ScrollableRegion aria-label="Example label" data-testid="container">

packages/react/src/ScrollableRegion/ScrollableRegion.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ function ScrollableRegion({
3434
: {}
3535

3636
return (
37-
<div {...rest} {...regionProps} ref={ref} className={clsx(classes.ScrollableRegion, className)}>
37+
<div
38+
{...rest}
39+
{...regionProps}
40+
ref={ref}
41+
className={clsx(classes.ScrollableRegion, className)}
42+
data-component="ScrollableRegion"
43+
>
3844
{children}
3945
</div>
4046
)

packages/react/src/SegmentedControl/SegmentedControl.test.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,44 @@ const segmentData = [
3434
describe('SegmentedControl', () => {
3535
implementsClassName(props => <SegmentedControl aria-label="File view" {...props} />, classes.SegmentedControl)
3636

37+
it('renders data-component attribute on the root', () => {
38+
const {getByRole} = render(
39+
<SegmentedControl aria-label="File view">
40+
{segmentData.map(({label}) => (
41+
<SegmentedControl.Button key={label}>{label}</SegmentedControl.Button>
42+
))}
43+
</SegmentedControl>,
44+
)
45+
46+
expect(getByRole('list')).toHaveAttribute('data-component', 'SegmentedControl')
47+
})
48+
49+
it('renders data-component attribute on segmented control buttons', () => {
50+
const {getByRole} = render(
51+
<SegmentedControl aria-label="File view">
52+
<SegmentedControl.Button>Preview</SegmentedControl.Button>
53+
</SegmentedControl>,
54+
)
55+
56+
expect(getByRole('button', {name: 'Preview'}).closest('li')).toHaveAttribute(
57+
'data-component',
58+
'SegmentedControl.Button',
59+
)
60+
})
61+
62+
it('renders data-component attribute on segmented control icon buttons', () => {
63+
const {getByRole} = render(
64+
<SegmentedControl aria-label="File view">
65+
<SegmentedControl.IconButton icon={() => <EyeIcon />} aria-label="Preview" />
66+
</SegmentedControl>,
67+
)
68+
69+
expect(getByRole('button', {name: 'Preview'}).closest('li')).toHaveAttribute(
70+
'data-component',
71+
'SegmentedControl.IconButton',
72+
)
73+
})
74+
3775
it('renders with a selected segment - controlled', () => {
3876
const {getByText} = render(
3977
<SegmentedControl aria-label="File view">

0 commit comments

Comments
 (0)