wordcount.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 HTMLElement, setTimeout, document */
  6. import WordCount from '../src/wordcount';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  9. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  10. import { setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import { add as addTranslations, _clear as clearTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  12. describe( 'WordCount', () => {
  13. testUtils.createSinonSandbox();
  14. let wordCountPlugin, editor, model;
  15. beforeEach( () => {
  16. return VirtualTestEditor.create( {
  17. plugins: [ WordCount, Paragraph ]
  18. } )
  19. .then( _editor => {
  20. editor = _editor;
  21. model = editor.model;
  22. wordCountPlugin = editor.plugins.get( 'WordCount' );
  23. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  24. } );
  25. } );
  26. describe( 'constructor()', () => {
  27. it( 'has defined "words" property', () => {
  28. expect( wordCountPlugin.words ).to.equal( 0 );
  29. } );
  30. it( 'has defined "characters" property', () => {
  31. expect( wordCountPlugin.characters ).to.equal( 0 );
  32. } );
  33. it( 'has defined "_outputView" property', () => {
  34. expect( wordCountPlugin._outputView ).to.be.undefined;
  35. } );
  36. it( 'has "WordCount" plugin name', () => {
  37. expect( WordCount.pluginName ).to.equal( 'WordCount' );
  38. } );
  39. } );
  40. describe( 'functionality', () => {
  41. it( 'counts words', () => {
  42. expect( wordCountPlugin.words ).to.equal( 0 );
  43. setModelData( model, '<paragraph>Foo(bar)baz</paragraph>' +
  44. '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' +
  45. '<paragraph>1234</paragraph>' +
  46. '<paragraph>(-@#$%^*())</paragraph>' );
  47. wordCountPlugin._calculateWordsAndCharacters();
  48. expect( wordCountPlugin.words ).to.equal( 6 );
  49. } );
  50. it( 'counts characters', () => {
  51. setModelData( model, '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  52. wordCountPlugin._calculateWordsAndCharacters();
  53. expect( wordCountPlugin.characters ).to.equal( 12 );
  54. } );
  55. describe( 'update event', () => {
  56. it( 'fires update event with actual amount of characters and words', () => {
  57. const fake = sinon.fake();
  58. wordCountPlugin.on( 'update', fake );
  59. wordCountPlugin._calculateWordsAndCharacters();
  60. sinon.assert.calledOnce( fake );
  61. sinon.assert.calledWithExactly( fake, sinon.match.any, { words: 0, characters: 0 } );
  62. // _calculateWordsAndCharacters is throttled, so for this test case is run manually
  63. setModelData( model, '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  64. wordCountPlugin._calculateWordsAndCharacters();
  65. sinon.assert.calledTwice( fake );
  66. sinon.assert.calledWithExactly( fake, sinon.match.any, { words: 2, characters: 12 } );
  67. } );
  68. } );
  69. } );
  70. describe( 'self-updating element', () => {
  71. let container;
  72. beforeEach( () => {
  73. container = wordCountPlugin.getWordCountContainer();
  74. } );
  75. it( 'provides html element', () => {
  76. expect( container ).to.be.instanceof( HTMLElement );
  77. } );
  78. it( 'provided element has proper structure', () => {
  79. expect( container.tagName ).to.equal( 'DIV' );
  80. expect( container.classList.contains( 'ck' ) ).to.be.true;
  81. expect( container.classList.contains( 'ck-word-count' ) ).to.be.true;
  82. const children = Array.from( container.children );
  83. expect( children.length ).to.equal( 2 );
  84. expect( children[ 0 ].tagName ).to.equal( 'DIV' );
  85. expect( children[ 0 ].innerHTML ).to.equal( 'Words: 0' );
  86. expect( children[ 1 ].tagName ).to.equal( 'DIV' );
  87. expect( children[ 1 ].innerHTML ).to.equal( 'Characters: 0' );
  88. } );
  89. it( 'updates container content', () => {
  90. expect( container.innerText ).to.equal( 'Words: 0Characters: 0' );
  91. setModelData( model, '<paragraph>Foo(bar)baz</paragraph>' +
  92. '<paragraph><$text foo="true">Hello</$text> world.</paragraph>' );
  93. wordCountPlugin._calculateWordsAndCharacters();
  94. // There is \n between paragraph which has to be included into calculations
  95. expect( container.innerText ).to.equal( 'Words: 5Characters: 24' );
  96. } );
  97. it( 'subsequent calls provides the same element', () => {
  98. const newContainer = wordCountPlugin.getWordCountContainer();
  99. expect( container ).to.equal( newContainer );
  100. } );
  101. describe( 'destroy()', () => {
  102. it( 'html element is removed', done => {
  103. const frag = document.createDocumentFragment();
  104. frag.appendChild( container );
  105. expect( frag.querySelector( '*' ) ).to.be.instanceof( HTMLElement );
  106. editor.destroy()
  107. .then( () => {
  108. expect( frag.querySelector( '*' ) ).to.be.null;
  109. } )
  110. .then( done )
  111. .catch( done );
  112. } );
  113. it( 'method is called', done => {
  114. const spy = sinon.spy( wordCountPlugin, 'destroy' );
  115. editor.destroy()
  116. .then( () => {
  117. sinon.assert.calledOnce( spy );
  118. } )
  119. .then( done )
  120. .catch( done );
  121. } );
  122. } );
  123. } );
  124. describe( '_calculateWordsAndCharacters and throttle', () => {
  125. beforeEach( done => {
  126. // We need to flush initial throttle value after editor's initialization
  127. setTimeout( done, 255 );
  128. } );
  129. it( 'gets update after model data change', done => {
  130. const fake = sinon.fake();
  131. wordCountPlugin.on( 'update', fake );
  132. // Initial change in model should be immediately reflected in word-count
  133. setModelData( model, '<paragraph>Hello world.</paragraph>' );
  134. sinon.assert.calledOnce( fake );
  135. sinon.assert.calledWith( fake, sinon.match.any, { words: 2, characters: 12 } );
  136. // Subsequent updates should be throttle and run with last parameters
  137. setTimeout( () => {
  138. sinon.assert.calledTwice( fake );
  139. sinon.assert.calledWith( fake, sinon.match.any, { words: 2, characters: 9 } );
  140. done();
  141. }, 255 );
  142. setModelData( model, '<paragraph>Hello world</paragraph>' );
  143. setModelData( model, '<paragraph>Hello worl</paragraph>' );
  144. setModelData( model, '<paragraph>Hello wor</paragraph>' );
  145. } );
  146. } );
  147. describe( 'custom config options', () => {
  148. it( 'displayWords = false', done => {
  149. VirtualTestEditor.create( {
  150. plugins: [ WordCount, Paragraph ],
  151. wordCount: {
  152. displayWords: false
  153. }
  154. } )
  155. .then( editor => {
  156. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  157. const container = wordCountPlugin.getWordCountContainer();
  158. expect( container.innerText ).to.equal( 'Characters: 0' );
  159. } )
  160. .then( done )
  161. .catch( done );
  162. } );
  163. it( 'displayCharacters = false', done => {
  164. VirtualTestEditor.create( {
  165. plugins: [ WordCount, Paragraph ],
  166. wordCount: {
  167. displayCharacters: false
  168. }
  169. } )
  170. .then( editor => {
  171. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  172. const container = wordCountPlugin.getWordCountContainer();
  173. expect( container.innerText ).to.equal( 'Words: 0' );
  174. } )
  175. .then( done )
  176. .catch( done );
  177. } );
  178. } );
  179. describe( 'translations', () => {
  180. before( () => {
  181. addTranslations( 'pl', {
  182. Words: 'Słowa',
  183. Characters: 'Znaki'
  184. } );
  185. addTranslations( 'en', {
  186. Words: 'Words',
  187. Characters: 'Characters'
  188. } );
  189. } );
  190. after( () => {
  191. clearTranslations();
  192. } );
  193. it( 'applies proper language translations', done => {
  194. VirtualTestEditor.create( {
  195. plugins: [ WordCount, Paragraph ],
  196. language: 'pl'
  197. } )
  198. .then( editor => {
  199. const wordCountPlugin = editor.plugins.get( 'WordCount' );
  200. const container = wordCountPlugin.getWordCountContainer();
  201. expect( container.innerText ).to.equal( 'Słowa: 0Znaki: 0' );
  202. } )
  203. .then( done )
  204. .catch( done );
  205. } );
  206. } );
  207. } );