8
0

conversion.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Conversion, { ConversionHelpers } from '../../src/conversion/conversion';
  6. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  7. import UpcastDispatcher from '../../src/conversion/upcastdispatcher';
  8. import UpcastHelpers, { convertText, convertToModelFragment } from '../../src/conversion/upcasthelpers';
  9. import DowncastHelpers from '../../src/conversion/downcasthelpers';
  10. import EditingController from '../../src/controller/editingcontroller';
  11. import Model from '../../src/model/model';
  12. import { parse as viewParse, stringify as viewStringify } from '../../src/dev-utils/view';
  13. import { stringify as modelStringify } from '../../src/dev-utils/model';
  14. describe( 'Conversion', () => {
  15. let conversion, dispA, dispB, dispC;
  16. beforeEach( () => {
  17. conversion = new Conversion();
  18. // Placeholders. Will be used only to see if their were given as attribute for a spy function.
  19. dispA = Symbol( 'dispA' );
  20. dispB = Symbol( 'dispB' );
  21. dispC = Symbol( 'dispC' );
  22. conversion.register( 'ab', new UpcastHelpers( [ dispA, dispB ] ) );
  23. conversion.register( 'a', new UpcastHelpers( dispA ) );
  24. conversion.register( 'b', new UpcastHelpers( dispB ) );
  25. conversion.register( 'c', new DowncastHelpers( dispC ) );
  26. } );
  27. describe( 'register()', () => {
  28. it( 'should throw when trying to use same group name twice', () => {
  29. expect( () => {
  30. conversion.register( 'ab' );
  31. } ).to.throw( CKEditorError, /conversion-register-group-exists/ );
  32. } );
  33. } );
  34. describe( 'for()', () => {
  35. it( 'should return ConversionHelpers', () => {
  36. expect( conversion.for( 'ab' ) ).to.be.instanceof( ConversionHelpers );
  37. } );
  38. it( 'should throw if non-existing group name has been used', () => {
  39. expect( () => {
  40. conversion.for( 'foo' );
  41. } ).to.throw( CKEditorError, /conversion-for-unknown-group/ );
  42. } );
  43. it( 'should return proper helpers for group', () => {
  44. expect( conversion.for( 'ab' ) ).to.be.instanceof( UpcastHelpers );
  45. expect( conversion.for( 'a' ) ).to.be.instanceof( UpcastHelpers );
  46. expect( conversion.for( 'b' ) ).to.be.instanceof( UpcastHelpers );
  47. expect( conversion.for( 'c' ) ).to.be.instanceof( DowncastHelpers );
  48. } );
  49. } );
  50. describe( 'add()', () => {
  51. let helperA, helperB;
  52. beforeEach( () => {
  53. helperA = sinon.stub();
  54. helperB = sinon.stub();
  55. } );
  56. it( 'should be chainable', () => {
  57. const forResult = conversion.for( 'ab' );
  58. const addResult = forResult.add( () => {} );
  59. expect( addResult ).to.equal( addResult.add( () => {} ) );
  60. } );
  61. it( 'should fire given helper for every dispatcher in given group', () => {
  62. conversion.for( 'ab' ).add( helperA );
  63. expect( helperA.calledWithExactly( dispA ) ).to.be.true;
  64. expect( helperA.calledWithExactly( dispB ) ).to.be.true;
  65. conversion.for( 'b' ).add( helperB );
  66. expect( helperB.calledWithExactly( dispA ) ).to.be.false;
  67. expect( helperB.calledWithExactly( dispB ) ).to.be.true;
  68. } );
  69. } );
  70. describe( 'converters', () => {
  71. let viewDispatcher, model, schema, conversion, modelRoot, viewRoot;
  72. beforeEach( () => {
  73. model = new Model();
  74. const controller = new EditingController( model );
  75. const modelDoc = model.document;
  76. modelRoot = modelDoc.createRoot();
  77. viewRoot = controller.view.document.getRoot();
  78. // Set name of view root the same as dom root.
  79. // This is a mock of attaching view root to dom root.
  80. viewRoot._name = 'div';
  81. schema = model.schema;
  82. schema.extend( '$text', {
  83. allowIn: '$root',
  84. allowAttributes: [ 'bold' ]
  85. } );
  86. schema.register( 'paragraph', {
  87. inheritAllFrom: '$block'
  88. } );
  89. viewDispatcher = new UpcastDispatcher( model, { schema } );
  90. viewDispatcher.on( 'text', convertText() );
  91. viewDispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
  92. viewDispatcher.on( 'documentFragment', convertToModelFragment(), { priority: 'lowest' } );
  93. conversion = new Conversion();
  94. conversion.register( 'upcast', new UpcastHelpers( [ viewDispatcher ] ) );
  95. conversion.register( 'downcast', new DowncastHelpers( controller.downcastDispatcher ) );
  96. } );
  97. describe( 'elementToElement', () => {
  98. it( 'config.view is a string', () => {
  99. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  100. test( '<p>Foo</p>', '<paragraph>Foo</paragraph>' );
  101. } );
  102. it( 'config.converterPriority is defined (override downcast)', () => {
  103. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  104. conversion.elementToElement( { model: 'paragraph', view: 'div', converterPriority: 'high' } );
  105. test( '<div>Foo</div>', '<paragraph>Foo</paragraph>' );
  106. test( '<p>Foo</p>', '<paragraph>Foo</paragraph>', '<div>Foo</div>' );
  107. } );
  108. it( 'config.converterPriority is defined (override upcast)', () => {
  109. schema.register( 'foo', {
  110. inheritAllFrom: '$block'
  111. } );
  112. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  113. conversion.elementToElement( { model: 'foo', view: 'p', converterPriority: 'high' } );
  114. test( '<p>Foo</p>', '<foo>Foo</foo>', '<p>Foo</p>' );
  115. } );
  116. it( 'config.view is an object', () => {
  117. schema.register( 'fancyParagraph', {
  118. inheritAllFrom: 'paragraph'
  119. } );
  120. conversion.elementToElement( {
  121. model: 'fancyParagraph',
  122. view: {
  123. name: 'p',
  124. classes: 'fancy'
  125. }
  126. } );
  127. test( '<p class="fancy">Foo</p>', '<fancyParagraph>Foo</fancyParagraph>' );
  128. } );
  129. it( 'config.view is an object with upcastAlso defined', () => {
  130. conversion.elementToElement( {
  131. model: 'paragraph',
  132. view: 'p',
  133. upcastAlso: [
  134. 'div',
  135. {
  136. // Any element with `display: block` style.
  137. styles: {
  138. display: 'block'
  139. }
  140. }
  141. ]
  142. } );
  143. test( '<p>Foo</p>', '<paragraph>Foo</paragraph>' );
  144. test( '<div>Foo</div>', '<paragraph>Foo</paragraph>', '<p>Foo</p>' );
  145. test( '<span style="display:block">Foo</span>', '<paragraph>Foo</paragraph>', '<p>Foo</p>' );
  146. } );
  147. it( 'upcastAlso given as a function', () => {
  148. schema.register( 'heading', {
  149. inheritAllFrom: '$block'
  150. } );
  151. conversion.elementToElement( {
  152. model: 'heading',
  153. view: 'h2',
  154. upcastAlso: viewElement => {
  155. const fontSize = viewElement.getStyle( 'font-size' );
  156. if ( !fontSize ) {
  157. return null;
  158. }
  159. const match = fontSize.match( /(\d+)\s*px/ );
  160. if ( !match ) {
  161. return null;
  162. }
  163. const size = Number( match[ 1 ] );
  164. if ( size >= 26 ) {
  165. return { name: true, style: [ 'font-size' ] };
  166. }
  167. return null;
  168. }
  169. } );
  170. conversion.elementToElement( {
  171. model: 'paragraph',
  172. view: 'p'
  173. } );
  174. test( '<p></p>', '<paragraph></paragraph>' );
  175. test( '<p style="font-size:20px"></p>', '<paragraph></paragraph>', '<p></p>' );
  176. test( '<h2></h2>', '<heading></heading>' );
  177. test( '<p style="font-size:26px"></p>', '<heading></heading>', '<h2></h2>' );
  178. } );
  179. } );
  180. describe( 'attributeToElement', () => {
  181. beforeEach( () => {
  182. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  183. } );
  184. it( 'config.view is a string', () => {
  185. conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  186. test( '<p><strong>Foo</strong> bar</p>', '<paragraph><$text bold="true">Foo</$text> bar</paragraph>' );
  187. } );
  188. it( 'config.converterPriority is defined (override downcast)', () => {
  189. conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  190. conversion.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
  191. test( '<p><b>Foo</b></p>', '<paragraph><$text bold="true">Foo</$text></paragraph>' );
  192. test( '<p><strong>Foo</strong></p>', '<paragraph><$text bold="true">Foo</$text></paragraph>', '<p><b>Foo</b></p>' );
  193. } );
  194. it( 'config.converterPriority is defined (override upcast)', () => {
  195. schema.extend( '$text', {
  196. allowAttributes: [ 'foo' ]
  197. } );
  198. conversion.attributeToElement( { model: 'bold', view: 'strong' } );
  199. conversion.attributeToElement( { model: 'foo', view: 'strong', converterPriority: 'high' } );
  200. test(
  201. '<p><strong>Foo</strong></p>',
  202. '<paragraph><$text foo="true">Foo</$text></paragraph>',
  203. '<p><strong>Foo</strong></p>'
  204. );
  205. } );
  206. it( 'config.view is an object', () => {
  207. conversion.attributeToElement( {
  208. model: 'bold',
  209. view: {
  210. name: 'span',
  211. classes: 'bold'
  212. }
  213. } );
  214. test( '<p><span class="bold">Foo</span> bar</p>', '<paragraph><$text bold="true">Foo</$text> bar</paragraph>' );
  215. } );
  216. it( 'config.view is an object with upcastAlso defined', () => {
  217. conversion.attributeToElement( {
  218. model: 'bold',
  219. view: 'strong',
  220. upcastAlso: [
  221. 'b',
  222. {
  223. name: 'span',
  224. classes: 'bold'
  225. },
  226. {
  227. name: 'span',
  228. styles: {
  229. 'font-weight': 'bold'
  230. }
  231. },
  232. viewElement => {
  233. const fontWeight = viewElement.getStyle( 'font-weight' );
  234. if ( viewElement.is( 'span' ) && fontWeight && /\d+/.test( fontWeight ) && Number( fontWeight ) > 500 ) {
  235. return {
  236. name: true,
  237. styles: [ 'font-weight' ]
  238. };
  239. }
  240. }
  241. ]
  242. } );
  243. test(
  244. '<p><strong>Foo</strong></p>',
  245. '<paragraph><$text bold="true">Foo</$text></paragraph>'
  246. );
  247. test(
  248. '<p><b>Foo</b></p>',
  249. '<paragraph><$text bold="true">Foo</$text></paragraph>',
  250. '<p><strong>Foo</strong></p>'
  251. );
  252. test(
  253. '<p><span class="bold">Foo</span></p>',
  254. '<paragraph><$text bold="true">Foo</$text></paragraph>',
  255. '<p><strong>Foo</strong></p>'
  256. );
  257. test(
  258. '<p><span style="font-weight: bold;">Foo</span></p>',
  259. '<paragraph><$text bold="true">Foo</$text></paragraph>',
  260. '<p><strong>Foo</strong></p>'
  261. );
  262. test(
  263. '<p><span style="font-weight: 500;">Foo</span></p>',
  264. '<paragraph>Foo</paragraph>',
  265. '<p>Foo</p>'
  266. );
  267. test(
  268. '<p><span style="font-weight: 600;">Foo</span></p>',
  269. '<paragraph><$text bold="true">Foo</$text></paragraph>',
  270. '<p><strong>Foo</strong></p>'
  271. );
  272. } );
  273. it( 'model attribute value is enumerable', () => {
  274. schema.extend( '$text', {
  275. allowAttributes: [ 'fontSize' ]
  276. } );
  277. conversion.attributeToElement( {
  278. model: {
  279. key: 'fontSize',
  280. values: [ 'big', 'small' ]
  281. },
  282. view: {
  283. big: {
  284. name: 'span',
  285. styles: {
  286. 'font-size': '1.2em'
  287. }
  288. },
  289. small: {
  290. name: 'span',
  291. styles: {
  292. 'font-size': '0.8em'
  293. }
  294. }
  295. },
  296. upcastAlso: {
  297. big: viewElement => {
  298. const fontSize = viewElement.getStyle( 'font-size' );
  299. if ( !fontSize ) {
  300. return null;
  301. }
  302. const match = fontSize.match( /(\d+)\s*px/ );
  303. if ( !match ) {
  304. return null;
  305. }
  306. const size = Number( match[ 1 ] );
  307. if ( viewElement.is( 'span' ) && size > 10 ) {
  308. return { name: true, style: [ 'font-size' ] };
  309. }
  310. return null;
  311. },
  312. small: viewElement => {
  313. const fontSize = viewElement.getStyle( 'font-size' );
  314. if ( !fontSize ) {
  315. return null;
  316. }
  317. const match = fontSize.match( /(\d+)\s*px/ );
  318. if ( !match ) {
  319. return null;
  320. }
  321. const size = Number( match[ 1 ] );
  322. if ( viewElement.is( 'span' ) && size < 10 ) {
  323. return { name: true, style: [ 'font-size' ] };
  324. }
  325. return null;
  326. }
  327. }
  328. } );
  329. test(
  330. '<p><span style="font-size:1.2em">Foo</span> bar</p>',
  331. '<paragraph><$text fontSize="big">Foo</$text> bar</paragraph>'
  332. );
  333. test(
  334. '<p><span style="font-size:12px">Foo</span> bar</p>',
  335. '<paragraph><$text fontSize="big">Foo</$text> bar</paragraph>',
  336. '<p><span style="font-size:1.2em">Foo</span> bar</p>'
  337. );
  338. test(
  339. '<p><span style="font-size:0.8em">Foo</span> bar</p>',
  340. '<paragraph><$text fontSize="small">Foo</$text> bar</paragraph>'
  341. );
  342. test(
  343. '<p><span style="font-size:8px">Foo</span> bar</p>',
  344. '<paragraph><$text fontSize="small">Foo</$text> bar</paragraph>',
  345. '<p><span style="font-size:0.8em">Foo</span> bar</p>'
  346. );
  347. test(
  348. '<p><span style="font-size:10px">Foo</span> bar</p>',
  349. '<paragraph>Foo bar</paragraph>',
  350. '<p>Foo bar</p>'
  351. );
  352. } );
  353. it( 'config.model.name is given', () => {
  354. schema.extend( '$text', {
  355. allowAttributes: [ 'textDecoration' ]
  356. } );
  357. conversion.attributeToElement( {
  358. model: {
  359. key: 'textDecoration',
  360. values: [ 'underline', 'lineThrough' ],
  361. name: '$text'
  362. },
  363. view: {
  364. underline: {
  365. name: 'span',
  366. styles: {
  367. 'text-decoration': 'underline'
  368. }
  369. },
  370. lineThrough: {
  371. name: 'span',
  372. styles: {
  373. 'text-decoration': 'line-through'
  374. }
  375. }
  376. }
  377. } );
  378. test(
  379. '<p><span style="text-decoration:underline">Foo</span></p>',
  380. '<paragraph><$text textDecoration="underline">Foo</$text></paragraph>'
  381. );
  382. test(
  383. '<p><span style="text-decoration:line-through">Foo</span></p>',
  384. '<paragraph><$text textDecoration="lineThrough">Foo</$text></paragraph>'
  385. );
  386. test(
  387. '<p><span style="text-decoration:underline">Foo</span></p>',
  388. '<paragraph><$text textDecoration="underline">Foo</$text></paragraph>'
  389. );
  390. } );
  391. } );
  392. describe( 'attributeToAttribute', () => {
  393. beforeEach( () => {
  394. conversion.elementToElement( { model: 'image', view: 'img' } );
  395. schema.register( 'image', {
  396. inheritAllFrom: '$block',
  397. } );
  398. } );
  399. it( 'config.view and config.model are strings', () => {
  400. schema.extend( 'image', {
  401. allowAttributes: [ 'source' ]
  402. } );
  403. conversion.attributeToAttribute( { model: 'source', view: 'src' } );
  404. test( '<img src="foo.jpg"></img>', '<image source="foo.jpg"></image>' );
  405. } );
  406. it( 'config.view and config.model are objects', () => {
  407. schema.extend( 'image', {
  408. allowAttributes: [ 'aside' ]
  409. } );
  410. conversion.attributeToAttribute( {
  411. model: {
  412. name: 'image',
  413. key: 'aside',
  414. values: [ 'aside' ]
  415. },
  416. view: {
  417. aside: {
  418. name: 'img',
  419. key: 'class',
  420. value: [ 'aside', 'half-size' ]
  421. }
  422. }
  423. } );
  424. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  425. test( '<img class="aside half-size"></img>', '<image aside="aside"></image>' );
  426. test( '<p class="aside half-size"></p>', '<paragraph></paragraph>', '<p></p>' );
  427. } );
  428. it( 'config.view and config.model are objects - convert to style attribute', () => {
  429. schema.extend( 'image', {
  430. allowAttributes: [ 'aside' ]
  431. } );
  432. conversion.attributeToAttribute( {
  433. model: {
  434. name: 'image',
  435. key: 'aside',
  436. values: [ 'aside' ]
  437. },
  438. view: {
  439. aside: {
  440. name: 'img',
  441. key: 'style',
  442. value: {
  443. float: 'right',
  444. width: '50%',
  445. margin: '5px'
  446. }
  447. }
  448. }
  449. } );
  450. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  451. test( '<img style="float:right;margin:5px;width:50%"></img>', '<image aside="aside"></image>' );
  452. test( '<p style="float:right;margin:5px;width:50%"></p>', '<paragraph></paragraph>', '<p></p>' );
  453. } );
  454. it( 'config is an array with upcastAlso defined', () => {
  455. conversion.elementToElement( { model: 'paragraph', view: 'p' } );
  456. schema.extend( 'paragraph', {
  457. allowAttributes: [ 'align' ]
  458. } );
  459. conversion.attributeToAttribute( {
  460. model: {
  461. key: 'align',
  462. values: [ 'right', 'center' ]
  463. },
  464. view: {
  465. right: {
  466. key: 'class',
  467. value: 'align-right'
  468. },
  469. center: {
  470. key: 'class',
  471. value: 'align-center'
  472. }
  473. },
  474. upcastAlso: {
  475. right: {
  476. styles: {
  477. 'text-align': 'right'
  478. }
  479. },
  480. center: {
  481. styles: {
  482. 'text-align': 'center'
  483. }
  484. }
  485. }
  486. } );
  487. test(
  488. '<p class="align-right">Foo</p>',
  489. '<paragraph align="right">Foo</paragraph>'
  490. );
  491. test(
  492. '<p style="text-align:right">Foo</p>',
  493. '<paragraph align="right">Foo</paragraph>',
  494. '<p class="align-right">Foo</p>'
  495. );
  496. test(
  497. '<p class="align-center">Foo</p>',
  498. '<paragraph align="center">Foo</paragraph>'
  499. );
  500. test(
  501. '<p style="text-align:center">Foo</p>',
  502. '<paragraph align="center">Foo</paragraph>',
  503. '<p class="align-center">Foo</p>'
  504. );
  505. } );
  506. it( 'config.view and config.model have name and key set', () => {
  507. schema.extend( 'image', {
  508. allowAttributes: [ 'source' ]
  509. } );
  510. conversion.attributeToAttribute( {
  511. model: {
  512. name: 'image',
  513. key: 'source'
  514. },
  515. view: {
  516. name: 'img',
  517. key: 'src'
  518. }
  519. } );
  520. test( '<img src="foo.jpg"></img>', '<image source="foo.jpg"></image>' );
  521. } );
  522. // #1443.
  523. it( 'should not set attributes on the element\'s children', () => {
  524. schema.register( 'div', {
  525. inheritAllFrom: '$root',
  526. allowWhere: '$block',
  527. isLimit: true,
  528. allowAttributes: [ 'border', 'shade' ]
  529. } );
  530. conversion.elementToElement(
  531. { model: 'div', view: 'div' }
  532. );
  533. conversion.attributeToAttribute( { model: 'border', view: { key: 'class', value: 'border' } } );
  534. conversion.attributeToAttribute( { model: 'shade', view: { key: 'class', value: 'shade' } } );
  535. test(
  536. '<div class="border"><div class="shade"></div></div>',
  537. '<div border="border"><div shade="shade"></div></div>'
  538. );
  539. } );
  540. it( 'config.converterPriority is defined (override downcast)', () => {
  541. schema.extend( 'image', {
  542. allowAttributes: [ 'foo' ]
  543. } );
  544. conversion.attributeToAttribute( { model: 'foo', view: 'foo' } );
  545. conversion.attributeToAttribute( { model: 'foo', view: 'foofoo', converterPriority: 'high' } );
  546. test( '<img foo="foo"></img>', '<image foo="foo"></image>', '<img foofoo="foo"></img>' );
  547. } );
  548. } );
  549. function test( input, expectedModel, expectedView = null ) {
  550. loadData( input );
  551. expect( modelStringify( model.document.getRoot() ) ).to.equal( expectedModel );
  552. expect( viewStringify( viewRoot, null, { ignoreRoot: true } ) ).to.equal( expectedView || input );
  553. }
  554. function loadData( input ) {
  555. const parsedView = viewParse( input );
  556. let convertedModel;
  557. model.change( writer => {
  558. convertedModel = viewDispatcher.convert( parsedView, writer );
  559. } );
  560. model.change( writer => {
  561. writer.remove( writer.createRange(
  562. writer.createPositionAt( modelRoot, 0 ),
  563. writer.createPositionAt( modelRoot, modelRoot.maxOffset ) )
  564. );
  565. writer.insert( convertedModel, modelRoot, 0 );
  566. } );
  567. }
  568. } );
  569. } );
  570. describe( 'ConversionHelpers', () => {
  571. describe( 'add()', () => {
  572. const dispA = Symbol( 'dispA' );
  573. const dispB = Symbol( 'dispB' );
  574. it( 'should call a helper for one defined dispatcher', () => {
  575. const spy = sinon.spy();
  576. const helpers = new ConversionHelpers( dispA );
  577. helpers.add( spy );
  578. sinon.assert.calledOnce( spy );
  579. sinon.assert.calledWithExactly( spy, dispA );
  580. } );
  581. it( 'should call helper for all defined dispatcherers', () => {
  582. const spy = sinon.spy();
  583. const helpers = new ConversionHelpers( [ dispA, dispB ] );
  584. helpers.add( spy );
  585. sinon.assert.calledTwice( spy );
  586. sinon.assert.calledWithExactly( spy, dispA );
  587. sinon.assert.calledWithExactly( spy, dispB );
  588. } );
  589. it( 'should be chainable', () => {
  590. const spy = sinon.spy();
  591. const helpers = new ConversionHelpers( dispA );
  592. expect( helpers.add( spy ) ).to.equal( helpers );
  593. } );
  594. } );
  595. } );