Skip to content

Commit

Permalink
Merge pull request #4699 from cloud-gov/chore-test-shared-buttonLink-…
Browse files Browse the repository at this point in the history
…4672

chore: test @shared/ButtonLink.jsx #4672
  • Loading branch information
sknep authored Dec 20, 2024
2 parents 09fb316 + 30dcfd1 commit 6753823
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
6 changes: 1 addition & 5 deletions frontend/shared/ButtonLink.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ const ButtonLink = ({ clickHandler, children, className = 'usa-button--outline'

ButtonLink.propTypes = {
clickHandler: PropTypes.func.isRequired,
children: PropTypes.node,
children: PropTypes.node.isRequired,
className: PropTypes.string,
};

ButtonLink.defaultProps = {
children: null,
};

export default ButtonLink;
37 changes: 37 additions & 0 deletions frontend/shared/ButtonLink.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';

import ButtonLink from './ButtonLink';
const clickHandler = jest.fn();

describe('<ButtonLink />', () => {
it('renders children as expected', () => {
render(<ButtonLink clickHandler={clickHandler}>child text</ButtonLink>);
const buttonElement = screen.getByRole('button');
expect(buttonElement).toHaveTextContent('child text');
});

it('calls the clickHandler when clicked', () => {
render(<ButtonLink clickHandler={clickHandler}>Click Me</ButtonLink>);
const buttonElement = screen.getByText('Click Me');
fireEvent.click(buttonElement);
expect(clickHandler).toHaveBeenCalledTimes(1);
});

it('applies the className as expected', () => {
render(
<ButtonLink clickHandler={clickHandler} className="custom-class">
Class Button
</ButtonLink>,
);
const buttonElement = screen.getByText('Class Button');
expect(buttonElement).toHaveClass('usa-button custom-class');
});

it('defaults to usa-button--outline class if no className is provided', () => {
render(<ButtonLink clickHandler={clickHandler}>No Class</ButtonLink>);
const buttonElement = screen.getByText('No Class');
expect(buttonElement).toHaveClass('usa-button--outline');
});
});

0 comments on commit 6753823

Please sign in to comment.