From 986e06aec056a99f26c9b119be78268d9605573a Mon Sep 17 00:00:00 2001 From: ph-fritsche <39068198+ph-fritsche@users.noreply.github.com> Date: Wed, 9 Sep 2020 01:25:25 +0200 Subject: [PATCH] refactor: test setup (#447) --- src/__tests__/helpers/utils.js | 11 ++++++--- src/__tests__/tab.js | 43 ++++++++++++---------------------- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/__tests__/helpers/utils.js b/src/__tests__/helpers/utils.js index 33ed0f44..5c773788 100644 --- a/src/__tests__/helpers/utils.js +++ b/src/__tests__/helpers/utils.js @@ -17,9 +17,14 @@ function setup(ui, {eventHandlers} = {}) { div.innerHTML = ui.trim() document.body.append(div) - const element = div.firstChild - - return {element, ...addListeners(element, {eventHandlers})} + return { + element: div.firstChild, + elements: div.children, + // for single elements add the listeners to the element for capturing non-bubbling events + ...addListeners(div.children.length === 1 ? div.firstChild : div, { + eventHandlers, + }), + } } function setupSelect({ diff --git a/src/__tests__/tab.js b/src/__tests__/tab.js index 6c75f2a0..8a4fae64 100644 --- a/src/__tests__/tab.js +++ b/src/__tests__/tab.js @@ -245,13 +245,14 @@ test('should suport a mix of elements with/without tab index', () => { }) test('ignore tabindex when active element has tabindex="-1"', () => { - const {element} = setup(` + const { + elements: [inputA, inputB, inputC, inputD], + } = setup(` `) - const [inputA, inputB, inputC, inputD] = element.parentElement.children inputB.focus() userEvent.tab() @@ -409,7 +410,9 @@ test('should keep focus on the document if there are no enabled, focusable eleme }) test('skip consecutive radios of same group', () => { - const {element} = setup(` + const { + elements: [inputA, radioA, radioB, inputB, radioC, radioD, radioE, inputC], + } = setup(` @@ -419,16 +422,6 @@ test('skip consecutive radios of same group', () => { `) - const [ - inputA, - radioA, - radioB, - inputB, - radioC, - radioD, - radioE, - inputC, - ] = element.parentElement.children inputA.focus() @@ -458,7 +451,9 @@ test('skip consecutive radios of same group', () => { }) test('skip unchecked radios if that group has a checked one', () => { - const {element} = setup(` + const { + elements: [inputA, , inputB, radioB, inputC, , inputD], + } = setup(` @@ -467,15 +462,6 @@ test('skip unchecked radios if that group has a checked one', () => { `) - const [ - inputA, - , - inputB, - radioB, - inputC, - , - inputD, - ] = element.parentElement.children inputA.focus() @@ -490,14 +476,15 @@ test('skip unchecked radios if that group has a checked one', () => { }) test('tab from active radio when another one is checked', () => { - const {element} = setup(` + const { + elements: [, , , radioB, inputC], + } = setup(` `) - const [, , , radioB, inputC] = element.parentElement.children radioB.focus() @@ -507,10 +494,10 @@ test('tab from active radio when another one is checked', () => { }) test('calls FocusEvents with relatedTarget', () => { - const {element} = setup('
') + const { + elements: [element0, element1], + } = setup('') - const element0 = element.children[0] - const element1 = element.children[1] element0.focus() const events0 = addListeners(element0) const events1 = addListeners(element1)