Skip to content

Commit

Permalink
chore #175 - Add IconButton tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-dassana committed Dec 10, 2020
1 parent 8e692f0 commit ec62d04
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 44 deletions.
93 changes: 61 additions & 32 deletions src/__snapshots__/storybook.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,41 @@ exports[`Storyshots Icon Predefined 1`] = `
</div>
`;

exports[`Storyshots IconButton Default 1`] = `
<div
className="light storyWrapper-0-1-2"
>
<span
className="iconButton-0-1-10"
>
<svg
aria-hidden="true"
className="svg-inline--fa fa-times fa-w-11 icon-0-1-9 icon-d0-0-1-11"
data-icon="times"
data-prefix="fas"
focusable="false"
onClick={[Function]}
role="img"
style={Object {}}
viewBox="0 0 352 512"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z"
fill="currentColor"
style={Object {}}
/>
</svg>
</span>
</div>
`;

exports[`Storyshots Input Default 1`] = `
<div
className="light storyWrapper-0-1-2"
>
<input
className="ant-input container-0-1-8 container-d0-0-1-11"
className="ant-input container-0-1-12 container-d0-0-1-15"
data-test="input"
disabled={false}
onBlur={[Function]}
Expand All @@ -253,7 +282,7 @@ exports[`Storyshots Input Error 1`] = `
className="light storyWrapper-0-1-2"
>
<input
className="ant-input container-0-1-8 container-d5-0-1-17 error-0-1-9"
className="ant-input container-0-1-12 container-d5-0-1-21 error-0-1-13"
data-test="input"
disabled={false}
onBlur={[Function]}
Expand All @@ -272,7 +301,7 @@ exports[`Storyshots Input Full Width 1`] = `
className="light storyWrapper-0-1-2"
>
<input
className="ant-input container-0-1-8 container-d4-0-1-16"
className="ant-input container-0-1-12 container-d4-0-1-20"
data-test="input"
disabled={false}
onBlur={[Function]}
Expand All @@ -291,13 +320,13 @@ exports[`Storyshots Input Loading 1`] = `
className="light storyWrapper-0-1-2"
>
<div
className="container-0-1-8 container-d3-0-1-14"
className="container-0-1-12 container-d3-0-1-18"
>
<div
className="inputSkeleton-0-1-10"
className="inputSkeleton-0-1-14"
>
<span
className="container-0-1-3 container-d1-0-1-15"
className="container-0-1-3 container-d1-0-1-19"
>
 
</span>
Expand All @@ -311,7 +340,7 @@ exports[`Storyshots Input Placeholder 1`] = `
className="light storyWrapper-0-1-2"
>
<input
className="ant-input container-0-1-8 container-d1-0-1-12"
className="ant-input container-0-1-12 container-d1-0-1-16"
data-test="input"
disabled={false}
onBlur={[Function]}
Expand Down Expand Up @@ -665,31 +694,31 @@ exports[`Storyshots Radio Group Loading 1`] = `
className="light storyWrapper-0-1-2"
>
<div
className="container-0-1-19"
className="container-0-1-23"
>
<div
className="skeletonButton-0-1-21"
className="skeletonButton-0-1-25"
>
<span
className="container-0-1-3 container-d2-0-1-22 skeleton-0-1-20"
className="container-0-1-3 container-d2-0-1-26 skeleton-0-1-24"
>
 
</span>
</div>
<div
className="skeletonButton-0-1-21"
className="skeletonButton-0-1-25"
>
<span
className="container-0-1-3 container-d3-0-1-23 skeleton-0-1-20"
className="container-0-1-3 container-d3-0-1-27 skeleton-0-1-24"
>
 
</span>
</div>
<div
className="skeletonButton-0-1-21"
className="skeletonButton-0-1-25"
>
<span
className="container-0-1-3 container-d4-0-1-24 skeleton-0-1-20"
className="container-0-1-3 container-d4-0-1-28 skeleton-0-1-24"
>
 
</span>
Expand Down Expand Up @@ -792,10 +821,10 @@ exports[`Storyshots Select Default 1`] = `
className="light storyWrapper-0-1-2"
>
<div
className="container-0-1-26 container-d0-0-1-30"
className="container-0-1-30 container-d0-0-1-34"
>
<div
className="ant-select dropdown-0-1-27 ant-select-single ant-select-show-arrow"
className="ant-select dropdown-0-1-31 ant-select-single ant-select-show-arrow"
data-test="select"
onBlur={[Function]}
onFocus={[Function]}
Expand Down Expand Up @@ -888,10 +917,10 @@ exports[`Storyshots Select Full Width 1`] = `
className="light storyWrapper-0-1-2"
>
<div
className="container-0-1-26 container-d2-0-1-32"
className="container-0-1-30 container-d2-0-1-36"
>
<div
className="ant-select dropdown-0-1-27 ant-select-single ant-select-show-arrow"
className="ant-select dropdown-0-1-31 ant-select-single ant-select-show-arrow"
data-test="select"
onBlur={[Function]}
onFocus={[Function]}
Expand Down Expand Up @@ -938,7 +967,7 @@ exports[`Storyshots Select Full Width 1`] = `
className="ant-select-selection-item"
>
<div
className="option-0-1-29"
className="option-0-1-33"
>
<span>
Lorem
Expand Down Expand Up @@ -989,10 +1018,10 @@ exports[`Storyshots Select Icon 1`] = `
className="light storyWrapper-0-1-2"
>
<div
className="container-0-1-26 container-d1-0-1-31"
className="container-0-1-30 container-d1-0-1-35"
>
<div
className="ant-select dropdown-0-1-27 ant-select-single ant-select-show-arrow"
className="ant-select dropdown-0-1-31 ant-select-single ant-select-show-arrow"
data-test="select"
onBlur={[Function]}
onFocus={[Function]}
Expand Down Expand Up @@ -1039,10 +1068,10 @@ exports[`Storyshots Select Icon 1`] = `
className="ant-select-selection-item"
>
<div
className="option-0-1-29"
className="option-0-1-33"
>
<span
className="icon-0-1-28"
className="icon-0-1-32"
>
<svg
className=""
Expand Down Expand Up @@ -1102,10 +1131,10 @@ exports[`Storyshots Select Search 1`] = `
className="light storyWrapper-0-1-2"
>
<div
className="container-0-1-26 container-d3-0-1-33"
className="container-0-1-30 container-d3-0-1-37"
>
<div
className="ant-select dropdown-0-1-27 ant-select-single ant-select-show-arrow ant-select-show-search"
className="ant-select dropdown-0-1-31 ant-select-single ant-select-show-arrow ant-select-show-search"
data-test="select"
onBlur={[Function]}
onFocus={[Function]}
Expand Down Expand Up @@ -1198,7 +1227,7 @@ exports[`Storyshots Skeleton Circle 1`] = `
className="light storyWrapper-0-1-2"
>
<span
className="container-0-1-3 container-d6-0-1-35"
className="container-0-1-3 container-d6-0-1-39"
>
 
</span>
Expand All @@ -1210,27 +1239,27 @@ exports[`Storyshots Skeleton Count 1`] = `
className="light storyWrapper-0-1-2"
>
<span
className="container-0-1-3 container-d7-0-1-36"
className="container-0-1-3 container-d7-0-1-40"
>
 
</span>
<span
className="container-0-1-3 container-d7-0-1-36"
className="container-0-1-3 container-d7-0-1-40"
>
 
</span>
<span
className="container-0-1-3 container-d7-0-1-36"
className="container-0-1-3 container-d7-0-1-40"
>
 
</span>
<span
className="container-0-1-3 container-d7-0-1-36"
className="container-0-1-3 container-d7-0-1-40"
>
 
</span>
<span
className="container-0-1-3 container-d7-0-1-36"
className="container-0-1-3 container-d7-0-1-40"
>
 
</span>
Expand All @@ -1242,7 +1271,7 @@ exports[`Storyshots Skeleton Default 1`] = `
className="light storyWrapper-0-1-2"
>
<span
className="container-0-1-3 container-d5-0-1-34"
className="container-0-1-3 container-d5-0-1-38"
>
 
</span>
Expand Down
23 changes: 23 additions & 0 deletions src/components/IconButton/IconButton.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { action } from '@storybook/addon-actions'
import React from 'react'
import { IconButton, IconButtonProps, IconSizes } from './index'
import { Meta, Story } from '@storybook/react/types-6-0'

export default {
argTypes: {
icon: { control: { disable: true } },
onClick: { defaultValue: action('onClick') },
size: {
control: {
options: [IconSizes.xs, IconSizes.sm, IconSizes.lg],
type: 'select'
}
}
},
component: IconButton,
title: 'IconButton'
} as Meta

const Template: Story<IconButtonProps> = args => <IconButton {...args} />

export const Default = Template.bind({})
53 changes: 53 additions & 0 deletions src/components/IconButton/IconButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import React from 'react'
import { IconButton, IconSizes } from './index'
import { mount, ReactWrapper } from 'enzyme'

let wrapper: ReactWrapper
let mockOnClick: jest.Mock<void>

beforeEach(() => {
mockOnClick = jest.fn()
wrapper = mount(<IconButton />)
})

afterEach(() => {
wrapper.unmount()
})

describe('IconButton', () => {
it('renders', () => {
const iconBtn = wrapper.find(IconButton)

expect(iconBtn).toHaveLength(1)
})

it('passes onClick props to FontAwesomeIcon if one is provided', () => {
wrapper = mount(<IconButton onClick={mockOnClick} />)

expect(wrapper.find(FontAwesomeIcon).props().onClick).toBeTruthy()
})

it('runs onClick function when IconButton is clicked', () => {
wrapper = mount(<IconButton onClick={mockOnClick} />)

const fontAwesomeIcon = wrapper.find(FontAwesomeIcon)

fontAwesomeIcon.simulate('click')
expect(mockOnClick).toHaveBeenCalledTimes(1)
})

it('inherits fontSize if size prop is not provided', () => {
const styles = window.getComputedStyle(wrapper.find('svg').getDOMNode())

expect(styles.fontSize).toBe('inherit')
})

it('renders IconButton with fontSize equal to size if size prop is provided', () => {
wrapper = mount(<IconButton size={IconSizes.xs} />)

const styles = window.getComputedStyle(wrapper.find('svg').getDOMNode())

expect(styles.fontSize).toBe(`${IconSizes.xs}px`)
})
})
17 changes: 10 additions & 7 deletions src/components/IconButton/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import cn from 'classnames'
import { createUseStyles } from 'react-jss'
import { faTimes } from '@fortawesome/free-solid-svg-icons'
import { generateThemedIconBtnStyles } from './utils'
import {
FontAwesomeIcon,
FontAwesomeIconProps
} from '@fortawesome/react-fontawesome'
import {
generateThemedHasBorderStyles,
generateThemedIconBtnStyles
} from './utils'
import React, { FC, SyntheticEvent } from 'react'
import { styleguide, ThemeType } from 'components/assets/styles'

Expand All @@ -15,7 +18,7 @@ const { light, dark } = ThemeType
const useStyles = createUseStyles({
hasBorder: {
...flexCenter,
border: '1px solid',
...generateThemedHasBorderStyles(light),
borderRadius,
height: spacing.xl,
width: spacing.xl
Expand All @@ -32,6 +35,7 @@ const useStyles = createUseStyles({
// eslint-disable-next-line sort-keys
'@global': {
[`.${dark}`]: {
'& $hasBorder': generateThemedHasBorderStyles(dark),
'& $iconButton': generateThemedIconBtnStyles(dark)
}
}
Expand All @@ -43,22 +47,21 @@ export enum IconSizes {
'lg' = font.h2.fontSize
}

interface Props {
export interface IconButtonProps {
classes?: string[]
hasBorder?: boolean
icon?: FontAwesomeIconProps['icon']
onClick?: (e?: SyntheticEvent) => void
size?: number
}

/* NOTE: This will be moved to web-components eventually */
export const IconButton: FC<Props> = ({
export const IconButton: FC<IconButtonProps> = ({
classes = [],
hasBorder = false,
icon = faTimes,
onClick,
size
}: Props) => {
}: IconButtonProps) => {
const componentClasses = useStyles({ size })

const iconBtnClasses = cn(
Expand All @@ -69,7 +72,7 @@ export const IconButton: FC<Props> = ({
classes
)

const optionalProps: Pick<Props, 'onClick'> = {}
const optionalProps: Pick<IconButtonProps, 'onClick'> = {}

if (onClick) {
optionalProps.onClick = onClick
Expand Down
Loading

0 comments on commit ec62d04

Please sign in to comment.