Skip to content

Commit

Permalink
test(vx-bounds): 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hshoff committed Aug 19, 2020
1 parent 89b4a01 commit a778369
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 54 deletions.
54 changes: 0 additions & 54 deletions packages/vx-bounds/test/withBoundingRects.test.ts

This file was deleted.

80 changes: 80 additions & 0 deletions packages/vx-bounds/test/withBoundingRects.test.tsx
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);
});
});

0 comments on commit a778369

Please sign in to comment.