8
0

editablecollection.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Editor from '/ckeditor5/editor.js';
  7. import Editable from '/ckeditor5/editable.js';
  8. import EditableCollection from '/ckeditor5/editablecollection.js';
  9. describe( 'EditableCollection', () => {
  10. let collection, editor;
  11. beforeEach( () => {
  12. collection = new EditableCollection();
  13. editor = new Editor();
  14. } );
  15. describe( 'constructor', () => {
  16. it( 'configures collection to use idProperty=name', () => {
  17. collection.add( new Editable( editor, 'foo' ) );
  18. expect( collection.get( 'foo' ).name ).to.equal( 'foo' );
  19. } );
  20. it( 'sets observable property current', () => {
  21. expect( collection ).to.have.property( 'current', null );
  22. const spy = sinon.spy();
  23. collection.on( 'change:current', spy );
  24. collection.current = 1;
  25. expect( spy.calledOnce ).to.be.true;
  26. } );
  27. } );
  28. describe( 'add', () => {
  29. it( 'binds collection.current to editable.isFocused changes', () => {
  30. const editable = new Editable( editor, 'foo' );
  31. collection.add( editable );
  32. editable.isFocused = true;
  33. expect( collection ).to.have.property( 'current', editable );
  34. editable.isFocused = false;
  35. expect( collection ).to.have.property( 'current', null );
  36. } );
  37. } );
  38. describe( 'remove', () => {
  39. it( 'stops watching editable.isFocused', () => {
  40. const editable = new Editable( editor, 'foo' );
  41. collection.add( editable );
  42. editable.isFocused = true;
  43. collection.remove( editable );
  44. editable.isFocused = false;
  45. expect( collection ).to.have.property( 'current', editable );
  46. } );
  47. } );
  48. describe( 'destroy', () => {
  49. let editables;
  50. beforeEach( () => {
  51. editables = [ new Editable( editor, 'foo' ), new Editable( editor, 'bar' ) ];
  52. collection.add( editables[ 0 ] );
  53. collection.add( editables[ 1 ] );
  54. } );
  55. it( 'stops watching all editables', () => {
  56. collection.destroy();
  57. editables[ 0 ].isFocused = true;
  58. editables[ 1 ].isFocused = true;
  59. expect( collection ).to.have.property( 'current', null );
  60. } );
  61. it( 'destroys all children', () => {
  62. editables.forEach( editable => {
  63. editable.destroy = sinon.spy();
  64. } );
  65. collection.destroy();
  66. expect( editables.map( editable => editable.destroy.calledOnce ) ).to.deep.equal( [ true, true ] );
  67. } );
  68. it( 'removes all children', () => {
  69. collection.destroy();
  70. expect( collection ).to.have.lengthOf( 0 );
  71. } );
  72. } );
  73. } );