-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcombine.js
88 lines (70 loc) · 2.55 KB
/
combine.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
jest.unmock('../src')
jest.useRealTimers()
import React from 'react'
import PropTypes from 'prop-types'
import Enzyme, { mount } from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
import Policy, { combine as policies } from '../src'
Enzyme.configure({ adapter: new Adapter() })
describe('Combine', () => {
it('should show component if combined policy validates true', () => {
const Dumb = props => (<div />)
const first = Policy({ test: props => props.first })
const second = Policy({ test: props => props.second })
const PoliciedComponent = policies(first, second)(Dumb)
const Wrapper = mount(<PoliciedComponent />)
expect(Wrapper.find(Dumb).length).toBe(0)
Wrapper.setProps({ first: true })
expect(Wrapper.find(Dumb).length).toBe(0)
Wrapper.setProps({ second: true })
expect(Wrapper.find(Dumb).length).toBe(1)
})
it('should hide component if at least one combined policy validates false', () => {
const Dumb = props => (<div />)
const first = Policy({ test: props => props.first })
const second = Policy({ test: props => props.second })
const PoliciedComponent = policies(first, second)(Dumb)
const Wrapper = mount(<PoliciedComponent />)
expect(Wrapper.find(Dumb).length).toBe(0)
Wrapper.setProps({ first: true })
expect(Wrapper.find(Dumb).length).toBe(0)
})
it('should have combined policy contexts available to components', () => {
class Dumb extends React.Component {
static contextTypes = {
policy: PropTypes.object,
}
render () {
return (
<dl>
{ Object.keys(this.context.policy || {}).map((name, key) => (
<div key={ key }>
<dt>{ name }</dt>
<dd>{ JSON.stringify(this.context.policy[name]) }</dd>
</div>
)) }
</dl>
)
}
}
const first = Policy({
test: props => props.first,
isTesting: props => !props.first,
name: 'first',
preview: true
})
const second = Policy({
test: props => props.second,
isTesting: props => !props.second,
name: 'second',
preview: true
})
const PoliciedComponent = policies(first, second)(Dumb)
const Wrapper = mount(<PoliciedComponent />)
expect(Wrapper.text()).toContain('first{"testing":true')
expect(Wrapper.text()).toContain('second{"testing":true')
Wrapper.setProps({ first: true, second: true })
expect(Wrapper.text()).toContain('second{"testing":false')
expect(Wrapper.text()).toContain('first{"testing":false')
})
})