commandcollection.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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( 'throws an error if command does not exist', () => {
  43. const command = new SomeCommand( editor );
  44. collection.add( 'bar', command );
  45. expectToThrowCKEditorError( () => {
  46. collection.execute( 'foo' );
  47. }, /^commandcollection-command-not-found:/, editor );
  48. } );
  49. } );
  50. describe( 'names()', () => {
  51. it( 'returns iterator', () => {
  52. const names = collection.names();
  53. expect( names.next ).to.be.a( 'function' );
  54. } );
  55. it( 'returns iterator of command names', () => {
  56. collection.add( 'foo', new SomeCommand( editor ) );
  57. collection.add( 'bar', new SomeCommand( editor ) );
  58. expect( Array.from( collection.names() ) ).to.have.members( [ 'foo', 'bar' ] );
  59. } );
  60. } );
  61. describe( 'commands()', () => {
  62. it( 'returns iterator', () => {
  63. const commands = collection.commands();
  64. expect( commands.next ).to.be.a( 'function' );
  65. } );
  66. it( 'returns iterator of commands', () => {
  67. const c1 = new SomeCommand( editor );
  68. const c2 = new SomeCommand( editor );
  69. collection.add( 'foo', c1 );
  70. collection.add( 'bar', c2 );
  71. const commandArray = Array.from( collection.commands() );
  72. expect( commandArray ).to.have.length( 2 );
  73. expect( commandArray ).to.have.members( [ c1, c2 ] );
  74. } );
  75. } );
  76. describe( 'iterator', () => {
  77. it( 'exists', () => {
  78. expect( collection ).to.have.property( Symbol.iterator );
  79. } );
  80. it( 'returns iterator of [ name, command ]', () => {
  81. const c1 = new SomeCommand( editor );
  82. const c2 = new SomeCommand( editor );
  83. collection.add( 'foo', c1 );
  84. collection.add( 'bar', c2 );
  85. const collectionArray = Array.from( collection );
  86. expect( collectionArray ).to.have.length( 2 );
  87. expect( collectionArray.map( pair => pair[ 0 ] ) ).to.have.members( [ 'foo', 'bar' ] );
  88. expect( collectionArray.map( pair => pair[ 1 ] ) ).to.have.members( [ c1, c2 ] );
  89. } );
  90. } );
  91. describe( 'commands()', () => {
  92. it( 'returns iterator of commands', () => {
  93. const c1 = new SomeCommand( editor );
  94. const c2 = new SomeCommand( editor );
  95. sinon.spy( c1, 'destroy' );
  96. sinon.spy( c2, 'destroy' );
  97. collection.add( 'foo', c1 );
  98. collection.add( 'bar', c2 );
  99. collection.destroy();
  100. expect( c1.destroy.calledOnce ).to.be.true;
  101. expect( c2.destroy.calledOnce ).to.be.true;
  102. } );
  103. } );
  104. } );