Browse Source

Add restricted document ui first tests.

Maciej Gołaszewski 6 years ago
parent
commit
1e883ac31b

+ 43 - 0
packages/ckeditor5-restricted-editing/src/restricteddocumentui.js

@@ -0,0 +1,43 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module core/restricteddocumentui
+ */
+
+import Plugin from './plugin';
+import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
+import boldIcon from '@ckeditor/ckeditor5-basic-styles/theme/icons/bold.svg';
+
+/**
+ * @extends module:core/plugin~Plugin
+ */
+export default class RestrictedDocumentUI extends Plugin {
+	/**
+	 * @inheritDoc
+	 */
+	init() {
+		const editor = this.editor;
+		const t = editor.t;
+
+		editor.ui.componentFactory.add( 'nonRestricted', locale => {
+			const command = editor.commands.get( 'nonRestricted' );
+			const view = new ButtonView( locale );
+
+			view.set( {
+				label: t( 'Restricted editing' ),
+				icon: boldIcon,
+				tooltip: true,
+				isToggleable: true
+			} );
+
+			view.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );
+
+			this.listenTo( view, 'execute', () => editor.execute( 'nonRestricted' ) );
+
+			return view;
+		} );
+	}
+}

+ 68 - 0
packages/ckeditor5-restricted-editing/tests/restricteddocumentui.js

@@ -0,0 +1,68 @@
+/**
+ * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/* globals document */
+
+import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
+import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
+import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
+import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+
+import RestrictedDocumentUI from '../src/restricteddocumentui';
+import RestrictedDocument from '../src/restricteddocument';
+
+describe( 'RestrictedDocumentUI', () => {
+	let editor, buttonView;
+
+	testUtils.createSinonSandbox();
+
+	beforeEach( () => {
+		const editorElement = document.createElement( 'div' );
+		document.body.appendChild( editorElement );
+
+		return ClassicTestEditor
+			.create( editorElement, {
+				plugins: [ Paragraph, RestrictedDocument, RestrictedDocumentUI ]
+			} )
+			.then( newEditor => {
+				editor = newEditor;
+
+				buttonView = editor.ui.componentFactory.create( 'nonRestricted' );
+			} );
+	} );
+
+	afterEach( () => {
+		return editor.destroy();
+	} );
+
+	it( 'should register button', () => {
+		expect( buttonView ).to.be.instanceOf( ButtonView );
+		expect( buttonView.isOn ).to.be.false;
+		expect( buttonView.label ).to.equal( 'Restricted editing' );
+		expect( buttonView.icon ).to.match( /<svg / );
+		expect( buttonView.isToggleable ).to.be.true;
+	} );
+
+	it( 'should execute command on model execute event', () => {
+		const executeSpy = testUtils.sinon.spy( editor, 'execute' );
+
+		buttonView.fire( 'execute' );
+
+		sinon.assert.calledOnce( executeSpy );
+	} );
+
+	it( 'should bind model to command', () => {
+		const command = editor.commands.get( 'nonRestricted' );
+
+		expect( buttonView.isOn ).to.be.false;
+		expect( buttonView.isEnabled ).to.be.true;
+
+		command.value = true;
+		expect( buttonView.isOn ).to.be.true;
+
+		command.isEnabled = false;
+		expect( buttonView.isEnabled ).to.be.false;
+	} );
+} );