Skip to content

Commit

Permalink
Merge pull request #325 from gavinhenderson/master
Browse files Browse the repository at this point in the history
Allow 0 to be a valid UUID
  • Loading branch information
Matthew Holloway committed Aug 1, 2021
2 parents e0dc05b + 980d745 commit 51a38ae
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
28 changes: 27 additions & 1 deletion src/components/AccordionItem.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { cleanup, render } from '@testing-library/react';
import { cleanup, render, fireEvent } from '@testing-library/react';
import * as React from 'react';
import { default as Accordion } from './Accordion';
import AccordionItem from './AccordionItem';
import AccordionItemButton from './AccordionItemButton';

enum UUIDS {
FOO = 'FOO',
Expand Down Expand Up @@ -74,4 +75,29 @@ describe('AccordionItem', () => {
expect(getByText('Hello World')).toBeTruthy();
});
});

describe('uuid prop', () => {
it('keeps the uuid as 0 even though its falsy', () => {
const testId = 'el-with-zero-uuid';
const zero = 0;
let selected: (string | number)[] = [];
const { getByTestId } = render(
<Accordion
onChange={(latestSelected) => {
selected = latestSelected;
}}
>
<AccordionItem uuid={zero}>
<AccordionItemButton data-testid={testId}>
Click me
</AccordionItemButton>
</AccordionItem>
</Accordion>,
);

fireEvent.click(getByTestId(testId));

expect(selected).toEqual([zero]);
});
});
});
4 changes: 2 additions & 2 deletions src/components/AccordionItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const AccordionItem = ({
...rest
}: Props): JSX.Element => {
const [instanceUuid] = useState<UUID>(nextUuid());
const uuid = customUuid || instanceUuid;
const uuid = customUuid ?? instanceUuid;

const renderChildren = (itemContext: ItemContext): JSX.Element => {
const { expanded } = itemContext;
Expand All @@ -39,7 +39,7 @@ const AccordionItem = ({
);
};

assertValidHtmlId(uuid);
assertValidHtmlId(uuid.toString());
if (rest.id) {
assertValidHtmlId(rest.id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ItemContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
Consumer as AccordionContextConsumer,
} from './AccordionContext';

export type UUID = string;
export type UUID = string | number;

type ProviderProps = {
children?: React.ReactNode;
Expand Down

0 comments on commit 51a38ae

Please sign in to comment.