8
0
Просмотр исходного кода

Tests: Initial tests for createButtonDropdown.

Maciej Gołaszewski 8 лет назад
Родитель
Сommit
d01a280d2e

+ 3 - 6
packages/ckeditor5-ui/src/dropdown/button/createbuttondropdown.js

@@ -70,12 +70,9 @@ export default function createButtonDropdown( model, buttonViews, locale ) {
 
 	const dropdownView = createDropdown( model, locale );
 
-	const buttonGroupView = dropdownView.buttonGroupView = new ButtonGroupView(
-		buttonViews,
-		{
-			isVertical: model.isVertical
-		}
-	);
+	const buttonGroupView = dropdownView.buttonGroupView = new ButtonGroupView( { isVertical: model.isVertical } );
+
+	buttonViews.map( view => buttonGroupView.items.add( view ) );
 
 	dropdownView.buttonView.extendTemplate( {
 		attributes: {

+ 175 - 0
packages/ckeditor5-ui/tests/dropdown/button/createbuttondropdown.js

@@ -0,0 +1,175 @@
+/**
+ * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md.
+ */
+
+/* globals document, Event */
+
+import Model from '../../../src/model';
+import createButtonDropdown from '../../../src/dropdown/button/createbuttondropdown';
+
+import ButtonView from '../../../src/button/buttonview';
+import ButtonGroupView from '../../../src/buttongroup/buttongroupview';
+
+import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
+
+describe( 'createButtonDropdown', () => {
+	let view, model, locale, buttonViews;
+
+	beforeEach( () => {
+		locale = { t() {} };
+		buttonViews = [ 'foo', 'bar' ].map( icon => {
+			const button = new ButtonView();
+			button.icon = icon;
+
+			return button;
+		} );
+
+		model = new Model( {
+			label: 'foo'
+		} );
+
+		view = createButtonDropdown( model, buttonViews, locale );
+		view.render();
+		document.body.appendChild( view.element );
+	} );
+
+	afterEach( () => {
+		view.element.remove();
+	} );
+
+	describe( 'constructor()', () => {
+		it( 'sets view#locale', () => {
+			expect( view.locale ).to.equal( locale );
+		} );
+
+		describe( 'view#buttonGroupView', () => {
+			it( 'is created', () => {
+				const panelChildren = view.panelView.children;
+
+				expect( panelChildren ).to.have.length( 1 );
+				expect( panelChildren.get( 0 ) ).to.equal( view.buttonGroupView );
+				expect( view.buttonGroupView ).to.be.instanceof( ButtonGroupView );
+			} );
+
+			it( 'delegates view.buttonGroupView#execute to the view', done => {
+				view.on( 'execute', evt => {
+					expect( evt.source ).to.equal( view.buttonGroupView.items.get( 0 ) );
+					expect( evt.path ).to.deep.equal( [ view.buttonGroupView.items.get( 0 ), view ] );
+
+					done();
+				} );
+
+				view.buttonGroupView.items.get( 0 ).fire( 'execute' );
+			} );
+		} );
+
+		it( 'changes view#isOpen on view#execute', () => {
+			view.isOpen = true;
+
+			view.fire( 'execute' );
+			expect( view.isOpen ).to.be.false;
+
+			view.fire( 'execute' );
+			expect( view.isOpen ).to.be.false;
+		} );
+
+		it( 'listens to view#isOpen and reacts to DOM events (valid target)', () => {
+			// Open the dropdown.
+			view.isOpen = true;
+
+			// Fire event from outside of the dropdown.
+			document.body.dispatchEvent( new Event( 'click', {
+				bubbles: true
+			} ) );
+
+			// Closed the dropdown.
+			expect( view.isOpen ).to.be.false;
+
+			// Fire event from outside of the dropdown.
+			document.body.dispatchEvent( new Event( 'click', {
+				bubbles: true
+			} ) );
+
+			// Dropdown is still closed.
+			expect( view.isOpen ).to.be.false;
+		} );
+
+		it( 'listens to view#isOpen and reacts to DOM events (invalid target)', () => {
+			// Open the dropdown.
+			view.isOpen = true;
+
+			// Event from view.element should be discarded.
+			view.element.dispatchEvent( new Event( 'click', {
+				bubbles: true
+			} ) );
+
+			// Dropdown is still open.
+			expect( view.isOpen ).to.be.true;
+
+			// Event from within view.element should be discarded.
+			const child = document.createElement( 'div' );
+			view.element.appendChild( child );
+
+			child.dispatchEvent( new Event( 'click', {
+				bubbles: true
+			} ) );
+
+			// Dropdown is still open.
+			expect( view.isOpen ).to.be.true;
+		} );
+
+		describe( 'activates keyboard navigation for the dropdown', () => {
+			it( 'so "arrowdown" focuses the #buttonGroupView if dropdown is open', () => {
+				const keyEvtData = {
+					keyCode: keyCodes.arrowdown,
+					preventDefault: sinon.spy(),
+					stopPropagation: sinon.spy()
+				};
+				const spy = sinon.spy( view.buttonGroupView, 'focus' );
+
+				view.isOpen = false;
+				view.keystrokes.press( keyEvtData );
+				sinon.assert.notCalled( spy );
+
+				view.isOpen = true;
+				view.keystrokes.press( keyEvtData );
+				sinon.assert.calledOnce( spy );
+			} );
+
+			it( 'so "arrowup" focuses the last #item in #buttonGroupView if dropdown is open', () => {
+				const keyEvtData = {
+					keyCode: keyCodes.arrowup,
+					preventDefault: sinon.spy(),
+					stopPropagation: sinon.spy()
+				};
+				const spy = sinon.spy( view.buttonGroupView, 'focusLast' );
+
+				view.isOpen = false;
+				view.keystrokes.press( keyEvtData );
+				sinon.assert.notCalled( spy );
+
+				view.isOpen = true;
+				view.keystrokes.press( keyEvtData );
+				sinon.assert.calledOnce( spy );
+			} );
+		} );
+
+		describe( 'icon', () => {
+			it( 'should be set to first button', () => {
+				expect( view.buttonView.icon ).to.equal( view.buttonGroupView.items.get( 0 ).icon );
+			} );
+
+			it( 'should be bound to first button that is on', () => {
+				view.buttonGroupView.items.get( 1 ).isOn = true;
+
+				expect( view.buttonView.icon ).to.equal( view.buttonGroupView.items.get( 1 ).icon );
+
+				view.buttonGroupView.items.get( 0 ).isOn = true;
+				view.buttonGroupView.items.get( 1 ).isOn = false;
+
+				expect( view.buttonView.icon ).to.equal( view.buttonGroupView.items.get( 0 ).icon );
+			} );
+		} );
+	} );
+} );