restrictededitingui.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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', 'Navigate 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.withKeystroke ).to.be.true;
  59. expect( button.label ).to.equal( 'Previous editable region' );
  60. expect( button.keystroke ).to.equal( 'Shift+Tab' );
  61. } );
  62. it( 'should have one that goes forward', () => {
  63. const list = dropdown.listView;
  64. const button = list.items.last.children.first;
  65. expect( button.isOn ).to.be.false;
  66. expect( button.withText ).to.be.true;
  67. expect( button.withKeystroke ).to.be.true;
  68. expect( button.label ).to.equal( 'Next editable region' );
  69. expect( button.keystroke ).to.equal( 'Tab' );
  70. } );
  71. it( 'should focus the view after executing the command', () => {
  72. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  73. const list = dropdown.listView;
  74. const goToPreviousButton = list.items.first.children.first;
  75. goToPreviousButton.fire( 'execute' );
  76. sinon.assert.calledOnce( focusSpy );
  77. } );
  78. it( 'be enabled just like their corresponding commands', () => {
  79. const listView = dropdown.listView;
  80. goToPreviousCommand.isEnabled = false;
  81. goToNextCommand.isEnabled = false;
  82. expect( listView.items.map( item => item.children.first.isEnabled ) ).to.deep.equal( [ false, false ] );
  83. goToPreviousCommand.isEnabled = true;
  84. expect( listView.items.map( item => item.children.first.isEnabled ) ).to.deep.equal( [ true, false ] );
  85. goToNextCommand.isEnabled = true;
  86. expect( listView.items.map( item => item.children.first.isEnabled ) ).to.deep.equal( [ true, true ] );
  87. } );
  88. it( 'should execute their corresponding commands', () => {
  89. const list = dropdown.listView;
  90. const goToPreviousButton = list.items.first.children.first;
  91. const goToNextButton = list.items.last.children.first;
  92. goToPreviousCommand.isEnabled = true;
  93. goToNextCommand.isEnabled = true;
  94. const spy = sinon.spy( editor, 'execute' );
  95. goToPreviousButton.fire( 'execute' );
  96. sinon.assert.calledWith( spy.firstCall, 'goToPreviousRestrictedEditingRegion' );
  97. goToNextButton.fire( 'execute' );
  98. sinon.assert.calledWith( spy.secondCall, 'goToNextRestrictedEditingRegion' );
  99. } );
  100. } );
  101. } );
  102. } );