Skip to content

Commit

Permalink
[Popover] Add test for getoffset (#6510)
Browse files Browse the repository at this point in the history
* Move getOffsetLeft and getOffsetTop to injectable handlers on Popover

* Add test coverage for getOffsetTop and getOffsetLeft
  • Loading branch information
agamrafaeli authored and mbrookes committed Apr 5, 2017
1 parent 2e26f9c commit ebd8a8e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/internal/Popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ export default class Popover extends Component {
};
}

handleGetOffsetTop = getOffsetTop;
handleGetOffsetLeft = getOffsetLeft;
/**
* Returns the top/left offset of the position
* to attach to on the anchor element (or body if none is provided)
Expand All @@ -322,8 +324,8 @@ export default class Popover extends Component {
const anchorVertical = contentAnchorOffset === 0 ? anchorOrigin.vertical : 'center';

return {
top: anchorRect.top + getOffsetTop(anchorRect, anchorVertical),
left: anchorRect.left + getOffsetLeft(anchorRect, anchorOrigin.horizontal),
top: anchorRect.top + this.handleGetOffsetTop(anchorRect, anchorVertical),
left: anchorRect.left + this.handleGetOffsetLeft(anchorRect, anchorOrigin.horizontal),
};
}

Expand Down Expand Up @@ -351,8 +353,8 @@ export default class Popover extends Component {
getTransformOrigin(elemRect, contentAnchorOffset = 0) {
const { transformOrigin } = this.props;
return {
vertical: getOffsetTop(elemRect, transformOrigin.vertical) + contentAnchorOffset,
horizontal: getOffsetLeft(elemRect, transformOrigin.horizontal),
vertical: this.handleGetOffsetTop(elemRect, transformOrigin.vertical) + contentAnchorOffset,
horizontal: this.handleGetOffsetLeft(elemRect, transformOrigin.horizontal),
};
}

Expand Down
72 changes: 72 additions & 0 deletions src/internal/Popover.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,78 @@ describe('<Popover />', () => {
wrapper.setProps({ open: false });
assert.strictEqual(wrapper.props().show, false, 'should not be open');
});

describe('getOffsetTop', () => {
let instance;
let rect;

before(() => {
instance = shallow(<Popover />).instance();
rect = {
height: 1,
};
});

it('should return vertical when vertical is a number', () => {
const vertical = 1;
const offsetTop = instance.handleGetOffsetTop('', vertical);
assert.strictEqual(offsetTop, vertical);
});

it('should return half of rect.height if vertical is \'center\'', () => {
const vertical = 'center';
const offsetTop = instance.handleGetOffsetTop(rect, vertical);
assert.strictEqual(offsetTop, rect.height / 2);
});

it('should return rect.height if vertical is \'bottom\'', () => {
const vertical = 'bottom';
const offsetTop = instance.handleGetOffsetTop(rect, vertical);
assert.strictEqual(offsetTop, rect.height);
});

it('should return zero if vertical is something else', () => {
const vertical = undefined;
const offsetTop = instance.handleGetOffsetTop(rect, vertical);
assert.strictEqual(offsetTop, 0);
});
});

describe('getOffsetLeft', () => {
let instance;
let rect;

before(() => {
instance = shallow(<Popover />).instance();
rect = {
width: 1,
};
});

it('should return horizontal when horizontal is a number', () => {
const horizontal = 1;
const offsetLeft = instance.handleGetOffsetLeft('', horizontal);
assert.strictEqual(offsetLeft, horizontal);
});

it('should return half of rect.width if horizontal is \'center\'', () => {
const horizontal = 'center';
const offsetLeft = instance.handleGetOffsetLeft(rect, horizontal);
assert.strictEqual(offsetLeft, rect.width / 2);
});

it('should return rect.width if horizontal is \'right\'', () => {
const horizontal = 'right';
const offsetLeft = instance.handleGetOffsetLeft(rect, horizontal);
assert.strictEqual(offsetLeft, rect.width);
});

it('should return zero if horizontal is something else', () => {
const horizontal = undefined;
const offsetLeft = instance.handleGetOffsetLeft(rect, horizontal);
assert.strictEqual(offsetLeft, 0);
});
});
});

describe('transition', () => {
Expand Down

0 comments on commit ebd8a8e

Please sign in to comment.