| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- import Command from '../src/command';
- import ModelTestEditor from './_utils/modeltesteditor';
- describe( 'Command', () => {
- let editor, command;
- beforeEach( () => {
- return ModelTestEditor
- .create()
- .then( newEditor => {
- editor = newEditor;
- command = new Command( editor );
- } );
- } );
- afterEach( () => {
- command.destroy();
- return editor.destroy();
- } );
- describe( 'constructor()', () => {
- it( 'sets the editor property', () => {
- expect( command.editor ).to.equal( editor );
- } );
- it( 'sets the state properties', () => {
- expect( command.value ).to.be.undefined;
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'adds a listener which refreshes the command on editor.model.Document#event:change', () => {
- sinon.spy( command, 'refresh' );
- editor.model.document.fire( 'change' );
- expect( command.refresh.calledOnce ).to.be.true;
- } );
- } );
- describe( 'value', () => {
- it( 'fires change event', () => {
- const spy = sinon.spy();
- command.on( 'change:value', spy );
- command.value = 1;
- expect( spy.calledOnce ).to.be.true;
- } );
- } );
- describe( 'isEnabled', () => {
- it( 'fires change event', () => {
- const spy = sinon.spy();
- command.on( 'change:isEnabled', spy );
- command.isEnabled = true;
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'is always falsy when the editor is in read-only mode', () => {
- editor.isReadOnly = false;
- command.isEnabled = true;
- editor.isReadOnly = true;
- // Is false.
- expect( command.isEnabled ).to.false;
- command.refresh();
- // Still false.
- expect( command.isEnabled ).to.false;
- editor.isReadOnly = false;
- // And is back to true.
- expect( command.isEnabled ).to.true;
- } );
- it( 'is observable when is overridden', () => {
- editor.isReadOnly = false;
- command.isEnabled = true;
- editor.bind( 'something' ).to( command, 'isEnabled' );
- expect( editor.something ).to.true;
- editor.isReadOnly = true;
- expect( editor.something ).to.false;
- } );
- it( 'stops `set` event to force disabled and not affect `change` event', () => {
- const setSpy = sinon.spy();
- const changeSpy = sinon.spy();
- command.isEnabled = true;
- editor.isReadOnly = false;
- command.on( 'set', setSpy );
- command.on( 'change', changeSpy );
- editor.isReadOnly = true;
- sinon.assert.notCalled( setSpy );
- sinon.assert.calledOnce( changeSpy );
- } );
- } );
- describe( 'execute()', () => {
- it( 'is decorated', () => {
- const spy = sinon.spy();
- command.on( 'execute', spy );
- command.isEnabled = true;
- command.execute( 1, 2 );
- expect( spy.calledOnce ).to.be.true;
- expect( spy.args[ 0 ][ 1 ] ).to.deep.equal( [ 1, 2 ] );
- } );
- it( 'is automatically blocked (with low priority listener) if command is disabled', () => {
- const spyExecute = sinon.spy();
- const spyHighest = sinon.spy();
- const spyHigh = sinon.spy();
- class SpyCommand extends Command {
- execute() {
- spyExecute();
- }
- }
- const command = new SpyCommand( editor );
- command.on( 'execute', spyHighest, { priority: 'highest' } );
- command.on( 'execute', spyHigh, { priority: 'high' } );
- command.execute();
- expect( spyExecute.called ).to.be.false;
- expect( spyHighest.calledOnce ).to.be.true;
- expect( spyHigh.called ).to.be.false;
- } );
- } );
- describe( 'refresh()', () => {
- it( 'sets isEnabled to true', () => {
- command.refresh();
- expect( command.isEnabled ).to.be.true;
- } );
- // This is an acceptance test for the ability to override a command's state from outside
- // in a way that at any moment the action can be reverted by just offing the listener and
- // refreshing the command once again.
- it( 'is safely overridable using change:isEnabled', () => {
- command.on( 'change:isEnabled', callback, { priority: 'high' } );
- command.isEnabled = false;
- command.refresh();
- expect( command.isEnabled ).to.be.false;
- command.off( 'change:isEnabled', callback );
- command.refresh();
- expect( command.isEnabled ).to.be.true;
- function callback( evt ) {
- command.isEnabled = false;
- evt.stop();
- }
- } );
- } );
- describe( 'forceDisabled() / clearForceDisabled()', () => {
- it( 'forceDisabled() should disable the command', () => {
- command.forceDisabled( 'foo' );
- command.isEnabled = true;
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'clearForceDisabled() should enable the command', () => {
- command.forceDisabled( 'foo' );
- command.clearForceDisabled( 'foo' );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'clearForceDisabled() used with wrong identifier should not enable the command', () => {
- command.forceDisabled( 'foo' );
- command.clearForceDisabled( 'bar' );
- command.isEnabled = true;
- expect( command.isEnabled ).to.be.false;
- } );
- it( 'using forceDisabled() twice with the same identifier should not have any effect', () => {
- command.forceDisabled( 'foo' );
- command.forceDisabled( 'foo' );
- command.clearForceDisabled( 'foo' );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'command is enabled only after all disables were cleared', () => {
- command.forceDisabled( 'foo' );
- command.forceDisabled( 'bar' );
- command.clearForceDisabled( 'foo' );
- command.isEnabled = true;
- expect( command.isEnabled ).to.be.false;
- command.clearForceDisabled( 'bar' );
- expect( command.isEnabled ).to.be.true;
- } );
- it( 'command should remain disabled if isEnabled has a callback disabling it', () => {
- command.on( 'set:isEnabled', evt => {
- evt.return = false;
- evt.stop();
- } );
- command.forceDisabled( 'foo' );
- command.clearForceDisabled( 'foo' );
- command.isEnabled = true;
- expect( command.isEnabled ).to.be.false;
- } );
- } );
- } );
|