tablecellpropertiesview.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. /* globals Event */
  6. import TableCellPropertiesView from '../../../src/tablecellproperties/ui/tablecellpropertiesview';
  7. import LabeledFieldView from '@ckeditor/ckeditor5-ui/src/labeledfield/labeledfieldview';
  8. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  10. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  11. import FocusCycler from '@ckeditor/ckeditor5-ui/src/focuscycler';
  12. import ViewCollection from '@ckeditor/ckeditor5-ui/src/viewcollection';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import ToolbarView from '@ckeditor/ckeditor5-ui/src/toolbar/toolbarview';
  15. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  16. import InputTextView from '@ckeditor/ckeditor5-ui/src/inputtext/inputtextview';
  17. import ColorInputView from '../../../src/ui/colorinputview';
  18. const VIEW_OPTIONS = {
  19. borderColors: [
  20. {
  21. model: 'rgb(255,0,0)',
  22. label: 'Red',
  23. hasBorder: false
  24. },
  25. {
  26. model: 'rgb(0,0,255)',
  27. label: 'Blue',
  28. hasBorder: false
  29. }
  30. ],
  31. backgroundColors: [
  32. {
  33. model: 'rgb(0,255,0)',
  34. label: 'Green',
  35. hasBorder: false
  36. }
  37. ]
  38. };
  39. describe( 'table cell properties', () => {
  40. describe( 'TableCellPropertiesView', () => {
  41. let view, locale;
  42. testUtils.createSinonSandbox();
  43. beforeEach( () => {
  44. locale = { t: val => val };
  45. view = new TableCellPropertiesView( locale, VIEW_OPTIONS );
  46. view.render();
  47. } );
  48. afterEach( () => {
  49. view.destroy();
  50. } );
  51. describe( 'constructor()', () => {
  52. it( 'should set view#options', () => {
  53. expect( view.options ).to.deep.equal( VIEW_OPTIONS );
  54. } );
  55. it( 'should set view#locale', () => {
  56. expect( view.locale ).to.equal( locale );
  57. } );
  58. it( 'should create view#children collection', () => {
  59. expect( view.children ).to.be.instanceOf( ViewCollection );
  60. } );
  61. it( 'should define the public data interface (observable properties)', () => {
  62. expect( view ).to.include( {
  63. borderStyle: '',
  64. borderWidth: '',
  65. borderColor: '',
  66. padding: '',
  67. backgroundColor: '',
  68. horizontalAlignment: '',
  69. verticalAlignment: ''
  70. } );
  71. } );
  72. it( 'should create element from template', () => {
  73. expect( view.element.classList.contains( 'ck' ) ).to.be.true;
  74. expect( view.element.classList.contains( 'ck-form' ) ).to.be.true;
  75. expect( view.element.classList.contains( 'ck-table-form' ) ).to.be.true;
  76. expect( view.element.classList.contains( 'ck-table-cell-properties-form' ) ).to.be.true;
  77. expect( view.element.getAttribute( 'tabindex' ) ).to.equal( '-1' );
  78. } );
  79. it( 'should create child views (and references)', () => {
  80. expect( view.borderStyleDropdown ).to.be.instanceOf( LabeledFieldView );
  81. expect( view.borderWidthInput ).to.be.instanceOf( LabeledFieldView );
  82. expect( view.borderColorInput ).to.be.instanceOf( LabeledFieldView );
  83. expect( view.backgroundInput ).to.be.instanceOf( LabeledFieldView );
  84. expect( view.paddingInput ).to.be.instanceOf( LabeledFieldView );
  85. expect( view.horizontalAlignmentToolbar ).to.be.instanceOf( ToolbarView );
  86. expect( view.verticalAlignmentToolbar ).to.be.instanceOf( ToolbarView );
  87. expect( view.saveButtonView ).to.be.instanceOf( ButtonView );
  88. expect( view.cancelButtonView ).to.be.instanceOf( ButtonView );
  89. } );
  90. it( 'should have a header', () => {
  91. const header = view.element.firstChild;
  92. expect( header.classList.contains( 'ck' ) ).to.be.true;
  93. expect( header.classList.contains( 'ck-form__header' ) ).to.be.true;
  94. expect( header.textContent ).to.equal( 'Cell properties' );
  95. } );
  96. describe( 'form rows', () => {
  97. describe( 'border row', () => {
  98. it( 'should be defined', () => {
  99. const row = view.element.childNodes[ 1 ];
  100. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  101. expect( row.classList.contains( 'ck-table-form__border-row' ) ).to.be.true;
  102. expect( row.childNodes[ 0 ].textContent ).to.equal( 'Border' );
  103. expect( row.childNodes[ 1 ] ).to.equal( view.borderStyleDropdown.element );
  104. expect( row.childNodes[ 2 ] ).to.equal( view.borderColorInput.element );
  105. expect( row.childNodes[ 3 ] ).to.equal( view.borderWidthInput.element );
  106. } );
  107. describe( 'border style labeled dropdown', () => {
  108. let labeledDropdown;
  109. beforeEach( () => {
  110. labeledDropdown = view.borderStyleDropdown;
  111. } );
  112. it( 'should have properties set', () => {
  113. expect( labeledDropdown.label ).to.equal( 'Style' );
  114. expect( labeledDropdown.class ).to.equal( 'ck-table-form__border-style' );
  115. } );
  116. it( 'should have a button with properties set', () => {
  117. expect( labeledDropdown.fieldView.buttonView.isOn ).to.be.false;
  118. expect( labeledDropdown.fieldView.buttonView.withText ).to.be.true;
  119. expect( labeledDropdown.fieldView.buttonView.tooltip ).to.equal( 'Style' );
  120. } );
  121. it( 'should bind button\'s label to #borderStyle property', () => {
  122. view.borderStyle = 'dotted';
  123. expect( labeledDropdown.fieldView.buttonView.label ).to.equal( 'Dotted' );
  124. view.borderStyle = 'dashed';
  125. expect( labeledDropdown.fieldView.buttonView.label ).to.equal( 'Dashed' );
  126. } );
  127. it( 'should bind #isEmpty to #borderStyle property', () => {
  128. view.borderStyle = 'dotted';
  129. expect( labeledDropdown.isEmpty ).to.be.false;
  130. view.borderStyle = null;
  131. expect( labeledDropdown.isEmpty ).to.be.true;
  132. } );
  133. it( 'should change #borderStyle when executed', () => {
  134. labeledDropdown.fieldView.listView.items.first.children.first.fire( 'execute' );
  135. expect( view.borderStyle ).to.equal( '' );
  136. labeledDropdown.fieldView.listView.items.last.children.first.fire( 'execute' );
  137. expect( view.borderStyle ).to.equal( 'outset' );
  138. } );
  139. it( 'should come with a set of pre–defined border styles', () => {
  140. expect( labeledDropdown.fieldView.listView.items.map( item => {
  141. return item.children.first.label;
  142. } ) ).to.have.ordered.members( [
  143. 'None', 'Solid', 'Dotted', 'Dashed', 'Double', 'Groove', 'Ridge', 'Inset', 'Outset'
  144. ] );
  145. } );
  146. it( 'should reset border width and color inputs when setting style to none', () => {
  147. view.borderStyle = 'dotted';
  148. view.borderWidth = '1px';
  149. view.borderColor = 'red';
  150. view.borderStyle = '';
  151. expect( view.borderColor ).to.equal( '' );
  152. expect( view.borderWidth ).to.equal( '' );
  153. } );
  154. } );
  155. describe( 'border width input', () => {
  156. let labeledInput;
  157. beforeEach( () => {
  158. labeledInput = view.borderWidthInput;
  159. } );
  160. it( 'should be created', () => {
  161. expect( labeledInput.fieldView ).to.be.instanceOf( InputTextView );
  162. expect( labeledInput.label ).to.equal( 'Width' );
  163. expect( labeledInput.class ).to.equal( 'ck-table-form__border-width' );
  164. } );
  165. it( 'should reflect #borderWidth property', () => {
  166. view.borderWidth = 'foo';
  167. expect( labeledInput.fieldView.value ).to.equal( 'foo' );
  168. view.borderWidth = 'bar';
  169. expect( labeledInput.fieldView.value ).to.equal( 'bar' );
  170. } );
  171. it( 'should be enabled only when #borderStyle is different than "none"', () => {
  172. view.borderStyle = '';
  173. expect( labeledInput.isEnabled ).to.be.false;
  174. view.borderStyle = 'dotted';
  175. expect( labeledInput.isEnabled ).to.be.true;
  176. } );
  177. it( 'should update #borderWidth on DOM "input" event', () => {
  178. labeledInput.fieldView.element.value = 'foo';
  179. labeledInput.fieldView.fire( 'input' );
  180. expect( view.borderWidth ).to.equal( 'foo' );
  181. labeledInput.fieldView.element.value = 'bar';
  182. labeledInput.fieldView.fire( 'input' );
  183. expect( view.borderWidth ).to.equal( 'bar' );
  184. } );
  185. } );
  186. describe( 'border color input', () => {
  187. let labeledInput;
  188. beforeEach( () => {
  189. labeledInput = view.borderColorInput;
  190. } );
  191. it( 'should be created', () => {
  192. expect( labeledInput.fieldView ).to.be.instanceOf( ColorInputView );
  193. expect( labeledInput.label ).to.equal( 'Color' );
  194. } );
  195. it( 'should get the color configuration', () => {
  196. expect( labeledInput.fieldView.options.colorDefinitions ).to.deep.equal( [
  197. {
  198. color: 'rgb(255,0,0)',
  199. label: 'Red',
  200. options: {
  201. hasBorder: false
  202. }
  203. },
  204. {
  205. color: 'rgb(0,0,255)',
  206. label: 'Blue',
  207. options: {
  208. hasBorder: false
  209. }
  210. }
  211. ] );
  212. } );
  213. it( 'should reflect #borderColor property', () => {
  214. view.borderColor = 'foo';
  215. expect( labeledInput.fieldView.value ).to.equal( 'foo' );
  216. view.borderColor = 'bar';
  217. expect( labeledInput.fieldView.value ).to.equal( 'bar' );
  218. } );
  219. it( 'should be enabled only when #borderStyle is different than "none"', () => {
  220. view.borderStyle = '';
  221. expect( labeledInput.isEnabled ).to.be.false;
  222. view.borderStyle = 'dotted';
  223. expect( labeledInput.isEnabled ).to.be.true;
  224. } );
  225. it( 'should update #borderColor on DOM "input" event', () => {
  226. labeledInput.fieldView.value = 'foo';
  227. labeledInput.fieldView.fire( 'input' );
  228. expect( view.borderColor ).to.equal( 'foo' );
  229. labeledInput.fieldView.value = 'bar';
  230. labeledInput.fieldView.fire( 'input' );
  231. expect( view.borderColor ).to.equal( 'bar' );
  232. } );
  233. } );
  234. } );
  235. describe( 'background row', () => {
  236. it( 'should be defined', () => {
  237. const row = view.element.childNodes[ 2 ];
  238. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  239. expect( row.classList.contains( 'ck-table-form__background-row' ) ).to.be.true;
  240. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  241. expect( row.childNodes[ 0 ].textContent ).to.equal( 'Background' );
  242. expect( row.childNodes[ 1 ] ).to.equal( view.backgroundInput.element );
  243. } );
  244. describe( 'background color input', () => {
  245. let labeledInput;
  246. beforeEach( () => {
  247. labeledInput = view.backgroundInput;
  248. } );
  249. it( 'should be created', () => {
  250. expect( labeledInput.fieldView ).to.be.instanceOf( ColorInputView );
  251. expect( labeledInput.label ).to.equal( 'Color' );
  252. expect( labeledInput.class ).to.equal( 'ck-table-cell-properties-form__background' );
  253. } );
  254. it( 'should get the color configuration', () => {
  255. expect( labeledInput.fieldView.options.colorDefinitions ).to.deep.equal( [
  256. {
  257. color: 'rgb(0,255,0)',
  258. label: 'Green',
  259. options: {
  260. hasBorder: false
  261. }
  262. }
  263. ] );
  264. } );
  265. it( 'should reflect #backgroundColor property', () => {
  266. view.backgroundColor = 'foo';
  267. expect( labeledInput.fieldView.value ).to.equal( 'foo' );
  268. view.backgroundColor = 'bar';
  269. expect( labeledInput.fieldView.value ).to.equal( 'bar' );
  270. } );
  271. it( 'should update #backgroundColor on DOM "input" event', () => {
  272. labeledInput.fieldView.value = 'foo';
  273. labeledInput.fieldView.fire( 'input' );
  274. expect( view.backgroundColor ).to.equal( 'foo' );
  275. labeledInput.fieldView.value = 'bar';
  276. labeledInput.fieldView.fire( 'input' );
  277. expect( view.backgroundColor ).to.equal( 'bar' );
  278. } );
  279. } );
  280. } );
  281. describe( 'dimensions row', () => {
  282. it( 'should be defined', () => {
  283. const row = view.element.childNodes[ 3 ].childNodes[ 0 ];
  284. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  285. expect( row.classList.contains( 'ck-table-form__dimensions-row' ) ).to.be.true;
  286. expect( row.childNodes[ 0 ].textContent ).to.equal( 'Dimensions' );
  287. expect( row.childNodes[ 1 ] ).to.equal( view.widthInput.element );
  288. expect( row.childNodes[ 2 ].textContent ).to.equal( '×' );
  289. expect( row.childNodes[ 3 ] ).to.equal( view.heightInput.element );
  290. } );
  291. describe( 'width input', () => {
  292. let labeledInput;
  293. beforeEach( () => {
  294. labeledInput = view.widthInput;
  295. } );
  296. it( 'should be created', () => {
  297. expect( labeledInput.fieldView ).to.be.instanceOf( InputTextView );
  298. expect( labeledInput.label ).to.equal( 'Width' );
  299. expect( labeledInput.class ).to.equal( 'ck-table-form__dimensions-row__width' );
  300. } );
  301. it( 'should reflect #width property', () => {
  302. view.width = 'foo';
  303. expect( labeledInput.fieldView.value ).to.equal( 'foo' );
  304. view.width = 'bar';
  305. expect( labeledInput.fieldView.value ).to.equal( 'bar' );
  306. } );
  307. it( 'should update #width on DOM "input" event', () => {
  308. labeledInput.fieldView.element.value = 'foo';
  309. labeledInput.fieldView.fire( 'input' );
  310. expect( view.width ).to.equal( 'foo' );
  311. labeledInput.fieldView.element.value = 'bar';
  312. labeledInput.fieldView.fire( 'input' );
  313. expect( view.width ).to.equal( 'bar' );
  314. } );
  315. } );
  316. describe( 'height input', () => {
  317. let labeledInput;
  318. beforeEach( () => {
  319. labeledInput = view.heightInput;
  320. } );
  321. it( 'should be created', () => {
  322. expect( labeledInput.fieldView ).to.be.instanceOf( InputTextView );
  323. expect( labeledInput.label ).to.equal( 'Height' );
  324. expect( labeledInput.class ).to.equal( 'ck-table-form__dimensions-row__height' );
  325. } );
  326. it( 'should reflect #height property', () => {
  327. view.height = 'foo';
  328. expect( labeledInput.fieldView.value ).to.equal( 'foo' );
  329. view.height = 'bar';
  330. expect( labeledInput.fieldView.value ).to.equal( 'bar' );
  331. } );
  332. it( 'should update #height on DOM "input" event', () => {
  333. labeledInput.fieldView.element.value = 'foo';
  334. labeledInput.fieldView.fire( 'input' );
  335. expect( view.height ).to.equal( 'foo' );
  336. labeledInput.fieldView.element.value = 'bar';
  337. labeledInput.fieldView.fire( 'input' );
  338. expect( view.height ).to.equal( 'bar' );
  339. } );
  340. } );
  341. } );
  342. describe( 'padding row', () => {
  343. it( 'should be defined', () => {
  344. const row = view.element.childNodes[ 3 ].childNodes[ 1 ];
  345. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  346. expect( row.classList.contains( 'ck-table-cell-properties-form__padding-row' ) ).to.be.true;
  347. expect( row.childNodes[ 0 ] ).to.equal( view.paddingInput.element );
  348. } );
  349. describe( 'padding input', () => {
  350. let labeledInput;
  351. beforeEach( () => {
  352. labeledInput = view.paddingInput;
  353. } );
  354. it( 'should be created', () => {
  355. expect( labeledInput.fieldView ).to.be.instanceOf( InputTextView );
  356. expect( labeledInput.label ).to.equal( 'Padding' );
  357. expect( labeledInput.class ).to.equal( 'ck-table-cell-properties-form__padding' );
  358. } );
  359. it( 'should reflect #padding property', () => {
  360. view.padding = 'foo';
  361. expect( labeledInput.fieldView.value ).to.equal( 'foo' );
  362. view.padding = 'bar';
  363. expect( labeledInput.fieldView.value ).to.equal( 'bar' );
  364. } );
  365. it( 'should update #padding on DOM "input" event', () => {
  366. labeledInput.fieldView.element.value = 'foo';
  367. labeledInput.fieldView.fire( 'input' );
  368. expect( view.padding ).to.equal( 'foo' );
  369. labeledInput.fieldView.element.value = 'bar';
  370. labeledInput.fieldView.fire( 'input' );
  371. expect( view.padding ).to.equal( 'bar' );
  372. } );
  373. } );
  374. } );
  375. describe( 'text alignment row', () => {
  376. it( 'should be defined', () => {
  377. const row = view.element.childNodes[ 4 ];
  378. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  379. expect( row.classList.contains( 'ck-table-cell-properties-form__alignment-row' ) ).to.be.true;
  380. expect( row.childNodes[ 0 ].textContent ).to.equal( 'Table cell text alignment' );
  381. expect( row.childNodes[ 1 ] ).to.equal( view.horizontalAlignmentToolbar.element );
  382. expect( row.childNodes[ 2 ] ).to.equal( view.verticalAlignmentToolbar.element );
  383. } );
  384. describe( 'horizontal text alignment toolbar', () => {
  385. let toolbar;
  386. beforeEach( () => {
  387. toolbar = view.horizontalAlignmentToolbar;
  388. } );
  389. it( 'should be defined', () => {
  390. expect( toolbar ).to.be.instanceOf( ToolbarView );
  391. } );
  392. it( 'should have an ARIA label', () => {
  393. expect( toolbar.ariaLabel ).to.equal( 'Horizontal text alignment toolbar' );
  394. } );
  395. it( 'should bring alignment buttons in the right order (left-to-right UI)', () => {
  396. expect( toolbar.items.map( ( { label } ) => label ) ).to.have.ordered.members( [
  397. 'Align cell text to the left',
  398. 'Align cell text to the center',
  399. 'Align cell text to the right',
  400. 'Justify cell text'
  401. ] );
  402. expect( toolbar.items.map( ( { isOn } ) => isOn ) ).to.have.ordered.members( [
  403. true, false, false, false
  404. ] );
  405. } );
  406. it( 'should bring alignment buttons in the right order (right-to-left UI)', () => {
  407. // Creates its own local instances of locale, view and toolbar.
  408. const locale = {
  409. t: val => val,
  410. uiLanguageDirection: 'rtl',
  411. contentLanguageDirection: 'rtl'
  412. };
  413. const view = new TableCellPropertiesView( locale, VIEW_OPTIONS );
  414. const toolbar = view.horizontalAlignmentToolbar;
  415. expect( toolbar.items.map( ( { label } ) => label ) ).to.have.ordered.members( [
  416. 'Align cell text to the right',
  417. 'Align cell text to the center',
  418. 'Align cell text to the left',
  419. 'Justify cell text'
  420. ] );
  421. expect( toolbar.items.map( ( { isOn } ) => isOn ) ).to.have.ordered.members( [
  422. true, false, false, false
  423. ] );
  424. view.destroy();
  425. } );
  426. it( 'should change the #horizontalAlignment value', () => {
  427. toolbar.items.last.fire( 'execute' );
  428. expect( view.horizontalAlignment ).to.equal( 'justify' );
  429. expect( toolbar.items.last.isOn ).to.be.true;
  430. toolbar.items.first.fire( 'execute' );
  431. expect( view.horizontalAlignment ).to.equal( '' );
  432. expect( toolbar.items.last.isOn ).to.be.false;
  433. expect( toolbar.items.first.isOn ).to.be.true;
  434. } );
  435. } );
  436. describe( 'vertical text alignment toolbar', () => {
  437. let toolbar;
  438. beforeEach( () => {
  439. toolbar = view.verticalAlignmentToolbar;
  440. } );
  441. it( 'should be defined', () => {
  442. expect( toolbar ).to.be.instanceOf( ToolbarView );
  443. } );
  444. it( 'should have an ARIA label', () => {
  445. expect( toolbar.ariaLabel ).to.equal( 'Vertical text alignment toolbar' );
  446. } );
  447. it( 'should bring alignment buttons', () => {
  448. expect( toolbar.items.map( ( { label } ) => label ) ).to.have.ordered.members( [
  449. 'Align cell text to the top',
  450. 'Align cell text to the middle',
  451. 'Align cell text to the bottom'
  452. ] );
  453. expect( toolbar.items.map( ( { isOn } ) => isOn ) ).to.have.ordered.members( [
  454. false, true, false
  455. ] );
  456. } );
  457. it( 'should change the #verticalAlignment value', () => {
  458. toolbar.items.last.fire( 'execute' );
  459. expect( view.verticalAlignment ).to.equal( 'bottom' );
  460. expect( toolbar.items.last.isOn ).to.be.true;
  461. toolbar.items.first.fire( 'execute' );
  462. expect( view.verticalAlignment ).to.equal( 'top' );
  463. expect( toolbar.items.last.isOn ).to.be.false;
  464. expect( toolbar.items.first.isOn ).to.be.true;
  465. } );
  466. } );
  467. } );
  468. describe( 'action row', () => {
  469. it( 'should be defined', () => {
  470. const row = view.element.childNodes[ 5 ];
  471. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  472. expect( row.classList.contains( 'ck-table-form__action-row' ) ).to.be.true;
  473. expect( row.childNodes[ 0 ] ).to.equal( view.saveButtonView.element );
  474. expect( row.childNodes[ 1 ] ).to.equal( view.cancelButtonView.element );
  475. } );
  476. it( 'should have buttons with right properties', () => {
  477. expect( view.saveButtonView.label ).to.equal( 'Save' );
  478. expect( view.saveButtonView.type ).to.equal( 'submit' );
  479. expect( view.saveButtonView.withText ).to.be.true;
  480. expect( view.saveButtonView.class ).to.equal( 'ck-button-save' );
  481. expect( view.cancelButtonView.label ).to.equal( 'Cancel' );
  482. expect( view.cancelButtonView.withText ).to.be.true;
  483. expect( view.cancelButtonView.class ).to.equal( 'ck-button-cancel' );
  484. } );
  485. it( 'should make the cancel button fire the #cancel event when executed', () => {
  486. const spy = sinon.spy();
  487. view.on( 'cancel', spy );
  488. view.cancelButtonView.fire( 'execute' );
  489. expect( spy.calledOnce ).to.be.true;
  490. } );
  491. it( 'should make sure the #saveButtonView is disabled until text fields are without errors', () => {
  492. view.borderWidthInput.errorText = 'foo';
  493. view.borderColorInput.errorText = 'foo';
  494. view.backgroundInput.errorText = 'foo';
  495. view.paddingInput.errorText = 'foo';
  496. expect( view.saveButtonView.isEnabled ).to.be.false;
  497. view.borderWidthInput.errorText = 'foo';
  498. view.borderColorInput.errorText = 'foo';
  499. view.backgroundInput.errorText = 'foo';
  500. view.paddingInput.errorText = null;
  501. expect( view.saveButtonView.isEnabled ).to.be.false;
  502. view.borderWidthInput.errorText = 'foo';
  503. view.borderColorInput.errorText = 'foo';
  504. view.backgroundInput.errorText = null;
  505. view.paddingInput.errorText = null;
  506. expect( view.saveButtonView.isEnabled ).to.be.false;
  507. view.borderWidthInput.errorText = 'foo';
  508. view.borderColorInput.errorText = null;
  509. view.backgroundInput.errorText = null;
  510. view.paddingInput.errorText = null;
  511. expect( view.saveButtonView.isEnabled ).to.be.false;
  512. view.borderWidthInput.errorText = null;
  513. view.borderColorInput.errorText = null;
  514. view.backgroundInput.errorText = null;
  515. view.paddingInput.errorText = null;
  516. expect( view.saveButtonView.isEnabled ).to.be.true;
  517. } );
  518. } );
  519. } );
  520. it( 'should create #focusTracker instance', () => {
  521. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  522. } );
  523. it( 'should create #keystrokes instance', () => {
  524. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  525. } );
  526. it( 'should create #_focusCycler instance', () => {
  527. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  528. } );
  529. it( 'should create #_focusables view collection', () => {
  530. expect( view._focusables ).to.be.instanceOf( ViewCollection );
  531. } );
  532. } );
  533. describe( 'render()', () => {
  534. it( 'should register child views in #_focusables', () => {
  535. expect( view._focusables.map( f => f ) ).to.have.members( [
  536. view.borderStyleDropdown,
  537. view.borderColorInput,
  538. view.borderWidthInput,
  539. view.backgroundInput,
  540. view.widthInput,
  541. view.heightInput,
  542. view.paddingInput,
  543. view.horizontalAlignmentToolbar,
  544. view.verticalAlignmentToolbar,
  545. view.saveButtonView,
  546. view.cancelButtonView
  547. ] );
  548. } );
  549. it( 'should register child views\' #element in #focusTracker', () => {
  550. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  551. const view = new TableCellPropertiesView( { t: val => val }, VIEW_OPTIONS );
  552. view.render();
  553. sinon.assert.calledWith( spy, view.borderStyleDropdown.element );
  554. sinon.assert.calledWith( spy, view.borderColorInput.element );
  555. sinon.assert.calledWith( spy, view.borderWidthInput.element );
  556. sinon.assert.calledWith( spy, view.backgroundInput.element );
  557. sinon.assert.calledWith( spy, view.paddingInput.element );
  558. sinon.assert.calledWith( spy, view.horizontalAlignmentToolbar.element );
  559. sinon.assert.calledWith( spy, view.verticalAlignmentToolbar.element );
  560. sinon.assert.calledWith( spy, view.saveButtonView.element );
  561. sinon.assert.calledWith( spy, view.cancelButtonView.element );
  562. view.destroy();
  563. } );
  564. it( 'starts listening for #keystrokes coming from #element', () => {
  565. const view = new TableCellPropertiesView( { t: val => val }, VIEW_OPTIONS );
  566. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  567. view.render();
  568. sinon.assert.calledOnce( spy );
  569. sinon.assert.calledWithExactly( spy, view.element );
  570. } );
  571. describe( 'activates keyboard navigation for the form', () => {
  572. it( 'so "tab" focuses the next focusable item', () => {
  573. const keyEvtData = {
  574. keyCode: keyCodes.tab,
  575. preventDefault: sinon.spy(),
  576. stopPropagation: sinon.spy()
  577. };
  578. // Mock the border style dropdown button is focused.
  579. view.focusTracker.isFocused = true;
  580. view.focusTracker.focusedElement = view.borderStyleDropdown.element;
  581. const spy = sinon.spy( view.borderColorInput, 'focus' );
  582. view.keystrokes.press( keyEvtData );
  583. sinon.assert.calledOnce( keyEvtData.preventDefault );
  584. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  585. sinon.assert.calledOnce( spy );
  586. } );
  587. it( 'so "shift + tab" focuses the previous focusable item', () => {
  588. const keyEvtData = {
  589. keyCode: keyCodes.tab,
  590. shiftKey: true,
  591. preventDefault: sinon.spy(),
  592. stopPropagation: sinon.spy()
  593. };
  594. // Mock the border style dropdown button is focused.
  595. view.focusTracker.isFocused = true;
  596. view.focusTracker.focusedElement = view.borderStyleDropdown.element;
  597. const spy = sinon.spy( view.cancelButtonView, 'focus' );
  598. view.keystrokes.press( keyEvtData );
  599. sinon.assert.calledOnce( keyEvtData.preventDefault );
  600. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  601. sinon.assert.calledOnce( spy );
  602. } );
  603. } );
  604. } );
  605. describe( 'DOM bindings', () => {
  606. describe( 'submit event', () => {
  607. it( 'should trigger submit event', () => {
  608. const spy = sinon.spy();
  609. view.on( 'submit', spy );
  610. view.element.dispatchEvent( new Event( 'submit' ) );
  611. expect( spy.calledOnce ).to.be.true;
  612. } );
  613. } );
  614. } );
  615. describe( 'focus()', () => {
  616. it( 'focuses the #borderStyleDropdown', () => {
  617. const spy = sinon.spy( view.borderStyleDropdown, 'focus' );
  618. view.focus();
  619. sinon.assert.calledOnce( spy );
  620. } );
  621. } );
  622. } );
  623. } );