|
| 1 | +import React from 'react'; |
| 2 | +import { fireEvent, render } from '@testing-library/react'; |
| 3 | +import { Button } from '../../src'; |
| 4 | + |
| 5 | +describe('Button', () => { |
| 6 | + it('render without crashing', () => { |
| 7 | + const { getByText } = render(<Button>button</Button>); |
| 8 | + expect(getByText('button')).toBeInTheDocument(); |
| 9 | + }); |
| 10 | + it('matches snapshot', () => { |
| 11 | + const { asFragment } = render(<Button variant="primary">button</Button>); |
| 12 | + expect(asFragment()).toMatchSnapshot(); |
| 13 | + }); |
| 14 | + it('should disabled button', () => { |
| 15 | + const { getByTestId } = render( |
| 16 | + <Button data-testid="disabled" disabled={true}> |
| 17 | + Button |
| 18 | + </Button> |
| 19 | + ); |
| 20 | + expect(getByTestId('disabled')).toBeDisabled(); |
| 21 | + }); |
| 22 | + it('should render all variant', () => { |
| 23 | + const { asFragment } = render( |
| 24 | + <> |
| 25 | + <Button variant="primary">primary</Button> |
| 26 | + <Button variant="secondary">secondary</Button> |
| 27 | + </> |
| 28 | + ); |
| 29 | + expect(asFragment()).toMatchSnapshot(); |
| 30 | + }); |
| 31 | + it('should render all sizes', () => { |
| 32 | + const { asFragment } = render( |
| 33 | + <> |
| 34 | + <Button size="xs">xs</Button> |
| 35 | + <Button size="sm">sm</Button> |
| 36 | + <Button size="md">md</Button> |
| 37 | + <Button size="lg">lg</Button> |
| 38 | + </> |
| 39 | + ); |
| 40 | + expect(asFragment()).toMatchSnapshot(); |
| 41 | + }); |
| 42 | + it('onClick event fire', () => { |
| 43 | + let count = 0; |
| 44 | + |
| 45 | + const { getByTestId } = render( |
| 46 | + <Button data-testid="click" onClick={() => (count = 1)}> |
| 47 | + increment |
| 48 | + </Button> |
| 49 | + ); |
| 50 | + fireEvent.click(getByTestId('click')); |
| 51 | + expect(count).toBe(1); |
| 52 | + }); |
| 53 | + it('onClick event fire on disabled button', () => { |
| 54 | + let count = 0; |
| 55 | + |
| 56 | + const { getByTestId } = render( |
| 57 | + <Button |
| 58 | + data-testid="click-disabled" |
| 59 | + disabled={true} |
| 60 | + onClick={() => (count = 1)} |
| 61 | + > |
| 62 | + increment |
| 63 | + </Button> |
| 64 | + ); |
| 65 | + fireEvent.click(getByTestId('click-disabled')); |
| 66 | + expect(count).toBe(0); |
| 67 | + }); |
| 68 | +}); |
0 commit comments