Skip to content

Commit

Permalink
chore: Make disabledReason prop applicable only for normal and primar…
Browse files Browse the repository at this point in the history
…y variants
  • Loading branch information
georgylobko committed Jul 8, 2024
1 parent 8368333 commit b675692
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 30 deletions.
77 changes: 49 additions & 28 deletions src/button-dropdown/__tests__/button-dropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,42 +100,63 @@ const items: ButtonDropdownProps.Items = [
expect(renderedItems.length).toEqual(0);
expect(wrapper.findOpenDropdown()).toBe(null);
});
});
});

(['normal', 'primary'] as Array<ButtonDropdownProps['variant']>).forEach(variant => {
describe(`"${variant}" variant 'disabled with reason`, () => {
const props = { expandToViewport, variant };

// disabledReason behavior is already tested for button component, which acts as the trigger for this component
// here we need to check the basic logic without diving deep into details
test('open tooltip on mouseenter', () => {
const wrapper = renderButtonDropdown({
...props,
items,
disabled: true,
disabledReason: 'disabled reason',
});

fireEvent.mouseEnter(wrapper.findTriggerButton()!.getElement());

expect(wrapper.findTriggerButton()!.findDisabledReason()).not.toBeNull();
expect(wrapper.findTriggerButton()!.findDisabledReason()!.getElement()).toHaveTextContent('reason');
});

describe('disabled with reason', () => {
// disabledReason behavior is already tested for button component, which acts as the trigger for this component
// here we need to check the basic logic without diving deep into details
test('open tooltip on mouseenter', () => {
const wrapper = renderButtonDropdown({
...props,
items,
disabled: true,
disabledReason: 'disabled reason',
});

fireEvent.mouseEnter(wrapper.findTriggerButton()!.getElement());

expect(wrapper.findTriggerButton()!.findDisabledReason()).not.toBeNull();
expect(wrapper.findTriggerButton()!.findDisabledReason()!.getElement()).toHaveTextContent('reason');
test('close tooltip on mouseleave', () => {
const wrapper = renderButtonDropdown({
...props,
items,
disabled: true,
disabledReason: 'disabled reason',
});

test('close tooltip on mouseleave', () => {
const wrapper = renderButtonDropdown({
...props,
items,
disabled: true,
disabledReason: 'disabled reason',
});
fireEvent.mouseEnter(wrapper.findTriggerButton()!.getElement());

fireEvent.mouseEnter(wrapper.findTriggerButton()!.getElement());
expect(wrapper.findTriggerButton()!.findDisabledReason()).not.toBeNull();
expect(wrapper.findTriggerButton()!.findDisabledReason()!.getElement()).toHaveTextContent('reason');

expect(wrapper.findTriggerButton()!.findDisabledReason()).not.toBeNull();
expect(wrapper.findTriggerButton()!.findDisabledReason()!.getElement()).toHaveTextContent('reason');
fireEvent.mouseLeave(wrapper.findTriggerButton()!.getElement());

expect(wrapper.findTriggerButton()!.findDisabledReason()).toBeNull();
});
});
});

fireEvent.mouseLeave(wrapper.findTriggerButton()!.getElement());
describe(`"icon" variant 'disabled with reason`, () => {
const props = { expandToViewport, variant: 'icon' as ButtonDropdownProps['variant'] };

expect(wrapper.findTriggerButton()!.findDisabledReason()).toBeNull();
});
test('does not open tooltip on mouseenter', () => {
const wrapper = renderButtonDropdown({
...props,
items,
disabled: true,
disabledReason: 'disabled reason',
});

fireEvent.mouseEnter(wrapper.findTriggerButton()!.getElement());

expect(wrapper.findTriggerButton()!.findDisabledReason()).toBeNull();
});
});

Expand Down
4 changes: 2 additions & 2 deletions src/button/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export const InternalButton = React.forwardRef(
checkSafeUrl('Button', href);
const isAnchor = Boolean(href);
const isNotInteractive = loading || disabled;
const hasAriaDisabled = (loading && !disabled) || (disabled && __focusable) || (disabled && !!disabledReason);
const isDisabledWithReason = !!disabledReason && disabled;
const isDisabledWithReason = (variant === 'normal' || variant === 'primary') && !!disabledReason && disabled;
const hasAriaDisabled = (loading && !disabled) || (disabled && __focusable) || isDisabledWithReason;
const shouldHaveContent =
children && ['icon', 'inline-icon', 'flashbar-icon', 'modal-dismiss'].indexOf(variant) === -1;

Expand Down

0 comments on commit b675692

Please sign in to comment.