-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.spec.js
85 lines (63 loc) · 2.69 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
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { mount } from 'enzyme'
import { propper } from './index.js'
class ClassComp extends Component {
render () {
const { truthyCount } = this.props
return <>You have {truthyCount} items available</>
}
}
const FuncComp = ({ truthyCount }) => {
return <>You have {truthyCount} items available</>
}
test('the propper export is a function', () => {
expect(propper).toBeInstanceOf(Function)
})
test('the propper export is a callable function which returns a function', () => {
const reduceProps = () => ({})
expect(propper(reduceProps)).toBeInstanceOf(Function)
})
test('the reduceProps function is called', () => {
const mockReduceProps = jest.fn(ownProps => ({
truthyCount: ownProps.items.filter(a => a).length
}))
const ProppedComp = propper(mockReduceProps)(ClassComp)
const wrapped = mount(<ProppedComp items={[true, true, false]} />)
// the reduceProp func should have been called once
expect(mockReduceProps.mock.calls.length).toBe(1)
})
test('class components properly pass through dynamic props', () => {
const mockReduceProps = jest.fn(ownProps => ({
truthyCount: ownProps.items.filter(a => a).length
}))
const ProppedComp = propper(mockReduceProps)(ClassComp)
const wrapped = mount(<ProppedComp items={[true, true, false]} />)
const comp = wrapped.childAt(0)
// ensure the wrapped tree is correct
expect(wrapped).toMatchSnapshot()
// ensre only two keys are present in resulting object
expect(Object.keys(comp.props()).length).toEqual(2)
// forceUpdate is auto-added & should be a function while truthyCount is dynamically added
expect(comp.props().forceUpdate).toBeInstanceOf(Function)
expect(comp.props().truthyCount).toEqual(2)
// the reduceProp func should have been called once
expect(mockReduceProps.mock.calls.length).toBe(1)
})
test('functional components properly pass through dynamic props', () => {
const mockReduceProps = jest.fn(ownProps => ({
truthyCount: ownProps.items.filter(a => a).length
}))
const ProppedComp = propper(mockReduceProps)(FuncComp)
const wrapped = mount(<ProppedComp items={[true, true, false]} />)
const comp = wrapped.childAt(0)
// ensure the wrapped tree is correct
expect(wrapped).toMatchSnapshot()
// ensre only two keys are present in resulting object
expect(Object.keys(comp.props()).length).toEqual(2)
// forceUpdate is auto-added & should be a function while truthyCount is dynamically added
expect(comp.props().forceUpdate).toBeInstanceOf(Function)
expect(comp.props().truthyCount).toEqual(2)
// the reduceProp func should have been called once
expect(mockReduceProps.mock.calls.length).toBe(1)
})