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

[EuiDualRange] Add regression test for Kibana use of ref prop #6467

Merged
merged 1 commit into from
Dec 7, 2022
Merged
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
37 changes: 34 additions & 3 deletions src/components/form/range/dual_range.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
* Side Public License, v 1.
*/

import React from 'react';
import { render } from 'enzyme';
import React, { useEffect, useRef } from 'react';
import { render, mount } from 'enzyme';
import { requiredProps } from '../../../test/required_props';
import { shouldRenderCustomStyles } from '../../../test/internal';

import { EuiForm } from '../form';
import type { EuiDualRangeProps } from './types';
import { EuiDualRange } from './dual_range';
import { EuiDualRange, EuiDualRangeClass } from './dual_range';

const props = {
min: 0,
Expand Down Expand Up @@ -209,4 +209,35 @@ describe('EuiDualRange', () => {
}
});
});

describe('ref methods', () => {
// Whether we like it or not, at least 2 Kibana instances are using EuiDualRange
// `ref`s to access the `onResize` instance method (search for `rangeRef.current?.onResize`)
// If we switch EuiDualRange to a function component, we'll need to use `useImperativeHandle`
// to allow Kibana to continue calling `onResize`
test('onResize', () => {
// This super annoying type is now required to pass both the `ref` typing and account for instance methods
type EuiDualRangeRef = React.ComponentProps<typeof EuiDualRange> &
EuiDualRangeClass;

const ConsumerDualRange = ({ callResize }: { callResize?: boolean }) => {
const rangeRef = useRef<EuiDualRangeRef>(null);

useEffect(() => {
if (callResize) rangeRef.current?.onResize(500);
}, [callResize]);

return <EuiDualRange {...props} ref={rangeRef} />;
};

const component = mount(<ConsumerDualRange />);
const instance = component
.find(EuiDualRangeClass)
.instance() as EuiDualRangeClass;

const onResizeSpy = jest.spyOn(instance, 'onResize');
component.setProps({ callResize: true }).update();
expect(onResizeSpy).toHaveBeenCalled();
});
});
});