Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: increase code coverage #784

Merged
merged 6 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use it(...) function instead of test(...) too so it will read more like

it('should have default x and y accessors', ...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, i don't have a preference

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);
Copy link
Member Author

@hshoff hshoff Aug 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 wasn't sure how to mock the RenderComponent.node.parentNode to not have getBoundingRect which triggers the parentRect = emptyRect branch... so skipped it for now. Safe to assume it works.

});
});