8
0

viewconsumable.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treecontroller */
  6. 'use strict';
  7. import ViewElement from '/ckeditor5/core/treeview/element.js';
  8. import ViewConsumable from '/ckeditor5/core/treecontroller/viewconsumable.js';
  9. describe( 'ViewConsumable', () => {
  10. let viewConsumable;
  11. beforeEach( () => {
  12. viewConsumable = new ViewConsumable();
  13. } );
  14. describe( 'add', () => {
  15. let el;
  16. beforeEach( () => {
  17. el = new ViewElement( 'p' );
  18. } );
  19. it( 'should allow to add element', () => {
  20. viewConsumable.add( el );
  21. expect( viewConsumable.test( el ) ).to.be.true;
  22. } );
  23. it( 'should allow to add element inside description object', () => {
  24. viewConsumable.add( { element: el } );
  25. expect( viewConsumable.test( el ) ).to.be.true;
  26. } );
  27. it( 'should allow to add attributes classes and styles', () => {
  28. viewConsumable.add( { element: el, attribute: 'href' } );
  29. viewConsumable.add( { element: el, class: 'foobar' } );
  30. viewConsumable.add( { element: el, style: 'color' } );
  31. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.true;
  32. expect( viewConsumable.test( { element: el, class: 'foobar' } ) ).to.be.true;
  33. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.true;
  34. expect( viewConsumable.test( el ) ).to.be.false;
  35. } );
  36. it( 'should allow to add attributes classes and styles in one call', () => {
  37. viewConsumable.add( { element: el, attribute: 'href', class: 'foobar', style: 'color' } );
  38. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.true;
  39. expect( viewConsumable.test( { element: el, class: 'foobar' } ) ).to.be.true;
  40. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.true;
  41. expect( viewConsumable.test( el ) ).to.be.false;
  42. } );
  43. it( 'should allow to add multiple attributes in one call', () => {
  44. viewConsumable.add( { element: el, attribute: [ 'href', 'target', 'title' ] } );
  45. expect( viewConsumable.test( { element: el, attribute: 'href' } ) ).to.be.true;
  46. expect( viewConsumable.test( { element: el, attribute: 'target' } ) ).to.be.true;
  47. expect( viewConsumable.test( { element: el, attribute: 'title' } ) ).to.be.true;
  48. expect( viewConsumable.test( el ) ).to.be.false;
  49. } );
  50. it( 'should allow to add multiple classes in one call', () => {
  51. viewConsumable.add( { element: el, class: [ 'foo', 'bar', 'baz' ] } );
  52. expect( viewConsumable.test( { element: el, class: 'foo' } ) ).to.be.true;
  53. expect( viewConsumable.test( { element: el, class: 'bar' } ) ).to.be.true;
  54. expect( viewConsumable.test( { element: el, class: 'baz' } ) ).to.be.true;
  55. expect( viewConsumable.test( el ) ).to.be.false;
  56. } );
  57. it( 'should allow to add multiple styles in one call', () => {
  58. viewConsumable.add( { element: el, style: [ 'color', 'position', 'top' ] } );
  59. expect( viewConsumable.test( { element: el, style: 'color' } ) ).to.be.true;
  60. expect( viewConsumable.test( { element: el, style: 'position' } ) ).to.be.true;
  61. expect( viewConsumable.test( { element: el, style: 'top' } ) ).to.be.true;
  62. expect( viewConsumable.test( el ) ).to.be.false;
  63. } );
  64. it( 'should allow to add multiple consumables in one call', () => {
  65. viewConsumable.add( el, { element: el, style: 'color' } );
  66. expect( viewConsumable.test( el ) ).to.be.true;
  67. expect( viewConsumable.test( { element: el, style: 'color' } ) );
  68. } );
  69. it( 'should throw an error when element is not provided', () => {
  70. expect( () => {
  71. viewConsumable.add( { style: 'color' } );
  72. } ).to.throw( 'viewconsumable-element-missing' );
  73. } );
  74. it( 'should throw if class attribute is added', () => {
  75. expect( () => {
  76. viewConsumable.add( { element: el, attribute: 'class' } );
  77. } ).to.throw( 'viewconsumable-invalid-attribute' );
  78. } );
  79. it( 'should throw if style attribute is added', () => {
  80. expect( () => {
  81. viewConsumable.add( { element: el, attribute: 'style' } );
  82. } ).to.throw( 'viewconsumable-invalid-attribute' );
  83. } );
  84. } );
  85. } );