restrictededitingui.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  7. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import RestrictedEditingEditing from './../src/restrictededitingediting';
  9. import RestrictedEditingUI from './../src/restrictededitingui';
  10. import lockIcon from '../theme/icons/contentlock.svg';
  11. describe( 'RestrictedEditingUI', () => {
  12. let editor, element, goToPreviousCommand, goToNextCommand;
  13. testUtils.createSinonSandbox();
  14. beforeEach( () => {
  15. element = document.createElement( 'div' );
  16. document.body.appendChild( element );
  17. return ClassicTestEditor
  18. .create( element, {
  19. plugins: [ RestrictedEditingEditing, RestrictedEditingUI ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. goToPreviousCommand = editor.commands.get( 'goToPreviousRestrictedEditingRegion' );
  24. goToNextCommand = editor.commands.get( 'goToNextRestrictedEditingRegion' );
  25. } );
  26. } );
  27. afterEach( () => {
  28. element.remove();
  29. return editor.destroy();
  30. } );
  31. describe( 'plugin', () => {
  32. it( 'should be named', () => {
  33. expect( RestrictedEditingUI.pluginName ).to.equal( 'RestrictedEditingUI' );
  34. } );
  35. it( 'should be loaded', () => {
  36. expect( editor.plugins.get( RestrictedEditingUI ) ).to.be.instanceOf( RestrictedEditingUI );
  37. } );
  38. } );
  39. describe( 'restricted editing dropdown', () => {
  40. let dropdown;
  41. beforeEach( () => {
  42. dropdown = editor.ui.componentFactory.create( 'restrictedEditing' );
  43. } );
  44. it( 'the button should have basic properties', () => {
  45. const button = dropdown.buttonView;
  46. expect( button ).to.have.property( 'label', 'Browse editable regions' );
  47. expect( button ).to.have.property( 'tooltip', true );
  48. expect( button ).to.have.property( 'icon', lockIcon );
  49. expect( button ).to.have.property( 'isEnabled', true );
  50. expect( button ).to.have.property( 'isOn', false );
  51. } );
  52. describe( 'exceptions navigation buttons', () => {
  53. it( 'should have one that goes backward', () => {
  54. const list = dropdown.listView;
  55. const button = list.items.first.children.first;
  56. expect( button.isOn ).to.be.false;
  57. expect( button.withText ).to.be.true;
  58. expect( button.label ).to.equal( 'Previous editable region' );
  59. } );
  60. it( 'should have one that goes forward', () => {
  61. const list = dropdown.listView;
  62. const button = list.items.last.children.first;
  63. expect( button.isOn ).to.be.false;
  64. expect( button.withText ).to.be.true;
  65. expect( button.label ).to.equal( 'Next editable region' );
  66. } );
  67. it( 'should focus the view after executing the command', () => {
  68. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  69. const list = dropdown.listView;
  70. const goToPreviousButton = list.items.first.children.first;
  71. goToPreviousButton.fire( 'execute' );
  72. sinon.assert.calledOnce( focusSpy );
  73. } );
  74. it( 'be enabled just like their corresponding commands', () => {
  75. const listView = dropdown.listView;
  76. goToPreviousCommand.isEnabled = false;
  77. goToNextCommand.isEnabled = false;
  78. expect( listView.items.map( item => item.children.first.isEnabled ) ).to.deep.equal( [ false, false ] );
  79. goToPreviousCommand.isEnabled = true;
  80. expect( listView.items.map( item => item.children.first.isEnabled ) ).to.deep.equal( [ true, false ] );
  81. goToNextCommand.isEnabled = true;
  82. expect( listView.items.map( item => item.children.first.isEnabled ) ).to.deep.equal( [ true, true ] );
  83. } );
  84. it( 'should execute their corresponding commands', () => {
  85. const list = dropdown.listView;
  86. const goToPreviousButton = list.items.first.children.first;
  87. const goToNextButton = list.items.last.children.first;
  88. goToPreviousCommand.isEnabled = true;
  89. goToNextCommand.isEnabled = true;
  90. const spy = sinon.spy( editor, 'execute' );
  91. goToPreviousButton.fire( 'execute' );
  92. sinon.assert.calledWith( spy.firstCall, 'goToPreviousRestrictedEditingRegion' );
  93. goToNextButton.fire( 'execute' );
  94. sinon.assert.calledWith( spy.secondCall, 'goToNextRestrictedEditingRegion' );
  95. } );
  96. } );
  97. } );
  98. } );