-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from kodiak-packages/14-create-button-component
Implemented the Button component.
- Loading branch information
Showing
15 changed files
with
442 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,99 @@ | ||
import React, { ComponentProps } from 'react'; | ||
import React from 'react'; | ||
import { fireEvent, render } from '@testing-library/react'; | ||
|
||
import { Button } from '../../index'; | ||
import { GitHub } from '../../index'; | ||
import Button from './Button'; | ||
|
||
describe('Button', () => { | ||
const onClickFn = jest.fn(); | ||
const defaultButtonProps: ComponentProps<typeof Button> = { | ||
type: 'primary', | ||
children: 'Click me', | ||
onClick: onClickFn, | ||
}; | ||
|
||
test('default snapshot', () => { | ||
const component = <Button {...defaultButtonProps} />; | ||
const component = <Button>Click me</Button>; | ||
const { asFragment } = render(component); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
test('secondary button', () => { | ||
const component = <Button {...defaultButtonProps} type="secondary" />; | ||
const component = <Button type="secondary">Click me</Button>; | ||
const { asFragment } = render(component); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
test('className prop', () => { | ||
const className = 'center'; | ||
const component = ( | ||
<Button className={className} name="class"> | ||
Hey | ||
</Button> | ||
); | ||
const { getByTestId } = render(component); | ||
const buttonElement = getByTestId('button-class'); | ||
|
||
const renderedClassNames = buttonElement.className.split(' '); | ||
expect(renderedClassNames).toContain(className); | ||
// className in prop should be the last in the row | ||
expect(renderedClassNames.indexOf(className)).toBe(renderedClassNames.length - 1); | ||
}); | ||
|
||
test('onClick should be triggered when clicked', async () => { | ||
const component = <Button {...defaultButtonProps} name="test" />; | ||
const { findByTestId } = render(component); | ||
const onClickFn = jest.fn(); | ||
const component = ( | ||
<Button onClick={onClickFn} name="click"> | ||
Click me | ||
</Button> | ||
); | ||
const { getByTestId } = render(component); | ||
|
||
const button = await findByTestId('test'); | ||
const button = await getByTestId('button-click'); | ||
fireEvent.click(button); | ||
|
||
expect(onClickFn).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('disabled button', () => { | ||
const onClickFn = jest.fn(); | ||
const component = ( | ||
<Button isDisabled name="disabled"> | ||
Click me | ||
</Button> | ||
); | ||
const { getByTestId } = render(component); | ||
const button = getByTestId('button-disabled'); | ||
|
||
expect(button.hasAttribute('disabled')).toBe(true); | ||
expect(button.getAttribute('disabled')).not.toBe(false); | ||
|
||
fireEvent.click(button); | ||
expect(onClickFn).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('loading button', () => { | ||
const onClickFn = jest.fn(); | ||
const component = ( | ||
<Button isLoading name="loading"> | ||
Click me | ||
</Button> | ||
); | ||
const { asFragment, getByTestId } = render(component); | ||
|
||
expect(asFragment()).toMatchSnapshot(); | ||
|
||
const button = getByTestId('button-loading'); | ||
|
||
expect(button.hasAttribute('disabled')).toBe(true); | ||
expect(button.getAttribute('disabled')).not.toBe(false); | ||
|
||
fireEvent.click(button); | ||
expect(onClickFn).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
test('prefix icon button', () => { | ||
const component = <Button prefixIcon={<GitHub />}>Click me</Button>; | ||
const { asFragment } = render(component); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
test('suffix icon button', () => { | ||
const component = <Button suffixIcon={<GitHub />}>Click me</Button>; | ||
const { asFragment } = render(component); | ||
expect(asFragment()).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.