-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor(web-react): Collapse API prop changed
- prop `hideOnCollapse`` changed to `isDisposable` - Solves DS-832
- Loading branch information
1 parent
f09a740
commit 9935692
Showing
9 changed files
with
266 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
packages/web-react/src/components/Collapse/__tests__/Collapse.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import '@testing-library/jest-dom'; | ||
import { act, fireEvent, render, screen } from '@testing-library/react'; | ||
import React, { useState } from 'react'; | ||
import { classNamePrefixProviderTest } from '../../../../tests/providerTests/classNamePrefixProviderTest'; | ||
import { restPropsTest } from '../../../../tests/providerTests/restPropsTest'; | ||
import { stylePropsTest } from '../../../../tests/providerTests/stylePropsTest'; | ||
import { Button } from '../../Button'; | ||
import Collapse from '../Collapse'; | ||
|
||
// @TODO: Missing test for collapsibleToBreakpoint | ||
|
||
describe('Collapse', () => { | ||
classNamePrefixProviderTest(Collapse, 'Collapse'); | ||
|
||
stylePropsTest(Collapse); | ||
|
||
restPropsTest(Collapse, 'div'); | ||
|
||
it('should render text children', () => { | ||
render( | ||
<Collapse id="collapse" isOpen data-testId="test"> | ||
Hello World | ||
</Collapse>, | ||
); | ||
|
||
const element = screen.getByTestId('test'); | ||
expect(element).toHaveTextContent('Hello World'); | ||
expect(element).toHaveClass('is-open'); | ||
}); | ||
|
||
it('should open collapse', () => { | ||
const RenderCollapse = () => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
return ( | ||
<> | ||
<Button type="button" data-testId="test-button" onClick={() => setIsOpen(!isOpen)}> | ||
Toggle Collapse | ||
</Button> | ||
<Collapse id="collapse" isOpen={isOpen} data-testId="test"> | ||
Hello World | ||
</Collapse> | ||
</> | ||
); | ||
}; | ||
|
||
render(<RenderCollapse />); | ||
|
||
const toggleButton = screen.getByTestId('test-button'); | ||
const collapseElement = screen.getByTestId('test'); | ||
|
||
expect(collapseElement).not.toHaveClass('is-open'); | ||
|
||
fireEvent.click(toggleButton); | ||
|
||
expect(collapseElement).toHaveClass('is-open'); | ||
}); | ||
|
||
it('should have correct html element', () => { | ||
render( | ||
<Collapse id="collapse" elementType="section" isOpen data-testId="test"> | ||
Hello World | ||
</Collapse>, | ||
); | ||
|
||
const element = screen.getByTestId('test'); | ||
|
||
expect(element.tagName).toBe('SECTION'); | ||
}); | ||
|
||
it('should respect transitionDuration prop', async () => { | ||
jest.useFakeTimers(); | ||
|
||
const RenderCollapse = ({ duration }: { duration: number }) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
return ( | ||
<> | ||
<Button type="button" data-testId="test-button" onClick={() => setIsOpen(!isOpen)}> | ||
Toggle Collapse | ||
</Button> | ||
<Collapse id="collapse" isOpen={isOpen} transitionDuration={duration} data-testId="test"> | ||
Hello World | ||
</Collapse> | ||
</> | ||
); | ||
}; | ||
|
||
const transitionDuration = 250; | ||
|
||
render(<RenderCollapse duration={transitionDuration} />); | ||
|
||
const toggleButton = screen.getByTestId('test-button'); | ||
const collapseElement = screen.getByTestId('test'); | ||
|
||
expect(collapseElement).not.toHaveClass('is-open'); | ||
|
||
fireEvent.click(toggleButton); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(transitionDuration - 1); | ||
}); | ||
|
||
expect(collapseElement).toHaveClass('is-transitioning'); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(1); | ||
}); | ||
|
||
expect(collapseElement).not.toHaveClass('is-transitioning'); | ||
expect(collapseElement).toHaveClass('is-open'); | ||
|
||
jest.useRealTimers(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 20 additions & 7 deletions
27
packages/web-react/src/components/Collapse/demo/CollapseUncontrolled.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,32 @@ | ||
// Because there is no `dist` directory during the CI run | ||
/* eslint-disable import/no-extraneous-dependencies, import/extensions, import/no-unresolved */ | ||
import { StoryFn } from '@storybook/react'; | ||
import React from 'react'; | ||
import { Button } from '../../Button'; | ||
import UncontrolledCollapse from '../UncontrolledCollapse'; | ||
import { CollapseTrigger, content } from './Collapse'; | ||
|
||
const Story: StoryFn<typeof UncontrolledCollapse> = () => { | ||
const UncontrolledCollapseDemo = () => { | ||
const content = ( | ||
<p> | ||
Condimentum odio, pulvinar et sollicitudin accumsan ac hendrerit vestibulum commodo, molestie laoreet dui sit | ||
amet. Molestie consectetur, sed ac felis scelerisque lectus accumsan purus id dolor sed vitae, praesent aliquam | ||
dolor quis ornare. Nulla sit amet, rhoncus at quis odio et iaculis lacinia suscipit vivamus sodales, nunc id | ||
condimentum felis. Consectetur nec commodo, praesent et elit magna purus molestie cursus molestie, libero ut | ||
venenatis erat id et nisi. Quam posuere, aliquam quam leo vitae tellus semper eget nunc, ultricies adipiscing sit | ||
amet accumsan. Lorem rutrum, porttitor ante mauris suspendisse ultricies consequat purus, congue a commodo magna | ||
et. | ||
</p> | ||
); | ||
|
||
return ( | ||
<div> | ||
{content} | ||
<UncontrolledCollapse id="uncontrolled-collapse-id" renderTrigger={CollapseTrigger}> | ||
<UncontrolledCollapse | ||
id="uncontrolled-collapse-id" | ||
renderTrigger={(props) => <Button {...props}>… more</Button>} | ||
isDisposable | ||
> | ||
{content} | ||
</UncontrolledCollapse> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Story; | ||
export default UncontrolledCollapseDemo; |
Oops, something went wrong.