Skip to content

Commit fc09211

Browse files
authored
feat(thumbnail-card-details): add keydown callback and default tabindex (#3782)
* feat(thumbnail-card-details): add keydown callback * feat(thumbnail-card-details): optionalize onKeyDownCallback * feat(thumbnail-card-details): spec update * feat(thumbnail-card-details): convert spec to RTL * feat(thumbnail-card-details): update specs * feat(thumbnail-card-details): rename prop
1 parent 1e8ef7f commit fc09211

File tree

4 files changed

+45
-127
lines changed

4 files changed

+45
-127
lines changed

src/components/thumbnail-card/ThumbnailCard.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Props = {
1212
className?: string,
1313
highlightOnHover?: boolean,
1414
icon?: React.Node,
15+
onKeyDown?: () => void,
1516
subtitle?: React.Node,
1617
thumbnail: React.Node,
1718
title: React.Node,
@@ -20,11 +21,12 @@ type Props = {
2021
const ThumbnailCard = ({
2122
actionItem,
2223
className = '',
23-
icon,
2424
highlightOnHover = false,
25+
icon,
26+
onKeyDown,
2527
subtitle,
26-
title,
2728
thumbnail,
29+
title,
2830
...rest
2931
}: Props) => (
3032
<div
@@ -34,7 +36,13 @@ const ThumbnailCard = ({
3436
{...rest}
3537
>
3638
<ThumbnailCardThumbnail thumbnail={thumbnail} />
37-
<ThumbnailCardDetails actionItem={actionItem} icon={icon} subtitle={subtitle} title={title} />
39+
<ThumbnailCardDetails
40+
actionItem={actionItem}
41+
icon={icon}
42+
onKeyDown={onKeyDown}
43+
subtitle={subtitle}
44+
title={title}
45+
/>
3846
</div>
3947
);
4048

src/components/thumbnail-card/ThumbnailCardDetails.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,36 @@ import { useIsContentOverflowed } from '../../utils/dom';
77
type Props = {
88
actionItem?: React.Element<any>,
99
icon?: React.Node,
10+
onKeyDown?: () => void,
1011
subtitle?: React.Node,
1112
title: React.Node,
1213
};
1314

1415
type TitleProps = {
16+
onKeyDown?: () => void,
1517
title: React.Node,
1618
};
1719

18-
const Title = ({ title }: TitleProps) => {
19-
// $FlowFixMe
20+
const Title = ({ title, onKeyDown }: TitleProps) => {
2021
const textRef: { current: null | HTMLElement } = React.useRef(null);
2122
const isTextOverflowed = useIsContentOverflowed(textRef);
2223

2324
return (
2425
<Tooltip isDisabled={!isTextOverflowed} text={title}>
25-
<div ref={textRef} className="thumbnail-card-title">
26+
{/* eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex, jsx-a11y/no-static-element-interactions */}
27+
<div ref={textRef} role="link" className="thumbnail-card-title" tabIndex={0} onKeyDown={onKeyDown}>
2628
{title}
2729
</div>
2830
</Tooltip>
2931
);
3032
};
3133

32-
const ThumbnailCardDetails = ({ actionItem, icon, subtitle, title }: Props) => (
34+
const ThumbnailCardDetails = ({ actionItem, icon, subtitle, title, onKeyDown }: Props) => (
3335
<div className="thumbnail-card-details">
3436
{icon}
3537
<div className="thumbnail-card-details-content">
3638
<div className="ThumbnailCardDetails-bodyText">
37-
<Title title={title} />
39+
<Title title={title} onKeyDown={onKeyDown} />
3840
{subtitle && <div className="thumbnail-card-subtitle">{subtitle}</div>}
3941
</div>
4042
{actionItem}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// @flow
22
import * as React from 'react';
3+
import userEvent from '@testing-library/user-event';
34

4-
import { mount, shallow } from 'enzyme';
5+
import { render, screen } from '../../../test-utils/testing-library';
56

67
import * as libDom from '../../../utils/dom';
7-
88
import ThumbnailCardDetails from '../ThumbnailCardDetails';
99

10-
const getWrapper = (props = {}) => shallow(<ThumbnailCardDetails title={<div>Foo Bar!</div>} {...props} />);
10+
const renderComponent = (props = {}) => render(<ThumbnailCardDetails title={<div>Foo Bar!</div>} {...props} />);
1111

1212
jest.mock('../../../utils/dom', () => ({ useIsContentOverflowed: jest.fn() }));
1313

@@ -17,37 +17,49 @@ describe('components/thumbnail-card/ThumbnailCardDetails', () => {
1717
});
1818

1919
test('should render', () => {
20-
const wrapper = getWrapper();
20+
const { container } = renderComponent();
2121

22-
expect(wrapper).toMatchSnapshot();
22+
expect(container.querySelector('.thumbnail-card-details')).toBeInTheDocument();
2323
});
2424

2525
test('should render icon', () => {
2626
const icon = <img alt="icon" />;
27-
const wrapper = getWrapper({ icon });
27+
renderComponent({ icon });
2828

29-
expect(wrapper).toMatchSnapshot();
29+
expect(screen.queryByAltText('icon')).toBeInTheDocument();
3030
});
3131

3232
test('should render subtitle', () => {
3333
const subtitle = <div>Subtitle!</div>;
34-
const wrapper = getWrapper({ subtitle });
34+
const { container } = renderComponent({ subtitle });
3535

36-
expect(wrapper).toMatchSnapshot();
36+
expect(container.querySelector('.thumbnail-card-subtitle')).toBeInTheDocument();
3737
});
3838

3939
test('should render actionItem', () => {
40-
const actionItem = <button type="button">Click Me</button>;
41-
const wrapper = getWrapper({ actionItem });
40+
const actionText = 'Click Me';
41+
const actionItem = <button type="button">{actionText}</button>;
42+
renderComponent({ actionItem });
4243

43-
expect(wrapper).toMatchSnapshot();
44+
expect(screen.getByText(actionText)).toBeInTheDocument();
4445
});
4546

46-
test('should render a Tooltip if text is overflowed', () => {
47+
test('should render a Tooltip if text is overflowed', async () => {
4748
libDom.useIsContentOverflowed.mockReturnValue(true);
49+
renderComponent();
50+
51+
await userEvent.tab();
52+
53+
expect(screen.getByRole('tooltip')).toBeInTheDocument();
54+
});
55+
56+
test('should accept a keydown callback', async () => {
57+
const someFunction = jest.fn();
58+
const { container } = renderComponent({ onKeyDown: someFunction });
59+
const title = container.querySelector('.thumbnail-card-title');
4860

49-
const wrapper = mount(<ThumbnailCardDetails title={<div>Foo Bar!</div>} />);
61+
await userEvent.type(title, '{enter}');
5062

51-
expect(wrapper.find('Tooltip').length).toBe(1);
63+
expect(someFunction).toHaveBeenCalled();
5264
});
5365
});

src/components/thumbnail-card/__tests__/__snapshots__/ThumbnailCardDetails.test.js.snap

-104
This file was deleted.

0 commit comments

Comments
 (0)