diff --git a/src/components/common/icon-button/__snapshots__/icon-button.test.tsx.snap b/src/components/common/icon-button/__snapshots__/icon-button.test.tsx.snap new file mode 100644 index 000000000..1d1c9096c --- /dev/null +++ b/src/components/common/icon-button/__snapshots__/icon-button.test.tsx.snap @@ -0,0 +1,116 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`IconButton correctly uses the onClick callback 1`] = ` +
+ +
+`; + +exports[`IconButton renders heart icon 1`] = ` +
+ +
+`; + +exports[`IconButton renders with additional className 1`] = ` +
+ +
+`; + +exports[`IconButton renders with border 1`] = ` +
+ +
+`; + +exports[`IconButton renders with fixed width icon 1`] = ` +
+ +
+`; diff --git a/src/components/common/icon-button/icon-button.test.tsx b/src/components/common/icon-button/icon-button.test.tsx new file mode 100644 index 000000000..a63c110ec --- /dev/null +++ b/src/components/common/icon-button/icon-button.test.tsx @@ -0,0 +1,59 @@ +/* + * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import type { IconName } from '../fork-awesome/types' +import { IconButton } from './icon-button' +import { fireEvent, render, screen } from '@testing-library/react' + +describe('IconButton', () => { + const icon: IconName = 'heart' + it('renders heart icon', () => { + const view = render(test) + expect(view.container).toMatchSnapshot() + }) + it('renders with border', () => { + const view = render( + + test with border + + ) + expect(view.container).toMatchSnapshot() + }) + it('renders with fixed width icon', () => { + const view = render( + + test with fixed with icon + + ) + expect(view.container).toMatchSnapshot() + }) + it('renders with additional className', () => { + const view = render( + + test with additional className + + ) + expect(view.container).toMatchSnapshot() + }) + it('correctly uses the onClick callback', async () => { + const onClick = jest.fn() + const view = render( + + test with onClick + + ) + expect(view.container).toMatchSnapshot() + const iconButton = await screen.findByTestId('icon-button') + fireEvent( + iconButton, + new MouseEvent('click', { + bubbles: true, + cancelable: true + }) + ) + expect(onClick).toHaveBeenCalled() + }) +}) diff --git a/src/components/common/icon-button/icon-button.tsx b/src/components/common/icon-button/icon-button.tsx index 70f836a97..a240391ef 100644 --- a/src/components/common/icon-button/icon-button.tsx +++ b/src/components/common/icon-button/icon-button.tsx @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file) + * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file) * * SPDX-License-Identifier: AGPL-3.0-only */ @@ -11,6 +11,7 @@ import { ForkAwesomeIcon } from '../fork-awesome/fork-awesome-icon' import type { IconName } from '../fork-awesome/types' import { ShowIf } from '../show-if/show-if' import styles from './icon-button.module.scss' +import { testId } from '../../../utils/test-id' export interface IconButtonProps extends ButtonProps { icon: IconName @@ -32,7 +33,8 @@ export const IconButton: React.FC = ({ {...props} className={`${styles['btn-icon']} p-0 d-inline-flex align-items-stretch ${border ? styles['with-border'] : ''} ${ className ?? '' - }`}> + }`} + {...testId('icon-button')}>