Skip to content

Instantly share code, notes, and snippets.

@diegorondao
Forked from diiegoburiti/Button.tsx
Created September 4, 2023 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegorondao/acadd2a7d13552b21d1794bb09491046 to your computer and use it in GitHub Desktop.
Save diegorondao/acadd2a7d13552b21d1794bb09491046 to your computer and use it in GitHub Desktop.
Button test with react-testing-library and jest
import { render, screen } from '@testing-library/react'
import { renderWithTheme } from 'utils/tests/helpers'
import ButtonTest from '.'
describe('<ButtonTest />', () => {
it('should render the button', () => {
render(<ButtonTest>Dev.to</ButtonTest>)
expect(screen.getByRole('button', { name: /Dev.to/i })).toHaveStyle({
background: '#000',
color: '#fff',
opacity: 1
})
})
it('should render the disabeld button', () => {
render(<ButtonTest disabled>Dev.to</ButtonTest>)
expect(screen.getByRole('button', { name: /Dev.to/i })).toHaveStyle({
cursor: 'not-allowed',
filter: 'saturate(40%)'
})
})
})
import * as S from './styles'
export type ButtonProps = {
children: React.ReactNode
disabled?: boolean
}
const ButtonTest = ({ children, disabled = false }: ButtonTestProps) => (
<S.Wrapper disabled={disabled}>{children}</S.Wrapper>
)
export default Button
import styled from 'styled-components'
export const Wrapper = styled.button`
background-color: #000;
color: #fff;
font-size: 1.1rem;
padding: 2rem;
border: none;
border-radius: 1rem;
opacity: 1;
&:disabled {
cursor: not-allowed;
filter: saturate(40%);
}
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment