todolistcheckcommand.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import TodoListEditing from '../src/todolistediting';
  6. import TodoListCheckCommand from '../src/todolistcheckcommand';
  7. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  8. import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  9. describe( 'TodoListCheckCommand', () => {
  10. let editor, model, command;
  11. beforeEach( () => {
  12. return ModelTestEditor
  13. .create( {
  14. plugins: [ TodoListEditing ]
  15. } )
  16. .then( newEditor => {
  17. editor = newEditor;
  18. model = editor.model;
  19. command = new TodoListCheckCommand( editor );
  20. } );
  21. } );
  22. describe( 'value', () => {
  23. it( 'should be false when selection is in not checked element', () => {
  24. setModelData( model, '<listItem listIndent="0" listType="todo">ab[]c</listItem>' );
  25. expect( command.value ).to.equal( false );
  26. } );
  27. it( 'should be true when selection is in checked element', () => {
  28. setModelData( model, '<listItem listIndent="0" listType="todo" todoListChecked="true">ab[]c</listItem>' );
  29. expect( command.value ).to.equal( true );
  30. } );
  31. it( 'should be false when at least one selected element is not checked', () => {
  32. setModelData( model,
  33. '<listItem listIndent="0" listType="todo" todoListChecked="true">ab[c</listItem>' +
  34. '<paragraph>abc</paragraph>' +
  35. '<listItem listIndent="0" listType="todo">abc</listItem>' +
  36. '<listItem listIndent="0" listType="todo" todoListChecked="true">ab]c</listItem>'
  37. );
  38. expect( command.value ).to.equal( false );
  39. } );
  40. it( 'should be true when all selected elements are checked', () => {
  41. setModelData( model,
  42. '<listItem listIndent="0" listType="todo" todoListChecked="true">ab[c</listItem>' +
  43. '<paragraph>abc</paragraph>' +
  44. '<listItem listIndent="0" listType="todo" todoListChecked="true">abc</listItem>' +
  45. '<listItem listIndent="0" listType="todo" todoListChecked="true">ab]c</listItem>'
  46. );
  47. expect( command.value ).to.equal( true );
  48. } );
  49. } );
  50. describe( 'isEnabled', () => {
  51. it( 'should be enabled when selection is inside to-do list item', () => {
  52. setModelData( model, '<listItem listIndent="0" listType="todo">a[b]c</listItem>' );
  53. expect( command.isEnabled ).to.equal( true );
  54. } );
  55. it( 'should be disabled when selection is not inside to-do list item', () => {
  56. setModelData( model, '<paragraph>a[b]c</paragraph>' );
  57. expect( command.isEnabled ).to.equal( false );
  58. } );
  59. it( 'should be enabled when at least one to-do list item is selected', () => {
  60. setModelData( model,
  61. '<paragraph>a[bc</paragraph>' +
  62. '<listItem listIndent="0" listType="todo">abc</listItem>' +
  63. '<paragraph>ab]c</paragraph>'
  64. );
  65. expect( command.isEnabled ).to.equal( true );
  66. } );
  67. it( 'should be enabled when none to-do list item is selected', () => {
  68. setModelData( model,
  69. '<paragraph>a[bc</paragraph>' +
  70. '<paragraph>abc</paragraph>' +
  71. '<paragraph>a]bc</paragraph>'
  72. );
  73. expect( command.isEnabled ).to.equal( false );
  74. } );
  75. } );
  76. describe( 'execute()', () => {
  77. it( 'should toggle checked state on to-do list item when collapsed selection is inside this item', () => {
  78. setModelData( model, '<listItem listIndent="0" listType="todo">b[]ar</listItem>' );
  79. command.execute();
  80. expect( getModelData( model ) ).to.equal(
  81. '<listItem listIndent="0" listType="todo" todoListChecked="true">b[]ar</listItem>'
  82. );
  83. command.execute();
  84. expect( getModelData( model ) ).to.equal(
  85. '<listItem listIndent="0" listType="todo">b[]ar</listItem>'
  86. );
  87. } );
  88. it( 'should toggle checked state on to-do list item when non-collapsed selection is inside this item', () => {
  89. setModelData( model, '<listItem listIndent="0" listType="todo">b[a]r</listItem>' );
  90. command.execute();
  91. expect( getModelData( model ) ).to.equal(
  92. '<listItem listIndent="0" listType="todo" todoListChecked="true">b[a]r</listItem>'
  93. );
  94. command.execute();
  95. expect( getModelData( model ) ).to.equal(
  96. '<listItem listIndent="0" listType="todo">b[a]r</listItem>'
  97. );
  98. } );
  99. it( 'should toggle state on multiple items', () => {
  100. setModelData( model,
  101. '<listItem listIndent="0" listType="todo">abc[</listItem>' +
  102. '<listItem listIndent="0" listType="todo">def</listItem>' +
  103. '<listItem listIndent="0" listType="todo">]ghi</listItem>'
  104. );
  105. command.execute();
  106. expect( getModelData( model ) ).to.equal(
  107. '<listItem listIndent="0" listType="todo" todoListChecked="true">abc[</listItem>' +
  108. '<listItem listIndent="0" listType="todo" todoListChecked="true">def</listItem>' +
  109. '<listItem listIndent="0" listType="todo" todoListChecked="true">]ghi</listItem>'
  110. );
  111. command.execute();
  112. expect( getModelData( model ) ).to.equal(
  113. '<listItem listIndent="0" listType="todo">abc[</listItem>' +
  114. '<listItem listIndent="0" listType="todo">def</listItem>' +
  115. '<listItem listIndent="0" listType="todo">]ghi</listItem>'
  116. );
  117. } );
  118. it( 'should toggle state on multiple items mixed with none to-do list items', () => {
  119. setModelData( model,
  120. '<paragraph>a[bc</paragraph>' +
  121. '<listItem listIndent="0" listType="todo">def</listItem>' +
  122. '<listItem listIndent="0" listType="numbered">ghi</listItem>' +
  123. '<listItem listIndent="0" listType="todo">jkl</listItem>' +
  124. '<paragraph>mn]o</paragraph>'
  125. );
  126. command.execute();
  127. expect( getModelData( model ) ).to.equal(
  128. '<paragraph>a[bc</paragraph>' +
  129. '<listItem listIndent="0" listType="todo" todoListChecked="true">def</listItem>' +
  130. '<listItem listIndent="0" listType="numbered">ghi</listItem>' +
  131. '<listItem listIndent="0" listType="todo" todoListChecked="true">jkl</listItem>' +
  132. '<paragraph>mn]o</paragraph>'
  133. );
  134. command.execute();
  135. expect( getModelData( model ) ).to.equal(
  136. '<paragraph>a[bc</paragraph>' +
  137. '<listItem listIndent="0" listType="todo">def</listItem>' +
  138. '<listItem listIndent="0" listType="numbered">ghi</listItem>' +
  139. '<listItem listIndent="0" listType="todo">jkl</listItem>' +
  140. '<paragraph>mn]o</paragraph>'
  141. );
  142. } );
  143. it( 'should mark all selected items as checked when at least one selected item is not checked', () => {
  144. setModelData( model,
  145. '<listItem listIndent="0" listType="todo">abc[</listItem>' +
  146. '<listItem listIndent="0" listType="todo" todoListChecked="true">def</listItem>' +
  147. '<listItem listIndent="0" listType="todo">]ghi</listItem>'
  148. );
  149. command.execute();
  150. expect( getModelData( model ) ).to.equal(
  151. '<listItem listIndent="0" listType="todo" todoListChecked="true">abc[</listItem>' +
  152. '<listItem listIndent="0" listType="todo" todoListChecked="true">def</listItem>' +
  153. '<listItem listIndent="0" listType="todo" todoListChecked="true">]ghi</listItem>'
  154. );
  155. } );
  156. it( 'should do nothing when there is no elements to toggle attribute', () => {
  157. setModelData( model, '<paragraph>b[]ar</paragraph>' );
  158. command.execute();
  159. expect( getModelData( model ) ).to.equal( '<paragraph>b[]ar</paragraph>' );
  160. } );
  161. it( 'should be up to date just before execution', () => {
  162. setModelData( model,
  163. '<listItem listIndent="0" listType="0">f[]oo</listItem>' +
  164. '<listItem listIndent="0" listType="0">bar</listItem>'
  165. );
  166. model.change( writer => {
  167. writer.setSelection( model.document.getRoot().getChild( 1 ), 'end' );
  168. command.execute();
  169. } );
  170. } );
  171. it( 'should set attribute if `forceValue` parameter is set to `true`', () => {
  172. setModelData( model, '<listItem listIndent="0" listType="todo" todoListChecked="true">b[]ar</listItem>' );
  173. command.execute( { forceValue: true } );
  174. expect( getModelData( model ) ).to.equal(
  175. '<listItem listIndent="0" listType="todo" todoListChecked="true">b[]ar</listItem>'
  176. );
  177. } );
  178. it( 'should remove attribute if `forceValue` parameter is set to `false`', () => {
  179. setModelData( model, '<listItem listIndent="0" listType="todo">b[]ar</listItem>' );
  180. command.execute( { forceValue: false } );
  181. expect( getModelData( model ) ).to.equal(
  182. '<listItem listIndent="0" listType="todo">b[]ar</listItem>'
  183. );
  184. } );
  185. } );
  186. } );