|
|
@@ -1,162 +0,0 @@
|
|
|
-/**
|
|
|
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
|
|
|
- * For licensing, see LICENSE.md.
|
|
|
- */
|
|
|
-
|
|
|
-import Editor from '../../src/editor/editor';
|
|
|
-import Command from '../../src/command/command';
|
|
|
-
|
|
|
-describe( 'Command', () => {
|
|
|
- let editor, command;
|
|
|
-
|
|
|
- class CommandWithSchema extends Command {
|
|
|
- constructor( editor, schemaValid ) {
|
|
|
- super( editor );
|
|
|
-
|
|
|
- this.schemaValid = schemaValid;
|
|
|
- }
|
|
|
-
|
|
|
- _checkEnabled() {
|
|
|
- return this.schemaValid;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- beforeEach( () => {
|
|
|
- editor = new Editor();
|
|
|
- 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 );
|
|
|
- expect( command.isEnabled ).to.be.true;
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'Command should have _doExecute method', () => {
|
|
|
- expect( () => {
|
|
|
- command._doExecute();
|
|
|
- } ).not.to.throw;
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should add listener to its refreshState event if checkSchema method is present', () => {
|
|
|
- expect( command._checkEnabled ).to.be.undefined;
|
|
|
-
|
|
|
- command._checkEnabled = sinon.spy();
|
|
|
- command.refreshState();
|
|
|
-
|
|
|
- expect( command._checkEnabled.called ).to.be.false;
|
|
|
-
|
|
|
- const newCommand = new CommandWithSchema( editor, true );
|
|
|
- sinon.spy( newCommand, '_checkEnabled' );
|
|
|
-
|
|
|
- newCommand.refreshState();
|
|
|
-
|
|
|
- expect( newCommand._checkEnabled.calledOnce ).to.be.true;
|
|
|
- } );
|
|
|
- } );
|
|
|
-
|
|
|
- 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', () => {
|
|
|
- const spy = sinon.spy();
|
|
|
-
|
|
|
- command.on( 'refreshState', spy );
|
|
|
- command.refreshState();
|
|
|
-
|
|
|
- expect( spy.called ).to.be.true;
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should set isEnabled property to the value passed by object-reference', () => {
|
|
|
- command.on( 'refreshState', ( evt, data ) => {
|
|
|
- data.isEnabled = true;
|
|
|
- } );
|
|
|
-
|
|
|
- expect( command.isEnabled ).to.be.true;
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should set isEnabled to false if _checkEnabled returns false', () => {
|
|
|
- const disabledCommand = new CommandWithSchema( editor, false );
|
|
|
-
|
|
|
- disabledCommand.refreshState();
|
|
|
-
|
|
|
- expect( disabledCommand.isEnabled ).to.be.false;
|
|
|
- } );
|
|
|
- } );
|
|
|
-
|
|
|
- describe( 'disable', () => {
|
|
|
- it( 'should make command disabled', () => {
|
|
|
- command._disable();
|
|
|
-
|
|
|
- expect( command.isEnabled ).to.be.false;
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should not make command disabled if there is a high-priority listener forcing command to be enabled', () => {
|
|
|
- command.on( 'refreshState', evt => {
|
|
|
- evt.stop();
|
|
|
-
|
|
|
- return true;
|
|
|
- }, command, 1 );
|
|
|
-
|
|
|
- command._disable();
|
|
|
-
|
|
|
- expect( command.isEnabled ).to.be.true;
|
|
|
- } );
|
|
|
- } );
|
|
|
-
|
|
|
- describe( 'enable', () => {
|
|
|
- it( 'should make command enabled if it was previously disabled by disable()', () => {
|
|
|
- command._disable();
|
|
|
- command._enable();
|
|
|
-
|
|
|
- expect( command.isEnabled ).to.be.true;
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should not make command enabled if there are other listeners disabling it', () => {
|
|
|
- command._disable();
|
|
|
-
|
|
|
- command.on( 'refreshState', ( evt, data ) => {
|
|
|
- data.isEnabled = false;
|
|
|
- } );
|
|
|
-
|
|
|
- command.refreshState();
|
|
|
- command._enable();
|
|
|
-
|
|
|
- expect( command.isEnabled ).to.be.false;
|
|
|
- } );
|
|
|
- } );
|
|
|
-
|
|
|
- describe( '_execute', () => {
|
|
|
- it( 'should not execute command if it is disabled', () => {
|
|
|
- command._disable();
|
|
|
-
|
|
|
- sinon.spy( command, '_doExecute' );
|
|
|
-
|
|
|
- command._execute();
|
|
|
-
|
|
|
- expect( command._doExecute.called ).to.be.false;
|
|
|
- } );
|
|
|
-
|
|
|
- it( 'should execute command if it is enabled', () => {
|
|
|
- sinon.spy( command, '_doExecute' );
|
|
|
-
|
|
|
- command._execute();
|
|
|
-
|
|
|
- expect( command._doExecute.called ).to.be.true;
|
|
|
- } );
|
|
|
- } );
|
|
|
-} );
|