|
| 1 | +import pick from 'lodash/object/pick'; |
1 | 2 | import React from 'react'; |
2 | 3 | import ReactTestUtils from 'react/lib/ReactTestUtils'; |
| 4 | + |
3 | 5 | import Position from '../src/Position'; |
4 | 6 | import overlayPositionUtils from '../src/utils/overlayPositionUtils'; |
5 | 7 |
|
| 8 | +import {render} from './helpers'; |
| 9 | + |
6 | 10 | describe('Position', function () { |
7 | 11 | it('Should output a child', function () { |
8 | 12 | let instance = ReactTestUtils.renderIntoDocument( |
@@ -89,5 +93,126 @@ describe('Position', function () { |
89 | 93 | }); |
90 | 94 | }); |
91 | 95 |
|
| 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 | + |
92 | 217 | // ToDo: add remaining tests |
93 | 218 | }); |
0 commit comments