Skip to content

Commit

Permalink
feat(Button): Add deprecation warnings, size preferred
Browse files Browse the repository at this point in the history
* Log deprecation warnings
* Prefer size over big/small; test that interaction
  • Loading branch information
Andrew Hobson committed Jun 4, 2020
1 parent 589ec5e commit 5d91226
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/components/Button/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ export const inverse = (): React.ReactElement => (
)

export const big = (): React.ReactElement => (
<Button type="button" big>
<Button type="button" size="big">
Click Me
</Button>
)

export const small = (): React.ReactElement => (
<Button type="button" small>
<Button type="button" size="small">
Click Me
</Button>
)
Expand Down
37 changes: 33 additions & 4 deletions src/components/Button/Button.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import React from 'react'
import { render, fireEvent } from '@testing-library/react'

jest.mock('../../deprecation')
import { deprecationWarning } from '../../deprecation'
import { Button } from './Button'

describe('Button component', () => {
beforeEach(() => {
jest.clearAllMocks()
})

it('renders without errors', () => {
const { queryByTestId } = render(<Button type="button">Click Me</Button>)
expect(queryByTestId('button')).toBeInTheDocument()
Expand All @@ -15,19 +21,17 @@ describe('Button component', () => {
expect(queryByTestId('button')).toHaveClass('usa-button')
})

const optionalClasses = [
const optionalBooleanClasses = [
['secondary', 'usa-button--secondary'],
['base', 'usa-button--base'],
['accent', 'usa-button--accent-cool'],
['outline', 'usa-button--outline'],
['inverse', 'usa-button--inverse'],
['big', 'usa-button--big'],
['small', 'usa-button--small'],
['icon', 'usa-button--icon'],
['unstyled', 'usa-button--unstyled'],
]

optionalClasses.map((data) => {
optionalBooleanClasses.map((data) => {
it(`${data[1]}`, () => {
const additionalProps: { [key: string]: boolean } = {}
additionalProps[data[0]] = true
Expand All @@ -49,6 +53,7 @@ describe('Button component', () => {
)
expect(queryByTestId('button')).toHaveClass('usa-button--small')
expect(queryByTestId('button')).not.toHaveClass('usa-button--big')
expect(deprecationWarning).toHaveBeenCalledTimes(0)
})

it('renders uswds class for size big', () => {
Expand All @@ -59,6 +64,30 @@ describe('Button component', () => {
)
expect(queryByTestId('button')).toHaveClass('usa-button--big')
expect(queryByTestId('button')).not.toHaveClass('usa-button--small')
expect(deprecationWarning).toHaveBeenCalledTimes(0)
})

it('prefers size to deprecated big', () => {
Button.bind
const { queryByTestId } = render(
<Button type="button" size="small" big>
Click Me
</Button>
)
expect(queryByTestId('button')).toHaveClass('usa-button--small')
expect(queryByTestId('button')).not.toHaveClass('usa-button--big')
expect(deprecationWarning).toHaveBeenCalledTimes(1)
})

it('prefers size to deprecated small', () => {
const { queryByTestId } = render(
<Button type="button" size="big" small>
Click Me
</Button>
)
expect(queryByTestId('button')).toHaveClass('usa-button--big')
expect(queryByTestId('button')).not.toHaveClass('usa-button--small')
expect(deprecationWarning).toHaveBeenCalledTimes(1)
})
})

Expand Down
15 changes: 9 additions & 6 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import classnames from 'classnames'
import { deprecationWarning } from '../../deprecation'

interface ButtonProps {
type: 'button' | 'submit' | 'reset'
Expand Down Expand Up @@ -44,14 +45,16 @@ export const Button = (
className,
} = props

let isBig = big || false
let isSmall = small || false
if (size === 'big') {
isBig = true
if (big) {
deprecationWarning('Button property big is deprecated. Use size')
}
if (size === 'small') {
isSmall = true
if (small) {
deprecationWarning('Button property small is deprecated. Use size')
}

const isBig = size ? size === 'big' : big
const isSmall = size ? size === 'small' : small

const classes = classnames(
'usa-button',
{
Expand Down
2 changes: 2 additions & 0 deletions src/deprecation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const deprecationWarning =
process.env.NODE_ENV !== 'production' ? console.warn : () => {}

0 comments on commit 5d91226

Please sign in to comment.