Skip to content

Commit

Permalink
add test and mprove code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-mng committed Dec 19, 2024
1 parent 346890c commit 34bfba7
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 19 deletions.
48 changes: 48 additions & 0 deletions src/web/components/folding/__tests__/folding.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {describe, test, expect} from '@gsa/testing';
import {render, screen, fireEvent} from 'web/utils/testing';

import {withFolding, FoldState, withFoldToggle} from '../folding';

describe('withFolding', () => {
const DummyComponent = props => <div {...props}>Dummy Component</div>;
const FoldableComponent = withFolding(DummyComponent);

test('hides content when foldState is Folded', () => {
const {rerender} = render(
<FoldableComponent foldState={FoldState.FOLDED} />,
);
expect(screen.getByText('Dummy Component')).not.toBeVisible();

rerender(<FoldableComponent foldState={FoldState.UNFOLDED} />);
expect(screen.getByText('Dummy Component')).toBeVisible();
});
});

describe('withFoldToggle', () => {
test('toggles foldState when onFolded isCalled', () => {
const DummyComponent = ({foldState, onFoldToggle}) => (
<div>
<span data-testid="foldState">{foldState}</span>
<button onClick={onFoldToggle}>Toggle</button>
</div>
);

const FoldToggleComponent = withFoldToggle(DummyComponent);

render(<FoldToggleComponent />);

expect(screen.getByTestId('foldState')).toHaveTextContent(
FoldState.UNFOLDED,
);

fireEvent.click(screen.getByText('Toggle'));
expect(screen.getByTestId('foldState')).toHaveTextContent(
FoldState.FOLDING_START,
);
});
});
36 changes: 17 additions & 19 deletions src/web/components/folding/folding.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,26 @@ const foldDelay = keyframes`
}
`;

const Div = styled.div`
const FoldableDiv = styled.div`
overflow: hidden;
transition: 0.4s;
transition: height 0.4s;
display: ${({$foldState}) =>
$foldState === FoldState.FOLDED ? 'none' : undefined};
$foldState === FoldState.FOLDED ? 'none' : 'block'};
height: ${({$foldState}) => {
if ($foldState === FoldState.FOLDED || $foldState === FoldState.FOLDING) {
return 0;
switch ($foldState) {
case FoldState.FOLDED:
case FoldState.FOLDING:
return '0px';
case FoldState.FOLDING_START:
case FoldState.UNFOLDING:
return `${Math.ceil(window.innerHeight * 1.2)}px`;

Check warning on line 45 in src/web/components/folding/folding.jsx

View check run for this annotation

Codecov / codecov/patch

src/web/components/folding/folding.jsx#L45

Added line #L45 was not covered by tests
case FoldState.UNFOLDING_START:
return '1px';

Check warning on line 47 in src/web/components/folding/folding.jsx

View check run for this annotation

Codecov / codecov/patch

src/web/components/folding/folding.jsx#L47

Added line #L47 was not covered by tests
default:
return 'auto';
}
if (
$foldState === FoldState.FOLDING_START ||
$foldState === FoldState.UNFOLDING
) {
const windowHeight = Math.ceil(window.innerHeight * 1.2) + 'px';
return windowHeight;
}
if ($foldState === FoldState.UNFOLDING_START) {
return '1px';
}
return undefined;
}};
animation: ${({$foldState}) =>
Expand All @@ -58,7 +56,7 @@ const Div = styled.div`
? css`
${foldDelay} 0.01s
`
: undefined};
: 'none'};
`;

const FoldStatePropType = PropTypes.oneOf([
Expand All @@ -81,13 +79,13 @@ export const withFolding = Component => {
onFoldToggle,
...props
}) => (
<Div
<FoldableDiv
$foldState={foldState}
onAnimationEnd={onFoldStepEnd}
onTransitionEnd={onFoldStepEnd}
>
<Component {...props} />
</Div>
</FoldableDiv>
);

FoldingWrapper.propTypes = {
Expand Down

0 comments on commit 34bfba7

Please sign in to comment.