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

Fix legend size #4272

Merged
merged 3 commits into from
Mar 11, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/component/Legend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,18 @@ export class Legend extends PureComponent<Props, State> {

public getBBox() {
if (this.wrapperNode && this.wrapperNode.getBoundingClientRect) {
return this.wrapperNode.getBoundingClientRect();
const box = this.wrapperNode.getBoundingClientRect();
box.height = this.wrapperNode.offsetHeight;
box.width = this.wrapperNode.offsetWidth;
return box;
}

return null;
}

private updateBBox() {
const { onBBoxUpdate } = this.props;

if (this.wrapperNode && this.wrapperNode.getBoundingClientRect) {
const box = this.wrapperNode.getBoundingClientRect();

const box = this.getBBox();
if (box) {
if (
Math.abs(box.width - this.lastBoundingBox.width) > EPS ||
Math.abs(box.height - this.lastBoundingBox.height) > EPS
Expand Down
16 changes: 16 additions & 0 deletions test/component/Legend.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ import React from 'react';
import { render, screen } from '@testing-library/react';
import { vi } from 'vitest';
import { Legend, LineChart, Line } from '../../src';
import { mockGetBoundingClientRect, mockHTMLElementProperty } from '../helper/elementMockHelper';

describe('<Legend />', () => {
const mockRect = {
width: 300,
height: 30,
};
const scale = 2;
beforeAll(() => mockGetBoundingClientRect(mockRect));
beforeAll(() => mockHTMLElementProperty('offsetHeight', mockRect.height * scale));
beforeAll(() => mockHTMLElementProperty('offsetWidth', mockRect.width * scale));

const data = [
{ value: 'Apple', color: '#ff7300' },
{ value: 'Samsung', color: '#bb7300' },
Expand Down Expand Up @@ -111,4 +121,10 @@ describe('<Legend />', () => {
'Ex: <Bar name="Name of my Data"/>',
);
});
test('it should get the correct BBox when scale', () => {
const handleUpdate = vi.fn();
render(<Legend height={30} width={300} onBBoxUpdate={handleUpdate} />);
expect(handleUpdate.mock.calls[0][0].height).toEqual(mockRect.height * scale);
expect(handleUpdate.mock.calls[0][0].width).toEqual(mockRect.width * scale);
});
});
34 changes: 34 additions & 0 deletions test/helper/elementMockHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* getBoundingClientRect always returns 0 in jsdom, we can't test for actual returned string size
* Execution order matters
* https://github.com/jsdom/jsdom/issues/1590#issuecomment-578350151
*
* @param rect overrides getBoundingClientRect return value in the mock. jsdom by design returns all zeroes
* @returns cleanup function, convenient for beforeAll or beforeEach
*/
export function mockGetBoundingClientRect(rect: Partial<DOMRect>) {
const original = Element.prototype.getBoundingClientRect;
Element.prototype.getBoundingClientRect = () => ({
x: 0,
y: 0,
width: 0,
height: 0,
top: 0,
right: 0,
bottom: 0,
left: 0,
toJSON: () => '{}',
...rect,
});
return function cleanup() {
Element.prototype.getBoundingClientRect = original;
};
}

export function mockHTMLElementProperty(name: string, value: number) {
const original = Object.getOwnPropertyDescriptor(HTMLElement.prototype, name);
Object.defineProperty(HTMLElement.prototype, name, { configurable: true, value });
return function cleanup() {
Object.defineProperty(HTMLElement.prototype, name, original);
};
}