瀏覽代碼

Added Command#destroy + tests.

Szymon Cofalik 9 年之前
父節點
當前提交
8580e3038b
共有 3 個文件被更改,包括 33 次插入9 次删除
  1. 13 9
      src/command/command.js
  2. 4 0
      tests/command/attributecommand.js
  3. 16 0
      tests/command/command.js

+ 13 - 9
src/command/command.js

@@ -52,15 +52,9 @@ export default class Command {
 		}
 	}
 
-	/**
-	 * Checks if a command should be enabled according to its own rules. Mostly it will check schema to see if the command
-	 * is allowed to be executed in given position. This method can be defined in child class (but is not obligatory).
-	 * If it is defined, it will be added as a callback to `refreshState` event.
-	 *
-	 * @protected
-	 * @method ckeditor5.command.Command#_checkEnabled
-	 * @returns {Boolean} `true` if command should be enabled according to {@link core.treeModel.Document#schema}. `false` otherwise.
-	 */
+	destroy() {
+		this.stopListening();
+	}
 
 	/**
 	 * Fires `refreshState` event and checks it's resolve value to decide whether command should be enabled or not.
@@ -118,6 +112,16 @@ export default class Command {
 	 * @protected
 	 */
 	_doExecute() {}
+
+	/**
+	 * Checks if a command should be enabled according to its own rules. Mostly it will check schema to see if the command
+	 * is allowed to be executed in given position. This method can be defined in child class (but is not obligatory).
+	 * If it is defined, it will be added as a callback to `refreshState` event.
+	 *
+	 * @protected
+	 * @method ckeditor5.command.Command#_checkEnabled
+	 * @returns {Boolean} `true` if command should be enabled according to {@link core.treeModel.Document#schema}. `false` otherwise.
+	 */
 }
 
 function disableCallback( evt, data ) {

+ 4 - 0
tests/command/attributecommand.js

@@ -41,6 +41,10 @@ beforeEach( () => {
 	modelDoc.schema.disallow( { name: 'img', attribute: 'bold', inside: 'div' } );
 } );
 
+afterEach( () => {
+	command.destroy();
+} );
+
 describe( 'value', () => {
 	it( 'should be set to true or false basing on selection attribute', () => {
 		modelDoc.selection.setAttribute( attrKey, true );

+ 16 - 0
tests/command/command.js

@@ -30,6 +30,12 @@ beforeEach( () => {
 	command = new Command( editor );
 } );
 
+afterEach( () => {
+	// Might be redundant if editor destroys the commands.
+	command.destroy();
+	editor.destroy();
+} );
+
 describe( 'constructor', () => {
 	it( 'should create a new command instance, that is enabled and bound to given editor', () => {
 		expect( command ).to.have.property( 'editor' ).equal( editor );
@@ -59,6 +65,16 @@ describe( 'constructor', () => {
 	} );
 } );
 
+describe( 'destroy', () => {
+	it( 'should stop listening', () => {
+		sinon.spy( command, 'stopListening' );
+
+		command.destroy();
+
+		expect( command.stopListening.calledOnce ).to.be.true;
+	} );
+} );
+
 describe( 'refreshState', () => {
 	it( 'should fire refreshState event', () => {
 		let spy = sinon.spy();