commandcollection.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import CommandCollection from '../src/commandcollection';
  6. import Command from '../src/command';
  7. import ModelTestEditor from './_utils/modeltesteditor';
  8. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  9. class SomeCommand extends Command {
  10. execute() {}
  11. }
  12. describe( 'CommandCollection', () => {
  13. let collection, editor;
  14. beforeEach( () => {
  15. collection = new CommandCollection();
  16. return ModelTestEditor
  17. .create()
  18. .then( newEditor => {
  19. editor = newEditor;
  20. } );
  21. } );
  22. afterEach( () => {
  23. collection.destroy();
  24. return editor.destroy();
  25. } );
  26. describe( 'add() and get()', () => {
  27. it( 'adds and retrieves a command', () => {
  28. const command = new SomeCommand( editor );
  29. collection.add( 'foo', command );
  30. expect( collection.get( 'foo' ) ).to.equal( command );
  31. } );
  32. } );
  33. describe( 'execute()', () => {
  34. it( 'executes given method with given attributes', () => {
  35. const command = new SomeCommand( editor );
  36. sinon.spy( command, 'execute' );
  37. collection.add( 'foo', command );
  38. collection.execute( 'foo', 1, 2 );
  39. expect( command.execute.calledOnce ).to.be.true;
  40. expect( command.execute.args[ 0 ] ).to.deep.equal( [ 1, 2 ] );
  41. } );
  42. it( 'returns the result of command\'s execute()', () => {
  43. const command = new SomeCommand( editor );
  44. const commandResult = { foo: 'bar' };
  45. sinon.stub( command, 'execute' ).returns( commandResult );
  46. collection.add( 'foo', command );
  47. const collectionResult = collection.execute( 'foo' );
  48. expect( collectionResult, 'collection.execute()' ).to.equal( commandResult );
  49. expect( collectionResult, 'collection.execute()' ).to.deep.equal( { foo: 'bar' } );
  50. } );
  51. it( 'throws an error if command does not exist', () => {
  52. const command = new SomeCommand( editor );
  53. collection.add( 'bar', command );
  54. expectToThrowCKEditorError( () => {
  55. collection.execute( 'foo' );
  56. }, /^commandcollection-command-not-found:/, editor );
  57. } );
  58. } );
  59. describe( 'names()', () => {
  60. it( 'returns iterator', () => {
  61. const names = collection.names();
  62. expect( names.next ).to.be.a( 'function' );
  63. } );
  64. it( 'returns iterator of command names', () => {
  65. collection.add( 'foo', new SomeCommand( editor ) );
  66. collection.add( 'bar', new SomeCommand( editor ) );
  67. expect( Array.from( collection.names() ) ).to.have.members( [ 'foo', 'bar' ] );
  68. } );
  69. } );
  70. describe( 'commands()', () => {
  71. it( 'returns iterator', () => {
  72. const commands = collection.commands();
  73. expect( commands.next ).to.be.a( 'function' );
  74. } );
  75. it( 'returns iterator of commands', () => {
  76. const c1 = new SomeCommand( editor );
  77. const c2 = new SomeCommand( editor );
  78. collection.add( 'foo', c1 );
  79. collection.add( 'bar', c2 );
  80. const commandArray = Array.from( collection.commands() );
  81. expect( commandArray ).to.have.length( 2 );
  82. expect( commandArray ).to.have.members( [ c1, c2 ] );
  83. } );
  84. } );
  85. describe( 'iterator', () => {
  86. it( 'exists', () => {
  87. expect( collection ).to.have.property( Symbol.iterator );
  88. } );
  89. it( 'returns iterator of [ name, command ]', () => {
  90. const c1 = new SomeCommand( editor );
  91. const c2 = new SomeCommand( editor );
  92. collection.add( 'foo', c1 );
  93. collection.add( 'bar', c2 );
  94. const collectionArray = Array.from( collection );
  95. expect( collectionArray ).to.have.length( 2 );
  96. expect( collectionArray.map( pair => pair[ 0 ] ) ).to.have.members( [ 'foo', 'bar' ] );
  97. expect( collectionArray.map( pair => pair[ 1 ] ) ).to.have.members( [ c1, c2 ] );
  98. } );
  99. } );
  100. describe( 'destroy()', () => {
  101. it( 'should destroy commands', () => {
  102. const c1 = new SomeCommand( editor );
  103. const c2 = new SomeCommand( editor );
  104. sinon.spy( c1, 'destroy' );
  105. sinon.spy( c2, 'destroy' );
  106. collection.add( 'foo', c1 );
  107. collection.add( 'bar', c2 );
  108. collection.destroy();
  109. expect( c1.destroy.calledOnce ).to.be.true;
  110. expect( c2.destroy.calledOnce ).to.be.true;
  111. } );
  112. } );
  113. } );