8
0
Pārlūkot izejas kodu

Tests: Add `DropdownPanelView#focus()` and `DropdownPanelView#focusLast()` tests.

Maciej Gołaszewski 7 gadi atpakaļ
vecāks
revīzija
f4e9bebde0

+ 7 - 3
packages/ckeditor5-ui/src/dropdown/dropdownpanelview.js

@@ -72,11 +72,15 @@ export default class DropdownPanelView extends View {
 		}
 	}
 
-	// TODO: child might not have focusLast
-	// TODO: maybe adding toolbar/list should somewhat intercept those?
 	focusLast() {
 		if ( this.children.length ) {
-			this.children.last.focusLast();
+			const lastChild = this.children.last;
+
+			if ( typeof lastChild.focusLast === 'function' ) {
+				lastChild.focusLast();
+			} else {
+				lastChild.focus();
+			}
 		}
 	}
 }

+ 53 - 0
packages/ckeditor5-ui/tests/dropdown/dropdownpanelview.js

@@ -1,3 +1,4 @@
+
 /**
  * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  * For licensing, see LICENSE.md.
@@ -7,6 +8,7 @@
 
 import ViewCollection from '../../src/viewcollection';
 import DropdownPanelView from '../../src/dropdown/dropdownpanelview';
+import View from '../../src/view';
 
 describe( 'DropdownPanelView', () => {
 	let view, locale;
@@ -63,4 +65,55 @@ describe( 'DropdownPanelView', () => {
 			} );
 		} );
 	} );
+
+	describe( 'focus()', () => {
+		it( 'does nothing for empty panel', () => {
+			expect( () => view.focus() ).to.not.throw();
+		} );
+
+		it( 'focuses first child view', () => {
+			const firstChildView = new View();
+
+			firstChildView.focus = sinon.spy();
+
+			view.children.add( firstChildView );
+			view.children.add( new View() );
+
+			view.focus();
+
+			sinon.assert.calledOnce( firstChildView.focus );
+		} );
+	} );
+
+	describe( 'focusLast()', () => {
+		it( 'does nothing for empty panel', () => {
+			expect( () => view.focusLast() ).to.not.throw();
+		} );
+
+		it( 'focuses last child view', () => {
+			const lastChildView = new View();
+
+			lastChildView.focusLast = sinon.spy();
+
+			view.children.add( new View() );
+			view.children.add( lastChildView );
+
+			view.focusLast();
+
+			sinon.assert.calledOnce( lastChildView.focusLast );
+		} );
+
+		it( 'focuses last child view even if it does not have focusLast() method', () => {
+			const lastChildView = new View();
+
+			lastChildView.focus = sinon.spy();
+
+			view.children.add( new View() );
+			view.children.add( lastChildView );
+
+			view.focusLast();
+
+			sinon.assert.calledOnce( lastChildView.focus );
+		} );
+	} );
 } );