Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow 0 to be a valid UUID #325

Merged
merged 2 commits into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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