-
Notifications
You must be signed in to change notification settings - Fork 9
/
portal-popper.spec.jsx
201 lines (157 loc) · 7.14 KB
/
portal-popper.spec.jsx
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import _ from 'lodash'
import React from 'react'
import { mount, shallow } from 'enzyme'
import sinon from 'sinon'
import PortalPopper from './portal-popper'
const getProps = (props) => {
return _.extend({
className: 'tooltip',
placement: 'top',
title: 'tooltip title',
getTargetNode: sinon.stub().returns('target node'),
Popper () {
return {
scheduleUpdate () {},
}
},
}, props)
}
const popperInstanceStub = () => ({
onCreate: sinon.stub(),
onUpdate: sinon.stub(),
scheduleUpdate: sinon.spy(),
destroy: sinon.spy(),
})
const popperStub = (popperInstance) => sinon.stub().returns(popperInstance)
describe('<PortalPopper />', () => {
it('renders a <Portal /> with a placement class', () => {
const component = shallow(<PortalPopper {...getProps()} />)
expect(component.find('.tooltip-top')).to.exist
})
it('renders a <Portal /> with default styles', () => {
const component = shallow(<PortalPopper {...getProps()} />)
const style = component.find('.tooltip-top').prop('style')
expect(style.position).to.equal('absolute')
expect(style.transform).to.equal('translate3d(0px, 0px, 0)')
expect(style.WebkitTransform).to.equal('translate3d(0px, 0px, 0)')
})
it('renders the title specified', () => {
const component = shallow(<PortalPopper {...getProps()} />)
expect(component.find('span')).to.have.text('tooltip title')
})
it('renders the tooltip arrow with default styles', () => {
const component = shallow(<PortalPopper {...getProps({})} />)
const style = component.find('.tooltip-arrow').prop('style')
expect(style.left).to.equal(0)
expect(style.top).to.equal(0)
})
it('renders with className specified', () => {
const component = shallow(<PortalPopper {...getProps({ className: 'custom-class' })} />)
expect(component.find('.custom-class')).to.exist
expect(component.find('.custom-class-top')).to.exist
expect(component.find('.custom-class-arrow')).to.exist
})
it('uses last className as prefix if multiple', () => {
const component = shallow(<PortalPopper {...getProps({ className: 'custom-class tooltip' })} />)
expect(component.find('.custom-class').prop('className')).to.equal('custom-class tooltip tooltip-top')
expect(component.find('.tooltip-arrow')).to.exist
})
it('creates Popper instance with the right props', () => {
const Popper = popperStub(popperInstanceStub())
mount(<PortalPopper {...getProps({ Popper, boundary: 'boundary' })} />)
expect(Popper).to.have.been.called
expect(Popper.firstCall.args[0]).to.equal('target node')
expect(Popper.firstCall.args[2].content).to.equal('tooltip title')
expect(Popper.firstCall.args[2].placement).to.equal('top')
expect(Popper.firstCall.args[2].modifiers.preventOverflow).to.eql({
boundariesElement: 'boundary',
})
expect(Popper.firstCall.args[2].onCreate).to.be.a('function')
expect(Popper.firstCall.args[2].onUpdate).to.be.a('function')
})
it('calls scheduleUpdate() on the Popper instance', () => {
const popperInstance = popperInstanceStub()
mount(<PortalPopper {...getProps({ Popper: popperStub(popperInstance) })} />)
expect(popperInstance.scheduleUpdate).to.have.been.called
})
it('destroys the Popper instance on unmount', () => {
const popperInstance = popperInstanceStub()
const component = mount(<PortalPopper {...getProps({ Popper: popperStub(popperInstance) })} />)
component.unmount()
expect(popperInstance.destroy).to.have.been.called
})
describe('Popper#onUpate', () => {
it('updates the arrow props if specified', () => {
const popperInstance = popperInstanceStub()
const Popper = popperStub(popperInstance)
const component = mount(<PortalPopper {...getProps({ Popper })} />)
Popper.firstCall.args[2].onUpdate({ offsets: { arrow: { left: 5, top: 10 } } })
component.update()
const style = component.find('.tooltip-arrow').prop('style')
expect(style.left).to.equal(5)
expect(style.top).to.equal(10)
})
it('does not update the arrow props if not specified', () => {
const popperInstance = popperInstanceStub()
const Popper = popperStub(popperInstance)
const component = mount(<PortalPopper {...getProps({ Popper })} />)
Popper.firstCall.args[2].onUpdate({ offsets: {} })
component.update()
const style = component.find('.tooltip-arrow').prop('style')
expect(style.left).to.equal(0)
expect(style.top).to.equal(0)
})
it('only updates the arrow props that are specified', () => {
const popperInstance = popperInstanceStub()
const Popper = popperStub(popperInstance)
const component = mount(<PortalPopper {...getProps({ Popper })} />)
Popper.firstCall.args[2].onUpdate({ offsets: { arrow: { top: 20 } } })
component.update()
const style = component.find('.tooltip-arrow').prop('style')
expect(style.left).to.equal(null)
expect(style.top).to.equal(20)
})
it('rounds the arrow props', () => {
const popperInstance = popperInstanceStub()
const Popper = popperStub(popperInstance)
const component = mount(<PortalPopper {...getProps({ Popper })} />)
Popper.firstCall.args[2].onUpdate({ offsets: { arrow: { left: 7.2, top: 20.8 } } })
component.update()
const style = component.find('.tooltip-arrow').prop('style')
expect(style.left).to.equal(7)
expect(style.top).to.equal(21)
})
it('updates the popper props if specified', () => {
const popperInstance = popperInstanceStub()
const Popper = popperStub(popperInstance)
const component = mount(<PortalPopper {...getProps({ Popper })} />)
Popper.firstCall.args[2].onUpdate({ offsets: { popper: { position: 'relative', left: 2, top: 4 } } })
component.update()
const style = component.find('.tooltip-top').prop('style')
expect(style.position).to.equal('relative')
expect(style.transform).to.equal('translate3d(2px, 4px, 0)')
expect(style.WebkitTransform).to.equal('translate3d(2px, 4px, 0)')
})
it('does not update the popper props if not specified', () => {
const popperInstance = popperInstanceStub()
const Popper = popperStub(popperInstance)
const component = mount(<PortalPopper {...getProps({ Popper })} />)
Popper.firstCall.args[2].onUpdate({ offsets: {} })
component.update()
const style = component.find('.tooltip-top').prop('style')
expect(style.position).to.equal('absolute')
expect(style.transform).to.equal('translate3d(0px, 0px, 0)')
expect(style.WebkitTransform).to.equal('translate3d(0px, 0px, 0)')
})
it('rounds the popper props', () => {
const popperInstance = popperInstanceStub()
const Popper = popperStub(popperInstance)
const component = mount(<PortalPopper {...getProps({ Popper })} />)
Popper.firstCall.args[2].onUpdate({ offsets: { popper: { left: 15.2, top: 2.8 } } })
component.update()
const style = component.find('.tooltip-top').prop('style')
expect(style.transform).to.equal('translate3d(15px, 3px, 0)')
expect(style.WebkitTransform).to.equal('translate3d(15px, 3px, 0)')
})
})
})