Skip to content

Commit 12b5d4f

Browse files
committed
write and pass tests
1 parent 740a973 commit 12b5d4f

File tree

7 files changed

+3676
-135
lines changed

7 files changed

+3676
-135
lines changed

__snapshots__/index.spec.js.snap

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`the reduceProps function is called 1`] = `
4+
<Component
5+
items={
6+
Array [
7+
true,
8+
true,
9+
false,
10+
]
11+
}
12+
>
13+
<ClassComp
14+
forceUpdate={[Function]}
15+
truthyCount={2}
16+
/>
17+
</Component>
18+
`;

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function propper (transformProps) {
99
return Comp => props => {
1010
const forceUpdate = initForceUpdate()
1111

12-
return <Comp forceUpdate={forceUpdate} {...props} {...transformProps(props)} />
12+
return <Comp forceUpdate={forceUpdate} {...transformProps(props)} />
1313
}
1414
}
1515

index.spec.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React, { Component } from 'react'
2+
import ReactDOM from 'react-dom'
3+
import { mount } from 'enzyme'
4+
5+
import { propper } from './index.js'
6+
7+
class ClassComp extends Component {
8+
render () {
9+
const { counter } = this.props
10+
11+
return <>{counter}</>
12+
}
13+
}
14+
15+
const FuncComp = ({ counter }) => {
16+
return <div></div>
17+
}
18+
19+
test('the propper export is a function', () => {
20+
expect(propper).toBeInstanceOf(Function)
21+
})
22+
23+
test('the propper export is a callable function which returns a function', () => {
24+
const reduceProps = () => ({})
25+
26+
expect(propper(reduceProps)).toBeInstanceOf(Function)
27+
})
28+
29+
test('the reduceProps function is called', () => {
30+
const mockReduceProps = jest.fn(ownProps => ({
31+
truthyCount: ownProps.items.filter(a => a).length
32+
}))
33+
const ProppedComp = propper(mockReduceProps)(ClassComp)
34+
35+
const component = mount(<ProppedComp items={[true, true, false]} />)
36+
37+
expect(component).toMatchSnapshot()
38+
//console.log(component.instance().props);
39+
// expect(component.childAt(0).props()).toEqual({ truthyCount: 2, forceUpdate: () => {} })
40+
expect(Object.keys(component.childAt(0).props()).length).toEqual(2)
41+
expect(component.childAt(0).props().forceUpdate).toBeInstanceOf( Function )
42+
expect(component.childAt(0).props().truthyCount).toEqual(2)
43+
expect(mockReduceProps.mock.calls.length).toBe(1)
44+
})
45+
46+
/*
47+
const component = renderer.create(
48+
<Link page="http://www.facebook.com">Facebook</Link>,
49+
);
50+
let tree = component.toJSON();
51+
expect(tree).toMatchSnapshot();
52+
53+
// manually trigger the callback
54+
tree.props.onMouseEnter();
55+
// re-rendering
56+
tree = component.toJSON();
57+
expect(tree).toMatchSnapshot();
58+
59+
// manually trigger the callback
60+
tree.props.onMouseLeave();
61+
// re-rendering
62+
tree = component.toJSON();
63+
expect(tree).toMatchSnapshot();*/

jest.setup.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { configure } from 'enzyme'
2+
import Adapter from 'enzyme-adapter-react-16'
3+
4+
configure({ adapter: new Adapter() })

lib/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function propper(transformProps) {
3636
var forceUpdate = initForceUpdate();
3737
return _react["default"].createElement(Comp, _extends({
3838
forceUpdate: forceUpdate
39-
}, props, transformProps(props)));
39+
}, transformProps(props)));
4040
};
4141
};
42-
}
42+
}

0 commit comments

Comments
 (0)