11import { expect } from '@esm-bundle/chai' ;
2- import { aTimeout , fixtureSync , focusin , focusout , keyboardEventFor , keyDownOn } from '@vaadin/testing-helpers' ;
2+ import { aTimeout , fixtureSync , focusin , focusout , keyDownOn } from '@vaadin/testing-helpers' ;
33import { resetMouse , sendKeys , sendMouse } from '@web/test-runner-commands' ;
44import sinon from 'sinon' ;
55import { html , PolymerElement } from '@polymer/polymer/polymer-element.js' ;
@@ -12,6 +12,7 @@ customElements.define(
1212 return html `
1313 < input id ="input " />
1414 < input id ="secondInput " />
15+ < input id ="thirdInput " />
1516 < slot > </ slot >
1617 ` ;
1718 }
@@ -35,6 +36,13 @@ customElements.define(
3536 }
3637) ;
3738
39+ async function click ( element ) {
40+ const rect = element . getBoundingClientRect ( ) ;
41+ const middleX = Math . floor ( rect . x + rect . width / 2 ) ;
42+ const middleY = Math . floor ( rect . y + rect . height / 2 ) ;
43+ await sendMouse ( { type : 'click' , position : [ middleX , middleY ] } ) ;
44+ }
45+
3846describe ( 'shadow-focus-mixin' , ( ) => {
3947 let customElement , focusElement ;
4048
@@ -97,19 +105,6 @@ describe('shadow-focus-mixin', () => {
97105 } ) ;
98106
99107 describe ( 'focus-ring' , ( ) => {
100- it ( 'should set _isShiftTabbing when pressing shift-tab' , ( ) => {
101- const event = keyboardEventFor ( 'keydown' , 9 , 'shift' ) ;
102- customElement . dispatchEvent ( event ) ;
103- expect ( customElement . _isShiftTabbing ) . to . be . true ;
104- } ) ;
105-
106- it ( 'should skip setting _isShiftTabbing if event is defaultPrevented' , ( ) => {
107- const evt = keyboardEventFor ( 'keydown' , 9 , 'shift' ) ;
108- evt . preventDefault ( ) ;
109- customElement . dispatchEvent ( evt ) ;
110- expect ( customElement . _isShiftTabbing ) . not . to . be . ok ;
111- } ) ;
112-
113108 it ( 'should set the focus-ring attribute when TAB is pressed and focus is received' , ( ) => {
114109 keyDownOn ( document . body , 9 ) ;
115110 focusin ( focusElement ) ;
@@ -198,11 +193,6 @@ describe('shadow-focus-mixin', () => {
198193 expect ( spy . calledOnce ) . to . be . true ;
199194 } ) ;
200195
201- it ( 'should not set focused attribute on host click' , ( ) => {
202- customElement . click ( ) ;
203- expect ( customElement . hasAttribute ( 'focused' ) ) . to . be . false ;
204- } ) ;
205-
206196 it ( 'should set focused attribute on focusin event' , ( ) => {
207197 focusin ( focusElement ) ;
208198 expect ( customElement . hasAttribute ( 'focused' ) ) . to . be . true ;
@@ -213,6 +203,7 @@ describe('shadow-focus-mixin', () => {
213203 const spy = sinon . spy ( focusElement , 'focus' ) ;
214204 await sendKeys ( { press : 'Tab' } ) ;
215205 expect ( spy . called ) . to . be . true ;
206+ expect ( customElement . hasAttribute ( 'focused' ) ) . to . be . true ;
216207 } ) ;
217208
218209 it ( 'should not delegate focus when clicking on non-focusable child' , async ( ) => {
@@ -221,21 +212,48 @@ describe('shadow-focus-mixin', () => {
221212 customElement . appendChild ( span ) ;
222213
223214 const spy = sinon . spy ( focusElement , 'focus' ) ;
224- // Click center of span child element
225- const rect = span . getBoundingClientRect ( ) ;
226- const middleX = Math . floor ( rect . x + rect . width / 2 ) ;
227- const middleY = Math . floor ( rect . y + rect . height / 2 ) ;
228- await sendMouse ( { type : 'click' , position : [ middleX , middleY ] } ) ;
229- // Clicking on some text content should not move focus
215+ await click ( span ) ;
216+ // Clicking on some text content should not move focus to focus element
230217 expect ( spy . called ) . to . be . false ;
231218 } ) ;
232219
233220 it ( 'should not delegate focus if the focus is not received from outside' , ( ) => {
234- const child = document . createElement ( 'div' ) ;
235- customElement . appendChild ( child ) ;
221+ customElement . focusElement . focus ( ) ;
222+ const spy = sinon . spy ( focusElement , 'focus' ) ;
223+ customElement . $ . secondInput . focus ( ) ;
224+ customElement . $ . thirdInput . focus ( ) ;
225+ customElement . $ . secondInput . focus ( ) ;
226+ expect ( spy . called ) . to . be . false ;
227+ } ) ;
236228
237- focusin ( customElement , child ) ;
229+ it ( 'should skip host element in tab order when shift-tabbing' , async ( ) => {
230+ const siblingInput = document . createElement ( 'input' ) ;
231+ customElement . parentElement . insertBefore ( siblingInput , customElement ) ;
238232
233+ customElement . focusElement . focus ( ) ;
234+ // Move focus back to body
235+ await sendKeys ( { down : 'Shift' } ) ;
236+ await sendKeys ( { press : 'Tab' } ) ;
237+ await sendKeys ( { up : 'Shift' } ) ;
238+ expect ( document . activeElement ) . to . equal ( siblingInput ) ;
239+ } ) ;
240+
241+ it ( 'should set focused attribute when clicking on non-focusable slotted element' , async ( ) => {
242+ const span = document . createElement ( 'span' ) ;
243+ span . textContent = 'test' ;
244+ customElement . appendChild ( span ) ;
245+
246+ await click ( span ) ;
247+ expect ( customElement . hasAttribute ( 'focused' ) ) . to . be . true ;
248+ } ) ;
249+
250+ it ( 'should remove focused attribute when moving to focusable slotted element' , async ( ) => {
251+ const input = document . createElement ( 'input' ) ;
252+ customElement . appendChild ( input ) ;
253+
254+ customElement . focus ( ) ;
255+ expect ( customElement . hasAttribute ( 'focused' ) ) . to . be . true ;
256+ input . focus ( ) ;
239257 expect ( customElement . hasAttribute ( 'focused' ) ) . to . be . false ;
240258 } ) ;
241259
0 commit comments