8
0
Quellcode durchsuchen

Refactoring and tests in the RestrictedEditingUI.

Aleksander Nowodzinski vor 6 Jahren
Ursprung
Commit
45ae737396

+ 32 - 17
packages/ckeditor5-restricted-editing/src/restrictededitingui.js

@@ -35,23 +35,8 @@ export default class RestrictedEditingUI extends Plugin {
 			const dropdownView = createDropdown( locale );
 			const listItems = new Collection();
 
-			listItems.add( {
-				type: 'button',
-				model: new Model( {
-					commandName: 'TODO',
-					label: t( 'Previous editable region' ),
-					withText: true
-				} )
-			} );
-
-			listItems.add( {
-				type: 'button',
-				model: new Model( {
-					commandName: 'TODO',
-					label: t( 'Next editable region' ),
-					withText: true
-				} )
-			} );
+			listItems.add( this._getButtonDefinition( 'goToPreviousRestrictedEditingRegion', t( 'Previous editable region' ) ) );
+			listItems.add( this._getButtonDefinition( 'goToNextRestrictedEditingRegion', t( 'Next editable region' ) ) );
 
 			addListToDropdown( dropdownView, listItems );
 
@@ -63,7 +48,37 @@ export default class RestrictedEditingUI extends Plugin {
 				isOn: false
 			} );
 
+			this.listenTo( dropdownView, 'execute', evt => {
+				editor.execute( evt.source._commandName );
+				editor.editing.view.focus();
+			} );
+
 			return dropdownView;
 		} );
 	}
+
+	/**
+	 * Returns a definition of the navigation button to be used in the dropdown.
+	 *
+	 * @private
+	 * @param {String} commandName Name of the command the button represents.
+	 * @param {String} label Translated label of the button.
+	 * @returns {module:ui/dropdown/utils~ListDropdownItemDefinition}
+	 */
+	_getButtonDefinition( commandName, label ) {
+		const editor = this.editor;
+		const command = editor.commands.get( commandName );
+		const definition = {
+			type: 'button',
+			model: new Model( {
+				label,
+				withText: true,
+				_commandName: commandName
+			} )
+		};
+
+		definition.model.bind( 'isEnabled' ).to( command, 'isEnabled' );
+
+		return definition;
+	}
 }

+ 130 - 0
packages/ckeditor5-restricted-editing/tests/restrictededitingui.js

@@ -0,0 +1,130 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/* global document */
+
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
+
+import RestrictedEditingEditing from './../src/restrictededitingediting';
+import RestrictedEditingUI from './../src/restrictededitingui';
+import lockIcon from '../theme/icons/contentlock.svg';
+
+describe( 'RestrictedEditingUI', () => {
+	let editor, element, goToPreviousCommand, goToNextCommand;
+
+	testUtils.createSinonSandbox();
+
+	beforeEach( () => {
+		element = document.createElement( 'div' );
+		document.body.appendChild( element );
+
+		return ClassicTestEditor
+			.create( element, {
+				plugins: [ RestrictedEditingEditing, RestrictedEditingUI ]
+			} )
+			.then( newEditor => {
+				editor = newEditor;
+
+				goToPreviousCommand = editor.commands.get( 'goToPreviousRestrictedEditingRegion' );
+				goToNextCommand = editor.commands.get( 'goToNextRestrictedEditingRegion' );
+			} );
+	} );
+
+	afterEach( () => {
+		element.remove();
+
+		return editor.destroy();
+	} );
+
+	describe( 'plugin', () => {
+		it( 'should be named', () => {
+			expect( RestrictedEditingUI.pluginName ).to.equal( 'RestrictedEditingUI' );
+		} );
+
+		it( 'should be loaded', () => {
+			expect( editor.plugins.get( RestrictedEditingUI ) ).to.be.instanceOf( RestrictedEditingUI );
+		} );
+	} );
+
+	describe( 'restricted editing dropdown', () => {
+		let dropdown;
+
+		beforeEach( () => {
+			dropdown = editor.ui.componentFactory.create( 'restrictedEditing' );
+		} );
+
+		it( 'the button should have basic properties', () => {
+			const button = dropdown.buttonView;
+
+			expect( button ).to.have.property( 'label', 'Browse editable regions' );
+			expect( button ).to.have.property( 'tooltip', true );
+			expect( button ).to.have.property( 'icon', lockIcon );
+			expect( button ).to.have.property( 'isEnabled', true );
+			expect( button ).to.have.property( 'isOn', false );
+		} );
+
+		describe( 'exceptions navigation buttons', () => {
+			it( 'should have one that goes backward', () => {
+				const list = dropdown.listView;
+				const button = list.items.first.children.first;
+
+				expect( button.isOn ).to.be.false;
+				expect( button.withText ).to.be.true;
+				expect( button.label ).to.equal( 'Previous editable region' );
+			} );
+
+			it( 'should have one that goes forward', () => {
+				const list = dropdown.listView;
+				const button = list.items.last.children.first;
+
+				expect( button.isOn ).to.be.false;
+				expect( button.withText ).to.be.true;
+				expect( button.label ).to.equal( 'Next editable region' );
+			} );
+
+			it( 'should focus the view after executing the command', () => {
+				const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
+				const list = dropdown.listView;
+				const goToPreviousButton = list.items.first.children.first;
+
+				goToPreviousButton.fire( 'execute' );
+				sinon.assert.calledOnce( focusSpy );
+			} );
+
+			it( 'be enabled just like their corresponding commands', () => {
+				const listView = dropdown.listView;
+
+				goToPreviousCommand.isEnabled = false;
+				goToNextCommand.isEnabled = false;
+
+				expect( listView.items.map( item => item.children.first.isEnabled ) ).to.deep.equal( [ false, false ] );
+
+				goToPreviousCommand.isEnabled = true;
+				expect( listView.items.map( item => item.children.first.isEnabled ) ).to.deep.equal( [ true, false ] );
+
+				goToNextCommand.isEnabled = true;
+				expect( listView.items.map( item => item.children.first.isEnabled ) ).to.deep.equal( [ true, true ] );
+			} );
+
+			it( 'should execute their corresponding commands', () => {
+				const list = dropdown.listView;
+				const goToPreviousButton = list.items.first.children.first;
+				const goToNextButton = list.items.last.children.first;
+
+				goToPreviousCommand.isEnabled = true;
+				goToNextCommand.isEnabled = true;
+
+				const spy = sinon.spy( editor, 'execute' );
+
+				goToPreviousButton.fire( 'execute' );
+				sinon.assert.calledWith( spy.firstCall, 'goToPreviousRestrictedEditingRegion' );
+
+				goToNextButton.fire( 'execute' );
+				sinon.assert.calledWith( spy.secondCall, 'goToNextRestrictedEditingRegion' );
+			} );
+		} );
+	} );
+} );