-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathswitch.test.js
More file actions
51 lines (45 loc) · 1.81 KB
/
Copy pathswitch.test.js
File metadata and controls
51 lines (45 loc) · 1.81 KB
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
import React from 'react'
import Switch from './switch'
import TestRenderer from 'react-test-renderer'
import { mount } from 'enzyme'
describe('Switch', () => {
test('Switch renders correctly with no options set', () => {
const testRenderer = TestRenderer.create(<Switch />)
const tree = testRenderer.toJSON()
expect(tree).toMatchSnapshot()
})
test('Switch renders correctly with all options set', () => {
const testRenderer = TestRenderer.create(
<Switch title="mocktitle" onChange={() => {}} checked={true} />
)
const tree = testRenderer.toJSON()
expect(tree).toMatchSnapshot()
})
test('Switch renders correctly with a title', () => {
const testRenderer = TestRenderer.create(<Switch title="mocktitle" />)
const testInstance = testRenderer.root
const checkbox = testInstance.findByType('span')
expect(checkbox.children).toEqual(['mocktitle'])
})
test('Switch renders when unchecked', () => {
const testRenderer = TestRenderer.create(<Switch checked={false} />)
const testInstance = testRenderer.root
const checkbox = testInstance.findByType('input')
expect(checkbox.props).toHaveProperty('checked', false)
})
test('Switch renders when checked', () => {
const testRenderer = TestRenderer.create(<Switch checked={true} />)
const testInstance = testRenderer.root
const checkbox = testInstance.findByType('input')
expect(checkbox.props).toHaveProperty('checked', true)
})
test('Switch calls onChange', () => {
const onChecked = jest.fn()
const component = mount(<Switch onChange={onChecked} />)
const checkbox = component.find('input')
expect(onChecked).not.toHaveBeenCalled()
checkbox.simulate('change', { target: { checked: true } })
expect(onChecked).toHaveBeenCalledTimes(1)
expect(onChecked).toHaveBeenCalledWith(expect.objectContaining({ target: { checked: true } }))
})
})