Skip to content

Commit

Permalink
Fix: Allow to pass and override the data-testid prop of SvgIcon compo…
Browse files Browse the repository at this point in the history
…nents

The data-testid was hardcoded to `svg-icon` even it should have been
overridden in the NewIcon component.
  • Loading branch information
bjoernricks committed Mar 4, 2025
1 parent 3871dcf commit 6c02943
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/web/components/icon/NewIcon.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import withSvgIcon from 'web/components/icon/withSvgIcon';

const NewIconComponent = withSvgIcon()(Icon);

const NewIcon = props => <NewIconComponent {...props} data-testid="new-icon" />;
const NewIcon = props => <NewIconComponent data-testid="new-icon" {...props} />;

export default NewIcon;
3 changes: 2 additions & 1 deletion src/web/components/icon/SvgIcon.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const SvgIcon = ({
onClick,
size,
color = Theme.black,
['data-testid']: dataTestId = 'svg-icon',
...other
}) => {
const [loading, setLoading] = useStateWithMountCheck(false);
Expand Down Expand Up @@ -125,7 +126,7 @@ const SvgIcon = ({
$isLoading={loading}
$width={width}
color={color}
data-testid="svg-icon"
data-testid={dataTestId}
title={title}
onClick={
isDefined(onClick) && !disabled && !loading ? handleClick : undefined
Expand Down
16 changes: 14 additions & 2 deletions src/web/components/icon/__tests__/NewIcon.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import {describe} from '@gsa/testing';
import {describe, test} from '@gsa/testing';
import NewIcon from 'web/components/icon/NewIcon';
import {testIcon} from 'web/components/icon/Testing';

import {render} from 'web/utils/Testing';

describe('NewIcon component tests', () => {
testIcon(NewIcon);

test('should render with default data-testid', () => {
const {element} = render(<NewIcon />);

expect(element).toHaveAttribute('data-testid', 'new-icon');
});

test('should render with custom data-testid', () => {
const {element} = render(<NewIcon data-testid="custom-new-icon" />);

expect(element).toHaveAttribute('data-testid', 'custom-new-icon');
});
});
22 changes: 21 additions & 1 deletion src/web/components/icon/__tests__/SvgIcon.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import {describe, test, expect, testing} from '@gsa/testing';
import React, {useEffect} from 'react';
import CloneIcon from 'web/components/icon/CloneIcon';
import {useStateWithMountCheck, useIsMountedRef} from 'web/components/icon/SvgIcon';
import SvgIcon, {
useStateWithMountCheck,
useIsMountedRef,
} from 'web/components/icon/SvgIcon';
import {render, fireEvent, act} from 'web/utils/Testing';

const entity = {name: 'entity'};
Expand Down Expand Up @@ -54,6 +57,22 @@ describe('SVG icon component tests', () => {

expect(element).toHaveAttribute('title', 'Clone Entity');
});

test('should render with default data-testid', () => {
const {element} = render(<SvgIcon active={true}>{() => null}</SvgIcon>);

expect(element).toHaveAttribute('data-testid', 'svg-icon');
});

test('should render with custom data-testid', () => {
const {element} = render(
<SvgIcon active={true} data-testid="foo">
{() => null}
</SvgIcon>,
);

expect(element).toHaveAttribute('data-testid', 'foo');
});
});

describe('useStateWithMountCheck() hook tests', () => {
Expand All @@ -76,6 +95,7 @@ describe('useStateWithMountCheck() hook tests', () => {
expect(element).toHaveTextContent('false');
});
});

describe('useIsMountedRef() hook tests', () => {
test('should return false after component is unmounted', () => {
const callback = testing.fn();
Expand Down

0 comments on commit 6c02943

Please sign in to comment.