Skip to content

Instantly share code, notes, and snippets.

@alpavlove
Created August 18, 2022 13:44
Show Gist options
  • Save alpavlove/6e3c64e528b8b6af39b39a8b409cb8d1 to your computer and use it in GitHub Desktop.
Save alpavlove/6e3c64e528b8b6af39b39a8b409cb8d1 to your computer and use it in GitHub Desktop.
Test React Components library
import React from 'react';
import { render, screen } from '@testing-library/react';
import { Button } from '../src/Button/Button';
describe('Button', () => {
describe('Should be rendered correctly', () => {
test('should be clickable', () => {
const onClick = jest.fn();
render(<Button onClick={onClick}>Click me</Button>);
fireEvent.click(screen.getByText('Click me'));
expect(onClick).toHaveBeenCalledTimes(1);
});
test('should not be clickable if disabled', () => {
const onClick = jest.fn();
render(
<Button onClick={onClick} isDisabled={true}>
Click me
</Button>
);
fireEvent.click(screen.getByText('Click me'));
expect(onClick).not.toHaveBeenCalled();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment