Skip to content

Commit b790e0d

Browse files
committed
Merge pull request react-bootstrap#1067 from taion/position-test
Properly test position calculation
2 parents b18ccef + 8e6bd63 commit b790e0d

2 files changed

Lines changed: 125 additions & 92 deletions

File tree

test/PositionSpec.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
import pick from 'lodash/object/pick';
12
import React from 'react';
23
import ReactTestUtils from 'react/lib/ReactTestUtils';
4+
35
import Position from '../src/Position';
46
import overlayPositionUtils from '../src/utils/overlayPositionUtils';
57

8+
import {render} from './helpers';
9+
610
describe('Position', function () {
711
it('Should output a child', function () {
812
let instance = ReactTestUtils.renderIntoDocument(
@@ -89,5 +93,126 @@ describe('Position', function () {
8993
});
9094
});
9195

96+
describe('position calculation', function () {
97+
let mountPoint;
98+
99+
beforeEach(function () {
100+
mountPoint = document.createElement('div');
101+
document.body.appendChild(mountPoint);
102+
});
103+
104+
afterEach(function () {
105+
React.unmountComponentAtNode(mountPoint);
106+
document.body.removeChild(mountPoint);
107+
});
108+
109+
function checkPosition(placement, targetPosition, expected) {
110+
class FakeOverlay extends React.Component {
111+
render() {
112+
return (
113+
<div style={{
114+
position: 'absolute',
115+
width: 200,
116+
height: 200
117+
}} />
118+
);
119+
}
120+
}
121+
122+
class FakeContainer extends React.Component {
123+
render() {
124+
return (
125+
<div style={{
126+
position: 'relative',
127+
width: 600,
128+
height: 600
129+
}}>
130+
<div ref="target" style={{
131+
position: 'absolute',
132+
width: 100,
133+
height: 100,
134+
...targetPosition
135+
}}/>
136+
137+
<Position
138+
target={() => React.findDOMNode(this.refs.target)}
139+
container={this}
140+
containerPadding={50}
141+
placement={placement}
142+
>
143+
<FakeOverlay ref="overlay" />
144+
</Position>
145+
</div>
146+
);
147+
}
148+
}
149+
150+
const expectedPosition = {
151+
positionLeft: expected[0],
152+
positionTop: expected[1],
153+
arrowOffsetLeft: expected[2],
154+
arrowOffsetTop: expected[3]
155+
};
156+
157+
it('Should calculate the correct position', function() {
158+
const instance = render(<FakeContainer />, mountPoint);
159+
160+
const calculatedPosition = pick(
161+
instance.refs.overlay.props, Object.keys(expectedPosition)
162+
);
163+
expect(calculatedPosition).to.eql(expectedPosition);
164+
});
165+
}
166+
167+
[
168+
{
169+
placement: 'left',
170+
noOffset: [50, 200, null, '50%'],
171+
offsetBefore: [-200, 50, null, '0%'],
172+
offsetAfter: [300, 350, null, '100%']
173+
},
174+
{
175+
placement: 'top',
176+
noOffset: [200, 50, '50%', null],
177+
offsetBefore: [50, -200, '0%', null],
178+
offsetAfter: [350, 300, '100%', null]
179+
},
180+
{
181+
placement: 'bottom',
182+
noOffset: [200, 350, '50%', null],
183+
offsetBefore: [50, 100, '0%', null],
184+
offsetAfter: [350, 600, '100%', null]
185+
},
186+
{
187+
placement: 'right',
188+
noOffset: [350, 200, null, '50%'],
189+
offsetBefore: [100, 50, null, '0%'],
190+
offsetAfter: [600, 350, null, '100%']
191+
}
192+
].forEach(function(testCase) {
193+
const placement = testCase.placement;
194+
195+
describe(`placement = ${placement}`, function() {
196+
describe('no viewport offset', function() {
197+
checkPosition(
198+
placement, {left: 250, top: 250}, testCase.noOffset
199+
);
200+
});
201+
202+
describe('viewport offset before', function() {
203+
checkPosition(
204+
placement, {left: 0, top: 0}, testCase.offsetBefore
205+
);
206+
});
207+
208+
describe('viewport offset after', function() {
209+
checkPosition(
210+
placement, {left: 500, top: 500}, testCase.offsetAfter
211+
);
212+
});
213+
});
214+
});
215+
});
216+
92217
// ToDo: add remaining tests
93218
});

test/utils/overlayPositionUtilsSpec.js

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)