Skip to content

Commit 51a38ae

Browse files
author
Matthew Holloway
authored
Merge pull request #325 from gavinhenderson/master
Allow 0 to be a valid UUID
2 parents e0dc05b + 980d745 commit 51a38ae

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/components/AccordionItem.spec.tsx

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { cleanup, render } from '@testing-library/react';
1+
import { cleanup, render, fireEvent } from '@testing-library/react';
22
import * as React from 'react';
33
import { default as Accordion } from './Accordion';
44
import AccordionItem from './AccordionItem';
5+
import AccordionItemButton from './AccordionItemButton';
56

67
enum UUIDS {
78
FOO = 'FOO',
@@ -74,4 +75,29 @@ describe('AccordionItem', () => {
7475
expect(getByText('Hello World')).toBeTruthy();
7576
});
7677
});
78+
79+
describe('uuid prop', () => {
80+
it('keeps the uuid as 0 even though its falsy', () => {
81+
const testId = 'el-with-zero-uuid';
82+
const zero = 0;
83+
let selected: (string | number)[] = [];
84+
const { getByTestId } = render(
85+
<Accordion
86+
onChange={(latestSelected) => {
87+
selected = latestSelected;
88+
}}
89+
>
90+
<AccordionItem uuid={zero}>
91+
<AccordionItemButton data-testid={testId}>
92+
Click me
93+
</AccordionItemButton>
94+
</AccordionItem>
95+
</Accordion>,
96+
);
97+
98+
fireEvent.click(getByTestId(testId));
99+
100+
expect(selected).toEqual([zero]);
101+
});
102+
});
77103
});

src/components/AccordionItem.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const AccordionItem = ({
2424
...rest
2525
}: Props): JSX.Element => {
2626
const [instanceUuid] = useState<UUID>(nextUuid());
27-
const uuid = customUuid || instanceUuid;
27+
const uuid = customUuid ?? instanceUuid;
2828

2929
const renderChildren = (itemContext: ItemContext): JSX.Element => {
3030
const { expanded } = itemContext;
@@ -39,7 +39,7 @@ const AccordionItem = ({
3939
);
4040
};
4141

42-
assertValidHtmlId(uuid);
42+
assertValidHtmlId(uuid.toString());
4343
if (rest.id) {
4444
assertValidHtmlId(rest.id);
4545
}

src/components/ItemContext.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
Consumer as AccordionContextConsumer,
1212
} from './AccordionContext';
1313

14-
export type UUID = string;
14+
export type UUID = string | number;
1515

1616
type ProviderProps = {
1717
children?: React.ReactNode;

0 commit comments

Comments
 (0)