Skip to content

Commit

Permalink
chore: pr comments + updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Conrad Chan committed May 11, 2021
1 parent e86a3ad commit 700003f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ import noop from 'lodash/noop';
import { mount, ReactWrapper } from 'enzyme';
import useClickOutside from '../useClickOutside';

type Props = {
callback?: () => void;
};

describe('useClickOutside', () => {
function TestComponent({ callback = noop }: Props): JSX.Element {
function TestComponent({ callback = noop }: { callback?: () => void }): JSX.Element {
const ref = React.createRef<HTMLButtonElement>();

useClickOutside(ref, callback);
Expand All @@ -22,7 +18,7 @@ describe('useClickOutside', () => {
);
}

const getWrapper = (props: Props): ReactWrapper =>
const getWrapper = (props: { callback?: () => void }): ReactWrapper =>
mount(
<div>
<TestComponent {...props} />
Expand All @@ -49,7 +45,7 @@ describe('useClickOutside', () => {
element.click();
}

expect(element).toBeTruthy();
expect(element).toBeDefined();
expect(callback.mock.calls.length).toBe(isCalled ? 1 : 0);
});
});
15 changes: 7 additions & 8 deletions src/lib/viewers/controls/settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default function Settings({
setIsFocused(false);
setIsOpen(false);
}, []);
const { height, width } = activeRect || { height: 'auto', width: 'auto' };

const handleClick = (): void => {
setActiveMenu(Menu.MAIN);
Expand Down Expand Up @@ -63,8 +64,6 @@ export default function Settings({

useClickOutside(controlsElRef, resetControls);

const { height, width } = activeRect || { height: 'auto', width: 'auto' };

return (
<div
ref={controlsElRef}
Expand All @@ -74,13 +73,13 @@ export default function Settings({
{...rest}
>
<SettingsContext.Provider value={{ activeMenu, setActiveMenu, setActiveRect }}>
<SettingsToggle ref={buttonElRef} isOpen={isOpen} onClick={handleClick} />
<SettingsFlyout
className={classNames({ [`${className}-flyout`]: className })}
height={height}
<SettingsToggle
ref={buttonElRef}
className="bp-Settings-toggle"
isOpen={isOpen}
width={width}
>
onClick={handleClick}
/>
<SettingsFlyout className="bp-Settings-flyout" height={height} isOpen={isOpen} width={width}>
{children}
</SettingsFlyout>
</SettingsContext.Provider>
Expand Down
6 changes: 1 addition & 5 deletions src/lib/viewers/controls/settings/SettingsMenu.scss
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
.bp-SettingsMenu {
display: none;
display: table;
padding: 8px;

&.bp-is-active {
display: table;
}
}
7 changes: 1 addition & 6 deletions src/lib/viewers/controls/settings/SettingsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ export default function SettingsMenu({ children, className, name }: Props): JSX.
}

return (
<SettingsList
ref={menuElRef}
className={classNames('bp-SettingsMenu', className, { 'bp-is-active': isActive })}
role="menu"
tabIndex={0}
>
<SettingsList ref={menuElRef} className={classNames('bp-SettingsMenu', className)} role="menu" tabIndex={0}>
{children}
</SettingsList>
);
Expand Down
12 changes: 12 additions & 0 deletions src/lib/viewers/controls/settings/__tests__/Settings-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { mount, ReactWrapper } from 'enzyme';
import Settings from '../Settings';
import SettingsGearToggle from '../SettingsToggle';
import SettingsFlyout from '../SettingsFlyout';
import SettingsContext from '../SettingsContext';

describe('Settings', () => {
const getHostNode = (): HTMLDivElement => {
Expand Down Expand Up @@ -94,6 +95,17 @@ describe('Settings', () => {
expect(wrapper.exists(SettingsGearToggle)).toBe(true);
});

describe('flyout dimensions', () => {
test('should apply activeRect dimensions if present', () => {
const wrapper = getWrapper();

wrapper.find(SettingsGearToggle).simulate('click');

expect(wrapper.find(SettingsFlyout).prop('height')).toBe('auto');
expect(wrapper.find(SettingsFlyout).prop('width')).toBe('auto');
});
});

describe('toggle prop', () => {
function CustomToggle(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ describe('SettingsMenu', () => {
});

describe('render', () => {
test('should set classes based on the active menu', () => {
test('should render based on the active menu', () => {
const context = getContext({ activeMenu: Menu.MAIN });
const wrapper = getWrapper({ name: Menu.MAIN }, context);

expect(wrapper.childAt(0).hasClass('bp-is-active')).toBe(true);
expect(wrapper.exists()).toBe(true);

context.activeMenu = Menu.AUTOPLAY;
wrapper.setProps({}); // Force re-render
wrapper.update();

expect(wrapper.childAt(0).hasClass('bp-is-active')).toBe(false);
expect(wrapper.isEmptyRender()).toBe(true);
});

test('should return a valid wrapper', () => {
Expand Down

0 comments on commit 700003f

Please sign in to comment.