Skip to content

Commit

Permalink
BREAKING CHANGE(web-react): Add mandatory id prop for Collapse compon…
Browse files Browse the repository at this point in the history
…ent #DS-1190
  • Loading branch information
curdaj committed May 14, 2024
1 parent c93d91d commit 5bf5151
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { useCollapse } from './useCollapse';
import { useCollapseAriaProps } from './useCollapseAriaProps';

const defaultProps = {
/** @deprecated ID will be made a required user input in the next major version. */
id: Math.random().toString(36).slice(2, 7),
isOpen: false,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '@testing-library/jest-dom';
import { fireEvent, render } from '@testing-library/react';
import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import { classNamePrefixProviderTest } from '../../../../tests/providerTests/classNamePrefixProviderTest';
import { restPropsTest } from '../../../../tests/providerTests/restPropsTest';
Expand All @@ -13,9 +13,10 @@ describe('UncontrolledCollapse', () => {

restPropsTest(UncontrolledCollapse, 'div');

it('should render text children', () => {
const dom = render(
beforeEach(() => {
render(
<UncontrolledCollapse
id="exampleId"
// Normally we want to display state change, not in this test, prop is passed anyway
// eslint-disable-next-line @typescript-eslint/no-unused-vars
renderTrigger={({ isOpen, ...rest }) => (
Expand All @@ -27,27 +28,17 @@ describe('UncontrolledCollapse', () => {
Hello World
</UncontrolledCollapse>,
);
const element = dom.container.querySelector('div') as HTMLElement;
});

expect(element.textContent).toBe('Hello World');
it('should render text children', () => {
const element = screen.getByRole('button').parentElement as HTMLElement;

expect(element).toHaveTextContent('Hello World');
});

it('should toggle a collapse', () => {
const dom = render(
<UncontrolledCollapse
// Normally we want to display state change, not in this test, prop is passed anyway
// eslint-disable-next-line @typescript-eslint/no-unused-vars
renderTrigger={({ isOpen, ...rest }) => (
<button type="button" {...rest}>
trigger
</button>
)}
>
Hello World
</UncontrolledCollapse>,
);
const element = dom.container.querySelector('div') as HTMLElement;
const trigger = dom.container.querySelector('button') as HTMLElement;
const trigger = screen.getByRole('button') as HTMLElement;
const element = trigger.nextElementSibling as HTMLElement;

fireEvent.click(trigger);

Expand All @@ -58,4 +49,10 @@ describe('UncontrolledCollapse', () => {

expect(trigger).toHaveAttribute('aria-expanded', 'false');
});

it('should have correct id', () => {
const element = screen.getByRole('button').nextElementSibling as HTMLElement;

expect(element).toHaveAttribute('id', 'exampleId');
});
});
4 changes: 3 additions & 1 deletion packages/web-react/src/components/Collapse/demo/Collapse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ const Story: ComponentStory<typeof Collapse> = () => {
<div>
{content}
<CollapseTrigger isOpen={isOpen} onClick={toggleHandler} />
<Collapse isOpen={isOpen}>{content}</Collapse>
<Collapse id="collapseId" isOpen={isOpen}>
{content}
</Collapse>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CollapseDefault = () => {
<Button isOpen={isOpen} onClick={toggleHandler}>
Collapse trigger
</Button>
<Collapse isOpen={isOpen}>
<Collapse id="collapseDefaultId" isOpen={isOpen}>
Aliquam varius, consequat posuere a lacinia mauris eu tellus condimentum ut id ante, accumsan vehicula nulla
neque. Mauris mi orci, in donec nullam odio leo sapien et vehicula nunc a lacinia, fermentum arcu ullamcorper
posuere. Mauris euismod, ac nec ante fermentum praesent nisi commodo neque placerat, vivamus dui et tempus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const CollapseHelperClass = () => {
posuere tristique bibendum aliquam. In donec dolor ut, imperdiet quam fermentum molestie vulputate scelerisque
ac nec, tortor mi orci sollicitudin elementum.
</p>
<Collapse isOpen={isOpen}>
<Collapse id="collapseHelperClassId" isOpen={isOpen}>
Aliquam varius, consequat posuere a lacinia mauris eu tellus condimentum ut id ante, accumsan vehicula nulla
neque. Mauris mi orci, in donec nullam odio leo sapien et vehicula nunc a lacinia, fermentum arcu ullamcorper
posuere. Mauris euismod, ac nec ante fermentum praesent nisi commodo neque placerat, vivamus dui et tempus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const CollapseHideTrigger = () => {
<a href="#" role="button" onClick={toggleHandler} aria-expanded={isOpen}>
<span className="accessibility-closed">… more</span>
</a>
<Collapse isOpen={isOpen}>
<Collapse id="collapseHideTriggerId" isOpen={isOpen}>
Commodo metus a lorem, a aliquet vestibulum rutrum pharetra sapien sed, ullamcorper quis odio dolor ut aliquam.
Rutrum suspendisse, fermentum tellus metus a lorem cursus volutpat proin bibendum, sed diam a duis id dui et
tempus. Ligula non, sapien augue libero eget aliquam semper varius, posuere urna leo vitae ullamcorper.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CollapseMultipleTriggers = () => {
<Button isOpen={isOpen} onClick={toggleHandler}>
Collapse trigger
</Button>
<Collapse isOpen={isOpen}>
<Collapse id="collapseMultipleTriggersId" isOpen={isOpen}>
Aliquam varius, consequat posuere a lacinia mauris eu tellus condimentum ut id ante, accumsan vehicula nulla
neque. Mauris mi orci, in donec nullam odio leo sapien et vehicula nunc a lacinia, fermentum arcu ullamcorper
posuere. Mauris euismod, ac nec ante fermentum praesent nisi commodo neque placerat, vivamus dui et tempus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CollapseOpenOnInit = () => {
<Button isOpen={isOpen} onClick={toggleHandler}>
Collapse trigger
</Button>
<Collapse isOpen={isOpen}>
<Collapse id="collapseOpenOnInitId" isOpen={isOpen}>
Cras dictum ante, mollis ollicitudin proin bibendum nec commodo consequat fusce ante, consequat venenatis
suscipit odio morbi. Dolor sit amet porta, placerat tristique sit amet ligula nisl risus et vehicula, suscipit
accumsan nunc curabitur. Et neque, augue ut nulla a sed porta scelerisque proin, elit sapien lacinia felis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const Story: ComponentStory<typeof UncontrolledCollapse> = () => {
return (
<div>
{content}
<UncontrolledCollapse renderTrigger={CollapseTrigger}>{content}</UncontrolledCollapse>
<UncontrolledCollapse id="uncontrolledCollapseId" renderTrigger={CollapseTrigger}>
{content}
</UncontrolledCollapse>
</div>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CollapseVisibilityBreakpointDesktop = () => {
<ButtonLink isOpen={isOpenDesktop} onClick={toggleHandlerDesktop} size="medium" UNSAFE_className="d-desktop-none">
Collapse trigger
</ButtonLink>
<Collapse isOpen={isOpenDesktop} collapsibleToBreakpoint="desktop">
<Collapse id="collapseVisibilityBreakpointDesktopId" isOpen={isOpenDesktop} collapsibleToBreakpoint="desktop">
Elementum luctus ultrices, ultrices quam metus a lorem sollicitudin lorem fringilla curabitur felis a, sed urna
consectetur maximus. Imperdiet donec, elit condimentum dolor ut accumsan congue nulla ut id ante vel arcu, lorem
fringilla interdum pulvinar nullam. Curabitur interdum, semper donec dui et tempus fusce vitae ornare risus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const CollapseVisibilityBreakpointTablet = () => {
<ButtonLink isOpen={isOpenTablet} onClick={toggleHandlerTablet} size="medium" UNSAFE_className="d-tablet-none">
Collapse trigger
</ButtonLink>
<Collapse isOpen={isOpenTablet} collapsibleToBreakpoint="tablet">
<Collapse id="collapseVisibilityBreakpointTabletId" isOpen={isOpenTablet} collapsibleToBreakpoint="tablet">
Turpis cursus, urna vehicula sed porttitor nulla non mauris sapien congue, urna dui augue facilisis. Nunc elit,
ipsum porttitor curabitur sapien nulla finibus quis pulvinar, commodo convallis lorem fringilla nec. Quam
libero, vitae massa ornare eget vestibulum et iaculis quisque sapien, turpis maximus maximus vivamus. Nibh
Expand Down
3 changes: 1 addition & 2 deletions packages/web-react/src/types/collapse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ export type CollapseRenderProps = {
};

export interface BaseCollapseProps extends ChildrenProps, StyleProps {
/** @deprecated The "id" property will be required instead of optional starting from the next major version. */
id?: string;
id: string;
}

export interface CollapseProps extends BaseCollapseProps {
Expand Down

0 comments on commit 5bf5151

Please sign in to comment.