Skip to content

Commit

Permalink
Change: Refactor RowDetailsToggle
Browse files Browse the repository at this point in the history
Replace withClickHandler with useClickHandler and add data-testid for
easier testing.
  • Loading branch information
bjoernricks committed Mar 7, 2025
1 parent 81642cf commit 37822eb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/web/entities/Row.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import PropTypes from 'prop-types';
import styled from 'styled-components';
import withClickHandler from 'web/components/form/withClickHandler';
import useClickHandler from 'web/components/form/useClickHandler';
import Theme from 'web/utils/Theme';

export const RowDetailsToggle = withClickHandler()(styled.span`
const StyledSpan = styled.span`
cursor: pointer;
text-decoration: none;
color: ${Theme.blue};
Expand All @@ -18,4 +19,24 @@ export const RowDetailsToggle = withClickHandler()(styled.span`
@media print {
color: ${Theme.black};
}
`);
`;

export const RowDetailsToggle = ({name, onClick, ...props}) => {
const handleClick = useClickHandler({
onClick,
name,
nameFunc: (event, props) => props.name,
});
return (
<StyledSpan
data-testid="row-details-toggle"
{...props}
name={name}
onClick={handleClick}
/>
);
};

RowDetailsToggle.propTypes = {name: PropTypes.string, onClick: PropTypes.func};

export default RowDetailsToggle;
29 changes: 29 additions & 0 deletions src/web/entities/__tests__/RowDetailsToggle.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* SPDX-FileCopyrightText: 2025 Greenbone AG
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {describe, test, testing} from '@gsa/testing';
import RowDetailsToggle from 'web/entities/Row';
import {fireEvent, rendererWith, screen, wait} from 'web/utils/Testing';

describe('RowDetailsToggle tests', () => {
test('renders without crashing', () => {
const {render} = rendererWith();

render(<RowDetailsToggle name="test" />);

expect(screen.getByTestId('row-details-toggle')).toBeInTheDocument();
});

test('calls onClick handler when row is clicked', async () => {
const handleClick = testing.fn();
const {render} = rendererWith();

render(<RowDetailsToggle name="test" onClick={handleClick} />);

const row = screen.getByTestId('row-details-toggle');
fireEvent.click(row);
expect(handleClick).toHaveBeenCalledWith(undefined, 'test');
});
});

0 comments on commit 37822eb

Please sign in to comment.