8
0

list.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import List from '../src/list';
  7. import ListEngine from '../src/listengine';
  8. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  12. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  13. describe( 'List', () => {
  14. let editor, doc, bulletedListButton, numberedListButton;
  15. beforeEach( () => {
  16. const editorElement = document.createElement( 'div' );
  17. document.body.appendChild( editorElement );
  18. return ClassicTestEditor.create( editorElement, { plugins: [ Paragraph, List ] } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. doc = editor.document;
  22. bulletedListButton = editor.ui.componentFactory.create( 'bulletedList' );
  23. numberedListButton = editor.ui.componentFactory.create( 'numberedList' );
  24. } );
  25. } );
  26. afterEach( () => {
  27. return editor.destroy();
  28. } );
  29. it( 'should be loaded', () => {
  30. expect( editor.plugins.get( List ) ).to.be.instanceOf( List );
  31. } );
  32. it( 'should load ListEngine', () => {
  33. expect( editor.plugins.get( ListEngine ) ).to.be.instanceOf( ListEngine );
  34. } );
  35. it( 'should set up keys for bulleted list and numbered list', () => {
  36. expect( bulletedListButton ).to.be.instanceOf( ButtonView );
  37. expect( numberedListButton ).to.be.instanceOf( ButtonView );
  38. } );
  39. it( 'should execute proper commands when buttons are used', () => {
  40. sinon.spy( editor, 'execute' );
  41. bulletedListButton.fire( 'execute' );
  42. expect( editor.execute.calledWithExactly( 'bulletedList' ) );
  43. numberedListButton.fire( 'execute' );
  44. expect( editor.execute.calledWithExactly( 'numberedList' ) );
  45. } );
  46. it( 'should bind bulleted list button model to bulledList command', () => {
  47. setData( doc, '<listItem type="bulleted" indent="0">[]foo</listItem>' );
  48. const command = editor.commands.get( 'bulletedList' );
  49. expect( bulletedListButton.isOn ).to.be.true;
  50. expect( bulletedListButton.isEnabled ).to.be.true;
  51. command.value = false;
  52. expect( bulletedListButton.isOn ).to.be.false;
  53. command.isEnabled = false;
  54. expect( bulletedListButton.isEnabled ).to.be.false;
  55. } );
  56. it( 'should bind numbered list button model to numberedList command', () => {
  57. setData( doc, '<listItem type="bulleted" indent="0">[]foo</listItem>' );
  58. const command = editor.commands.get( 'numberedList' );
  59. // We are in UL, so numbered list is off.
  60. expect( numberedListButton.isOn ).to.be.false;
  61. expect( numberedListButton.isEnabled ).to.be.true;
  62. command.value = true;
  63. expect( numberedListButton.isOn ).to.be.true;
  64. command.isEnabled = false;
  65. expect( numberedListButton.isEnabled ).to.be.false;
  66. } );
  67. describe( 'enter key handling callback', () => {
  68. it( 'should execute outdentList command on enter key in empty list', () => {
  69. const domEvtDataStub = { preventDefault() {} };
  70. sinon.spy( editor, 'execute' );
  71. setData( doc, '<listItem type="bulleted" indent="0">[]</listItem>' );
  72. editor.editing.view.fire( 'enter', domEvtDataStub );
  73. expect( editor.execute.calledOnce ).to.be.true;
  74. expect( editor.execute.calledWithExactly( 'outdentList' ) );
  75. } );
  76. it( 'should not execute outdentList command on enter key in non-empty list', () => {
  77. const domEvtDataStub = { preventDefault() {} };
  78. sinon.spy( editor, 'execute' );
  79. setData( doc, '<listItem type="bulleted" indent="0">foo[]</listItem>' );
  80. editor.editing.view.fire( 'enter', domEvtDataStub );
  81. expect( editor.execute.called ).to.be.false;
  82. } );
  83. } );
  84. describe( 'tab key handling callback', () => {
  85. let domEvtDataStub;
  86. beforeEach( () => {
  87. domEvtDataStub = {
  88. keyCode: getCode( 'Tab' ),
  89. preventDefault: sinon.spy(),
  90. stopPropagation: sinon.spy()
  91. };
  92. sinon.spy( editor, 'execute' );
  93. } );
  94. afterEach( () => {
  95. editor.execute.restore();
  96. } );
  97. it( 'should execute indentList command on tab key', () => {
  98. setData(
  99. doc,
  100. '<listItem type="bulleted" indent="0">foo</listItem>' +
  101. '<listItem type="bulleted" indent="0">[]bar</listItem>'
  102. );
  103. editor.editing.view.fire( 'keydown', domEvtDataStub );
  104. expect( editor.execute.calledOnce ).to.be.true;
  105. expect( editor.execute.calledWithExactly( 'indentList' ) ).to.be.true;
  106. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  107. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  108. } );
  109. it( 'should execute outdentList command on Shift+Tab keystroke', () => {
  110. domEvtDataStub.keyCode += getCode( 'Shift' );
  111. setData(
  112. doc,
  113. '<listItem type="bulleted" indent="0">foo</listItem>' +
  114. '<listItem type="bulleted" indent="1">[]bar</listItem>'
  115. );
  116. editor.editing.view.fire( 'keydown', domEvtDataStub );
  117. expect( editor.execute.calledOnce ).to.be.true;
  118. expect( editor.execute.calledWithExactly( 'outdentList' ) ).to.be.true;
  119. sinon.assert.calledOnce( domEvtDataStub.preventDefault );
  120. sinon.assert.calledOnce( domEvtDataStub.stopPropagation );
  121. } );
  122. it( 'should not indent if command is disabled', () => {
  123. setData( doc, '<listItem type="bulleted" indent="0">[]foo</listItem>' );
  124. editor.editing.view.fire( 'keydown', domEvtDataStub );
  125. expect( editor.execute.called ).to.be.false;
  126. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  127. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  128. } );
  129. it( 'should not indent or outdent if alt+tab is pressed', () => {
  130. domEvtDataStub.keyCode += getCode( 'alt' );
  131. setData(
  132. doc,
  133. '<listItem type="bulleted" indent="0">foo</listItem>' +
  134. '<listItem type="bulleted" indent="0">[]bar</listItem>'
  135. );
  136. editor.editing.view.fire( 'keydown', domEvtDataStub );
  137. expect( editor.execute.called ).to.be.false;
  138. sinon.assert.notCalled( domEvtDataStub.preventDefault );
  139. sinon.assert.notCalled( domEvtDataStub.stopPropagation );
  140. } );
  141. } );
  142. } );