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

Fix-folding #4256

Merged
merged 2 commits into from
Dec 23, 2024
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
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,
);
});
});
42 changes: 20 additions & 22 deletions src/web/components/folding/folding.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,34 @@
};

const foldDelay = keyframes`
0%: {
0% {
min-width: 0px;
}
100%: {
100% {
min-width: 1px;
}
`;

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 @@
? css`
${foldDelay} 0.01s
`
: undefined};
: 'none'};
`;

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

FoldingWrapper.propTypes = {
Expand Down Expand Up @@ -185,7 +183,7 @@

return (
<Component
$foldState={foldState}
foldState={foldState}
onFoldStepEnd={this.handleFoldStepEnd}
onFoldToggle={this.handleFoldToggle}
{...other}
Expand Down
Loading