Skip to content
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
88 changes: 88 additions & 0 deletions src/components/table/__tests__/makeSelectable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,25 @@ describe('components/table/makeSelectable', () => {
testClassNamePreventsArrowNavigation(className, hotKey);
},
);

test('should not set focus if element with role="menu" is rendered', () => {
const wrapper = getWrapper({
selectedItems: ['a'],
});

jest.spyOn(document, 'querySelector').mockImplementation(selector => {
if (selector === '[role="menu"]') {
return document.createElement('div'); // mock element found
}
return null;
});

wrapper.setState({ focusedIndex: undefined });
const instance = wrapper.instance();
const shortcut = instance.getHotkeyConfigs().find(h => h.get('key') === hotKey);
shortcut.handler({ preventDefault: sandbox.stub() });
expect(wrapper.state('focusedIndex')).toEqual(undefined);
});
});

describe('up', () => {
Expand Down Expand Up @@ -766,6 +785,25 @@ describe('components/table/makeSelectable', () => {
testClassNamePreventsArrowNavigation(className, hotKey);
},
);

test('should not set focus if element with role="menu" is rendered', () => {
const wrapper = getWrapper({
selectedItems: ['a'],
});

jest.spyOn(document, 'querySelector').mockImplementation(selector => {
if (selector === '[role="menu"]') {
return document.createElement('div');
}
return null;
});

wrapper.setState({ focusedIndex: 1 });
const instance = wrapper.instance();
const shortcut = instance.getHotkeyConfigs().find(h => h.get('key') === hotKey);
shortcut.handler({ preventDefault: sandbox.stub() });
expect(wrapper.state('focusedIndex')).toEqual(1);
});
});

describe('shift+down', () => {
Expand Down Expand Up @@ -1418,6 +1456,56 @@ describe('components/table/makeSelectable', () => {
});
});

describe('isDropdownMenuOpen()', () => {
test('should return true when .dropdown-menu-element exists', () => {
const wrapper = getWrapper();
const instance = wrapper.instance();

jest.spyOn(document, 'querySelector').mockImplementation(selector => {
if (selector === '.dropdown-menu-element') {
return document.createElement('div');
}
return null;
});

expect(instance.isDropdownMenuOpen()).toBe(true);
});

test('should return true when element with role="menu" exists', () => {
const wrapper = getWrapper();
const instance = wrapper.instance();

jest.spyOn(document, 'querySelector').mockImplementation(selector => {
if (selector === '[role="menu"]') {
return document.createElement('div');
}
return null;
});

expect(instance.isDropdownMenuOpen()).toBe(true);
});

test('should return true when both .dropdown-menu-element and role="menu" exist', () => {
const wrapper = getWrapper();
const instance = wrapper.instance();

jest.spyOn(document, 'querySelector').mockImplementation(() => {
return document.createElement('div');
});

expect(instance.isDropdownMenuOpen()).toBe(true);
});

test('should return false when neither .dropdown-menu-element nor role="menu" exist', () => {
const wrapper = getWrapper();
const instance = wrapper.instance();

jest.spyOn(document, 'querySelector').mockImplementation(() => null);

expect(instance.isDropdownMenuOpen()).toBe(false);
});
});

describe('render()', () => {
test('should add "is-selectable" class and pass props to table', () => {
const wrapper = getWrapper({});
Expand Down
4 changes: 3 additions & 1 deletion src/components/table/makeSelectable.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,9 @@ function makeSelectable(BaseTable) {

isFlyoutOpen = () => document.querySelector('.flyout-overlay') !== null;

isDropdownMenuOpen = () => document.querySelector('.dropdown-menu-element') !== null;
isDropdownMenuOpen = () =>
document.querySelector('.dropdown-menu-element') !== null ||
document.querySelector('[role="menu"]') !== null;

shouldNotAllowArrowKeyNavigation = event =>
this.isTargetQuickSearch(event) || this.isFlyoutOpen() || this.isDropdownMenuOpen();
Expand Down