@@ -8,6 +8,10 @@ import View from '../src/view';
88import FocusCycler from '../src/focuscycler' ;
99import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler' ;
1010import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard' ;
11+ import global from '@ckeditor/ckeditor5-utils/src/dom/global' ;
12+ import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils' ;
13+
14+ testUtils . createSinonSandbox ( ) ;
1115
1216describe ( 'FocusCycler' , ( ) => {
1317 let focusables , focusTracker , cycler ;
@@ -22,6 +26,8 @@ describe( 'FocusCycler', () => {
2226 focusTracker : focusTracker
2327 } ) ;
2428
29+ testUtils . sinon . stub ( global . window , 'getComputedStyle' ) ;
30+
2531 focusables . add ( nonFocusable ( ) ) ;
2632 focusables . add ( focusable ( ) ) ;
2733 focusables . add ( focusable ( ) ) ;
@@ -196,11 +202,9 @@ describe( 'FocusCycler', () => {
196202
197203 describe ( 'focusFirst()' , ( ) => {
198204 it ( 'focuses first focusable view' , ( ) => {
199- const spy = sinon . spy ( focusables . get ( 1 ) , 'focus' ) ;
200-
201205 cycler . focusFirst ( ) ;
202206
203- sinon . assert . calledOnce ( spy ) ;
207+ sinon . assert . calledOnce ( focusables . get ( 1 ) . focus ) ;
204208 } ) ;
205209
206210 it ( 'does not throw when no focusable items' , ( ) => {
@@ -223,15 +227,27 @@ describe( 'FocusCycler', () => {
223227 cycler . focusFirst ( ) ;
224228 } ) . to . not . throw ( ) ;
225229 } ) ;
230+
231+ it ( 'ignores invisible items' , ( ) => {
232+ const item = focusable ( ) ;
233+
234+ focusables = new ViewCollection ( ) ;
235+ focusables . add ( nonFocusable ( ) ) ;
236+ focusables . add ( focusable ( true ) ) ;
237+ focusables . add ( item ) ;
238+
239+ cycler = new FocusCycler ( { focusables, focusTracker } ) ;
240+
241+ cycler . focusFirst ( ) ;
242+ sinon . assert . calledOnce ( item . focus ) ;
243+ } ) ;
226244 } ) ;
227245
228246 describe ( 'focusLast()' , ( ) => {
229247 it ( 'focuses last focusable view' , ( ) => {
230- const spy = sinon . spy ( focusables . get ( 3 ) , 'focus' ) ;
231-
232248 cycler . focusLast ( ) ;
233249
234- sinon . assert . calledOnce ( spy ) ;
250+ sinon . assert . calledOnce ( focusables . get ( 3 ) . focus ) ;
235251 } ) ;
236252
237253 it ( 'does not throw when no focusable items' , ( ) => {
@@ -258,12 +274,10 @@ describe( 'FocusCycler', () => {
258274
259275 describe ( 'focusNext()' , ( ) => {
260276 it ( 'focuses next focusable view' , ( ) => {
261- const spy = sinon . spy ( focusables . get ( 3 ) , 'focus' ) ;
262-
263277 focusTracker . focusedElement = focusables . get ( 2 ) . element ;
264278 cycler . focusNext ( ) ;
265279
266- sinon . assert . calledOnce ( spy ) ;
280+ sinon . assert . calledOnce ( focusables . get ( 3 ) . focus ) ;
267281 } ) ;
268282
269283 it ( 'does not throw when no focusable items' , ( ) => {
@@ -290,12 +304,10 @@ describe( 'FocusCycler', () => {
290304
291305 describe ( 'focusPrevious()' , ( ) => {
292306 it ( 'focuses previous focusable view' , ( ) => {
293- const spy = sinon . spy ( focusables . get ( 3 ) , 'focus' ) ;
294-
295307 focusTracker . focusedElement = focusables . get ( 1 ) . element ;
296308 cycler . focusPrevious ( ) ;
297309
298- sinon . assert . calledOnce ( spy ) ;
310+ sinon . assert . calledOnce ( focusables . get ( 3 ) . focus ) ;
299311 } ) ;
300312
301313 it ( 'does not throw when no focusable items' , ( ) => {
@@ -385,15 +397,23 @@ describe( 'FocusCycler', () => {
385397 } ) ;
386398} ) ;
387399
388- function focusable ( ) {
400+ function nonFocusable ( isHidden ) {
389401 const view = new View ( ) ;
402+ view . element = Math . random ( ) ;
390403
391- view . focus = ( ) => { } ;
392- view . element = { } ;
404+ global . window . getComputedStyle
405+ . withArgs ( view . element )
406+ . returns ( {
407+ display : isHidden ? 'none' : 'block'
408+ } ) ;
393409
394410 return view ;
395411}
396412
397- function nonFocusable ( ) {
398- return new View ( ) ;
413+ function focusable ( isHidden ) {
414+ const view = nonFocusable ( isHidden ) ;
415+
416+ view . focus = sinon . spy ( ) ;
417+
418+ return view ;
399419}
0 commit comments