commandcollection.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import CommandCollection from '../src/commandcollection';
  6. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  7. import Command from '../src/command';
  8. import ModelTestEditor from './_utils/modeltesteditor';
  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. expect( () => {
  44. collection.execute( 'foo' );
  45. } ).to.throw( CKEditorError, /^commandcollection-command-not-found:/ );
  46. } );
  47. } );
  48. describe( 'names()', () => {
  49. it( 'returns iterator', () => {
  50. const names = collection.names();
  51. expect( names.next ).to.be.a( 'function' );
  52. } );
  53. it( 'returns iterator of command names', () => {
  54. collection.add( 'foo', new SomeCommand( editor ) );
  55. collection.add( 'bar', new SomeCommand( editor ) );
  56. expect( Array.from( collection.names() ) ).to.have.members( [ 'foo', 'bar' ] );
  57. } );
  58. } );
  59. describe( 'commands()', () => {
  60. it( 'returns iterator', () => {
  61. const commands = collection.commands();
  62. expect( commands.next ).to.be.a( 'function' );
  63. } );
  64. it( 'returns iterator of commands', () => {
  65. const c1 = new SomeCommand( editor );
  66. const c2 = new SomeCommand( editor );
  67. collection.add( 'foo', c1 );
  68. collection.add( 'bar', c2 );
  69. const commandArray = Array.from( collection.commands() );
  70. expect( commandArray ).to.have.length( 2 );
  71. expect( commandArray ).to.have.members( [ c1, c2 ] );
  72. } );
  73. } );
  74. describe( 'iterator', () => {
  75. it( 'exists', () => {
  76. expect( collection ).to.have.property( Symbol.iterator );
  77. } );
  78. it( 'returns iterator of [ name, command ]', () => {
  79. const c1 = new SomeCommand( editor );
  80. const c2 = new SomeCommand( editor );
  81. collection.add( 'foo', c1 );
  82. collection.add( 'bar', c2 );
  83. const collectionArray = Array.from( collection );
  84. expect( collectionArray ).to.have.length( 2 );
  85. expect( collectionArray.map( pair => pair[ 0 ] ) ).to.have.members( [ 'foo', 'bar' ] );
  86. expect( collectionArray.map( pair => pair[ 1 ] ) ).to.have.members( [ c1, c2 ] );
  87. } );
  88. } );
  89. describe( 'commands()', () => {
  90. it( 'returns iterator of commands', () => {
  91. const c1 = new SomeCommand( editor );
  92. const c2 = new SomeCommand( editor );
  93. sinon.spy( c1, 'destroy' );
  94. sinon.spy( c2, 'destroy' );
  95. collection.add( 'foo', c1 );
  96. collection.add( 'bar', c2 );
  97. collection.destroy();
  98. expect( c1.destroy.calledOnce ).to.be.true;
  99. expect( c2.destroy.calledOnce ).to.be.true;
  100. } );
  101. } );
  102. } );