-
Notifications
You must be signed in to change notification settings - Fork 32
/
Button.test.tsx
114 lines (96 loc) · 3.35 KB
/
Button.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import '@testing-library/jest-dom/extend-expect'
import React, { FormEvent } from 'react'
import { render, RenderResult, fireEvent } from '@testing-library/react'
import { Button } from './index'
describe('Button', () => {
let wrapper: RenderResult
let onClickSpy: (event: FormEvent<HTMLButtonElement>) => void
let blurSpy: jest.SpyInstance
let button: HTMLElement
beforeEach(() => {
onClickSpy = jest.fn()
blurSpy = jest.fn()
})
describe('default props', () => {
beforeEach(() => {
wrapper = render(<Button onClick={onClickSpy}>Click me</Button>)
button = wrapper.getByText('Click me').parentElement
})
it('should style the button', () => {
expect(button.classList).toContain('rn-btn')
expect(button.classList).toContain('rn-btn--regular')
})
it('should default the type to "button"', () => {
expect(button).toHaveAttribute('type', 'button')
})
describe('when the button is clicked', () => {
beforeEach(() => {
fireEvent.click(button, {
target: {
blur: blurSpy,
},
})
})
it('should blur the button so it does not remain active', () => {
expect(blurSpy).toHaveBeenCalledTimes(1)
})
it('should handle the click event', () => {
expect(onClickSpy).toHaveBeenCalledTimes(1)
})
})
})
describe('when the onClick callback has not been specified', () => {
beforeEach(() => {
wrapper = render(<Button>Click me</Button>)
button = wrapper.getByText('Click me').parentElement
})
describe('when the button is clicked', () => {
beforeEach(() => {
fireEvent.click(button, {
target: {
blur: blurSpy,
},
})
})
it('should blur the button so it does not remain active', () => {
expect(blurSpy).toHaveBeenCalledTimes(1)
})
})
})
describe('when the size is specified', () => {
it.each`
size | expected
${'small'} | ${'rn-btn--small'}
${'regular'} | ${'rn-btn--regular'}
${'large'} | ${'rn-btn--large'}
${'xlarge'} | ${'rn-btn--xlarge'}
`('styles the button when the size is $size', ({ size, expected }) => {
wrapper = render(<Button onClick={onClickSpy} size={size}>Click me</Button>)
button = wrapper.getByText('Click me').parentElement
expect(button.classList).toContain(expected)
})
})
describe('when the type is specified', () => {
it.each`
type | expected
${'button'} | ${'button'}
${'submit'} | ${'submit'}
`('should set the type attribute to $type', ({ type, expected }) => {
wrapper = render(<Button onClick={onClickSpy} type={type}>Click me</Button>)
button = wrapper.getByText('Click me').parentElement
expect(button).toHaveAttribute('type', expected)
})
})
describe('when the variant is specified', () => {
it.each`
variant | expected
${'primary'} | ${'rn-btn--primary'}
${'secondary'} | ${'rn-btn--secondary'}
${'tertiary'} | ${'rn-btn--tertiary'}
`('styles the button when the variant is $variant', ({ variant, expected }) => {
wrapper = render(<Button onClick={onClickSpy} variant={variant}>Click me</Button>)
button = wrapper.getByText('Click me').parentElement
expect(button.classList).toContain(expected)
})
})
})