Skip to content

Commit

Permalink
Merge pull request airbnb#784 from hshoff/harry-cov
Browse files Browse the repository at this point in the history
test: increase code coverage
  • Loading branch information
hshoff authored Aug 19, 2020
2 parents 1864626 + e0597bc commit d1a6e53
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 54 deletions.
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@
"coverageReporters": [
"json"
],
"collectCoverageFrom": [
"packages/src/**/*.{ts,tsx}",
"!node_modules/**",
"!packages/**/esm/**",
"!packages/**/lib/**",
"!packages/**/node_modules/**",
"!packages/vx-demo/**",
"!packages/vx-vx/**"
],
"testPathIgnorePatterns" : [
"<rootDir>/packages/vx-demo",
"<rootDir>/packages/vx-vx"
],
"coverageThreshold": {
"global": {
"branches": 0,
Expand Down
11 changes: 11 additions & 0 deletions packages/vx-annotation/test/LinePathAnnotation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,15 @@ describe('<LinePathAnnotation />', () => {
),
).toBe(true);
});

test('it should have default x and y accessors', () => {
const point = new Point({ x: 0, y: 0 });
const points = [point];
const wrapper = shallow(<LinePathAnnotation label="test" points={points} />);
const linepath = wrapper.childAt(0).props();
const x = linepath.x(point);
const y = linepath.y(point);
expect(x).toEqual(point.x);
expect(y).toEqual(point.y);
});
});
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 } 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')).toBeUndefined();
expect(RenderedComponent.prop('parentRect')).toBeUndefined();
});

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 d1a6e53

Please sign in to comment.