forked from airbnb/visx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
54 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import * as React from 'react'; | ||
import { mount, shallow } from 'enzyme'; | ||
import { withBoundingRects } from '../src'; | ||
|
||
const expectedRectShape = expect.objectContaining({ | ||
top: expect.any(Number), | ||
right: expect.any(Number), | ||
bottom: expect.any(Number), | ||
left: expect.any(Number), | ||
width: expect.any(Number), | ||
height: expect.any(Number), | ||
}); | ||
|
||
const emptyRect = { | ||
top: 0, | ||
right: 0, | ||
bottom: 0, | ||
left: 0, | ||
width: 0, | ||
height: 0, | ||
}; | ||
|
||
describe('withBoundingRects()', () => { | ||
beforeAll(() => { | ||
// mock getBoundingClientRect | ||
Element.prototype.getBoundingClientRect = jest.fn(() => ({ | ||
width: 100, | ||
height: 100, | ||
top: 0, | ||
left: 0, | ||
bottom: 0, | ||
right: 0, | ||
x: 0, | ||
y: 0, | ||
toJSON: jest.fn(), | ||
})); | ||
}); | ||
|
||
test('it should be defined', () => { | ||
expect(withBoundingRects).toBeDefined(); | ||
}); | ||
|
||
test('it should pass rect, parentRect, and getRect props to the wrapped component', () => { | ||
const Component = () => <div />; | ||
const HOC = withBoundingRects(Component); | ||
const wrapper = mount(<HOC />); | ||
const RenderedComponent = wrapper.find(Component); | ||
|
||
expect(Element.prototype.getBoundingClientRect).toHaveBeenCalled(); | ||
expect(RenderedComponent.prop('rect')).toEqual(expectedRectShape); | ||
expect(RenderedComponent.prop('parentRect')).toEqual(expectedRectShape); | ||
expect(typeof RenderedComponent.prop('getRects')).toBe('function'); | ||
}); | ||
|
||
test('it should pass additional props to the wrapped component', () => { | ||
const Component = () => <div />; | ||
const HOC = withBoundingRects(Component); | ||
// @ts-ignore | ||
const wrapper = mount(<HOC bananas="are yellow" />); | ||
const RenderedComponent = wrapper.find(Component); | ||
expect(RenderedComponent.prop('bananas')).toBe('are yellow'); | ||
}); | ||
|
||
test('it should return default empty state if no node', () => { | ||
const Component = () => null; | ||
const HOC = withBoundingRects(Component); | ||
const wrapper = mount(<HOC />); | ||
const RenderedComponent = wrapper.find(Component); | ||
expect(RenderedComponent.prop('rect')).toEqual(undefined); | ||
expect(RenderedComponent.prop('parentRect')).toEqual(undefined); | ||
}); | ||
|
||
test('it should set rect and parentRect to empty state if no getBoundingClient()', () => { | ||
const Component = () => <>''</>; | ||
const HOC = withBoundingRects(Component); | ||
const wrapper = mount(<HOC />); | ||
const RenderedComponent = wrapper.find(Component); | ||
expect(RenderedComponent.prop('rect')).toEqual(emptyRect); | ||
}); | ||
}); |