Skip to content

Commit 2455791

Browse files
committed
Add support for data-testid prop
1 parent b17c1ce commit 2455791

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

packages/react-clock/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Displays a complete clock.
6767
| Prop name | Description | Default value | Example values |
6868
| ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------- | --------------------------------------------------------------------------------------------------- |
6969
| className | Class name(s) that will be added along with `"react-clock"` to the main react-clock `<time>` element. | n/a | <ul><li>String: `"class1 class2"`</li><li>Array of strings: `["class1", "class2 class3"]`</li></ul> |
70+
| data-testid | The test ID used for testing purposes. | n/a | `'clock'` |
7071
| formatHour | Function called to override default formatting of hour marks. Can be used to use your own formatting function. | (default formatter) | `(locale, hour) => formatHour(hour, 'HH')` |
7172
| hourHandLength | Hour hand length, in %. | `50` | `80` |
7273
| hourHandOppositeLength | The length of the part of an hour hand on the opposite side the hand is pointing to, in %. | `10` | `20` |

packages/react-clock/src/Clock.spec.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ describe('Clock', () => {
2323
expect(wrapper).toHaveClass(className);
2424
});
2525

26+
it('applies data-testid to its wrapper when given a string', async () => {
27+
const dataTestId = 'testClock';
28+
29+
const { container } = await render(<Clock data-testid={dataTestId} />);
30+
31+
const wrapper = container.querySelector('.react-clock');
32+
33+
expect(wrapper).toHaveAttribute('data-testid', dataTestId);
34+
});
35+
2636
it('has 150px size by default', async () => {
2737
const { container } = await render(<Clock />);
2838

packages/react-clock/src/Clock.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export type ClockProps = {
2525
* @example ['class1', 'class2 class3']
2626
*/
2727
className?: ClassName;
28+
/**
29+
* The test ID used for testing purposes.
30+
*
31+
* @example 'clock'
32+
*/
33+
'data-testid'?: string;
2834
/**
2935
* Function called to override default formatting of hour marks. Can be used to use your own formatting function.
3036
*
@@ -193,6 +199,7 @@ export type ClockProps = {
193199
*/
194200
export default function Clock({
195201
className,
202+
'data-testid': dataTestId,
196203
formatHour = defaultFormatHour,
197204
hourHandLength = 50,
198205
hourHandOppositeLength,
@@ -362,6 +369,7 @@ export default function Clock({
362369
return (
363370
<time
364371
className={clsx('react-clock', className)}
372+
data-testid={dataTestId}
365373
dateTime={
366374
value instanceof Date
367375
? // Returns a string in the format "HH:MM" or "HH:MM:SS"

0 commit comments

Comments
 (0)