-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.js
97 lines (79 loc) · 2.93 KB
/
index.spec.js
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
require('jest-dom/extend-expect')
require('react-testing-library/cleanup-after-each')
const React = require('react')
const { render, waitForElement, fireEvent } = require('react-testing-library')
const hookIntoProps = require('../dist/index.js')
describe('The `hookIntoProps` helper', () => {
it('passes props through to the wrapped Component', async () => {
const MockComponent = props => props.name
const useHooks = () => {}
const WrappedMockComponent = hookIntoProps(useHooks)(MockComponent)
const { getByText } = render(
React.createElement(WrappedMockComponent, { name: 'Jules' })
)
await waitForElement(() => getByText(/Jules/i))
})
it('calls hooks based on props', async () => {
const MockComponent = props => `Count: ${props.count}`
const useHooks = props => {
const [count] = React.useState(props.initialCount)
return { count }
}
const WrappedMockComponent = hookIntoProps(useHooks)(MockComponent)
const { getByText } = render(
React.createElement(WrappedMockComponent, { initialCount: 5 })
)
await waitForElement(() => getByText(/Count: 5/i))
})
it('updates if required', async () => {
class MockComponent extends React.Component {
constructor(props) {
super(props)
this.handleChange = this.handleChange.bind(this)
}
handleChange() {
this.props.setCount(count => count + 1)
}
render() {
return React.createElement(
'div',
{},
React.createElement('span', {}, `Count: ${this.props.count}`),
React.createElement(
'button',
{ onClick: this.handleChange },
'Click to increase count'
)
)
}
}
const useHooks = () => {
const [count, setCount] = React.useState(0)
return { count, setCount }
}
const WrappedMockComponent = hookIntoProps(useHooks)(MockComponent)
const { getByText } = render(React.createElement(WrappedMockComponent))
fireEvent.click(getByText('Click to increase count'))
await waitForElement(() => getByText(/Count: 1/i))
})
it('passes no additional props', async () => {
const MockComponent = props =>
`List of props: "${Object.keys(props).toString()}"`
const useHooks = () => {
const [count, setCount] = React.useState(0)
return { count, setCount }
}
const WrappedMockComponent = hookIntoProps(useHooks)(MockComponent)
const { getByText } = render(React.createElement(WrappedMockComponent))
await waitForElement(() => getByText(/List of props: "count,setCount"/i))
})
it('hoists statics of the wrapped Component', () => {
const MockComponent = props => null
MockComponent.foo = 'bar'
const useHooks = () => {}
const WrappedMockComponent = hookIntoProps(useHooks)(MockComponent)
const actual = WrappedMockComponent.foo
const expected = MockComponent.foo
expect(actual).toEqual(expected)
})
})