tablepropertiesview.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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 TablePropertiesView from '../../../src/tableproperties/ui/tablepropertiesview';
  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 properties', () => {
  40. describe( 'TablePropertiesView', () => {
  41. let view, locale;
  42. testUtils.createSinonSandbox();
  43. beforeEach( () => {
  44. locale = { t: val => val };
  45. view = new TablePropertiesView( 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. backgroundColor: '',
  67. width: '',
  68. height: '',
  69. alignment: ''
  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-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.widthInput ).to.be.instanceOf( LabeledFieldView );
  85. expect( view.heightInput ).to.be.instanceOf( LabeledFieldView );
  86. expect( view.alignmentToolbar ).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( 'Table 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 obtain the columns configuration', () => {
  214. expect( labeledInput.fieldView.options.columns ).to.equal( 5 );
  215. } );
  216. it( 'should reflect #borderColor property', () => {
  217. view.borderColor = 'foo';
  218. expect( labeledInput.fieldView.value ).to.equal( 'foo' );
  219. view.borderColor = 'bar';
  220. expect( labeledInput.fieldView.value ).to.equal( 'bar' );
  221. } );
  222. it( 'should be enabled only when #borderStyle is different than "none"', () => {
  223. view.borderStyle = '';
  224. expect( labeledInput.isEnabled ).to.be.false;
  225. view.borderStyle = 'dotted';
  226. expect( labeledInput.isEnabled ).to.be.true;
  227. } );
  228. it( 'should update #borderColor on DOM "input" event', () => {
  229. labeledInput.fieldView.value = 'foo';
  230. labeledInput.fieldView.fire( 'input' );
  231. expect( view.borderColor ).to.equal( 'foo' );
  232. labeledInput.fieldView.value = 'bar';
  233. labeledInput.fieldView.fire( 'input' );
  234. expect( view.borderColor ).to.equal( 'bar' );
  235. } );
  236. } );
  237. } );
  238. describe( 'background row', () => {
  239. it( 'should be defined', () => {
  240. const row = view.element.childNodes[ 2 ];
  241. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  242. expect( row.classList.contains( 'ck-table-form__background-row' ) ).to.be.true;
  243. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  244. expect( row.childNodes[ 0 ].textContent ).to.equal( 'Background' );
  245. expect( row.childNodes[ 1 ] ).to.equal( view.backgroundInput.element );
  246. } );
  247. describe( 'background color input', () => {
  248. let labeledInput;
  249. beforeEach( () => {
  250. labeledInput = view.backgroundInput;
  251. } );
  252. it( 'should be created', () => {
  253. expect( labeledInput.fieldView ).to.be.instanceOf( ColorInputView );
  254. expect( labeledInput.label ).to.equal( 'Color' );
  255. expect( labeledInput.class ).to.equal( 'ck-table-properties-form__background' );
  256. } );
  257. it( 'should get the color configuration', () => {
  258. expect( labeledInput.fieldView.options.colorDefinitions ).to.deep.equal( [
  259. {
  260. color: 'rgb(0,255,0)',
  261. label: 'Green',
  262. options: {
  263. hasBorder: false
  264. }
  265. }
  266. ] );
  267. } );
  268. it( 'should obtain the columns configuration', () => {
  269. expect( labeledInput.fieldView.options.columns ).to.equal( 5 );
  270. } );
  271. it( 'should reflect #backgroundColor property', () => {
  272. view.backgroundColor = 'foo';
  273. expect( labeledInput.fieldView.value ).to.equal( 'foo' );
  274. view.backgroundColor = 'bar';
  275. expect( labeledInput.fieldView.value ).to.equal( 'bar' );
  276. } );
  277. it( 'should update #backgroundColor on DOM "input" event', () => {
  278. labeledInput.fieldView.value = 'foo';
  279. labeledInput.fieldView.fire( 'input' );
  280. expect( view.backgroundColor ).to.equal( 'foo' );
  281. labeledInput.fieldView.value = 'bar';
  282. labeledInput.fieldView.fire( 'input' );
  283. expect( view.backgroundColor ).to.equal( 'bar' );
  284. } );
  285. } );
  286. } );
  287. describe( 'dimensions row', () => {
  288. it( 'should be defined', () => {
  289. const row = view.element.childNodes[ 3 ].childNodes[ 0 ];
  290. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  291. expect( row.classList.contains( 'ck-table-form__dimensions-row' ) ).to.be.true;
  292. expect( row.childNodes[ 0 ].textContent ).to.equal( 'Dimensions' );
  293. expect( row.childNodes[ 1 ] ).to.equal( view.widthInput.element );
  294. expect( row.childNodes[ 2 ].textContent ).to.equal( '×' );
  295. expect( row.childNodes[ 3 ] ).to.equal( view.heightInput.element );
  296. } );
  297. describe( 'width input', () => {
  298. let labeledInput;
  299. beforeEach( () => {
  300. labeledInput = view.widthInput;
  301. } );
  302. it( 'should be created', () => {
  303. expect( labeledInput.fieldView ).to.be.instanceOf( InputTextView );
  304. expect( labeledInput.label ).to.equal( 'Width' );
  305. expect( labeledInput.class ).to.equal( 'ck-table-form__dimensions-row__width' );
  306. } );
  307. it( 'should reflect #width property', () => {
  308. view.width = 'foo';
  309. expect( labeledInput.fieldView.value ).to.equal( 'foo' );
  310. view.width = 'bar';
  311. expect( labeledInput.fieldView.value ).to.equal( 'bar' );
  312. } );
  313. it( 'should update #width on DOM "input" event', () => {
  314. labeledInput.fieldView.element.value = 'foo';
  315. labeledInput.fieldView.fire( 'input' );
  316. expect( view.width ).to.equal( 'foo' );
  317. labeledInput.fieldView.element.value = 'bar';
  318. labeledInput.fieldView.fire( 'input' );
  319. expect( view.width ).to.equal( 'bar' );
  320. } );
  321. } );
  322. describe( 'height input', () => {
  323. let labeledInput;
  324. beforeEach( () => {
  325. labeledInput = view.heightInput;
  326. } );
  327. it( 'should be created', () => {
  328. expect( labeledInput.fieldView ).to.be.instanceOf( InputTextView );
  329. expect( labeledInput.label ).to.equal( 'Height' );
  330. expect( labeledInput.class ).to.equal( 'ck-table-form__dimensions-row__height' );
  331. } );
  332. it( 'should reflect #height property', () => {
  333. view.height = 'foo';
  334. expect( labeledInput.fieldView.value ).to.equal( 'foo' );
  335. view.height = 'bar';
  336. expect( labeledInput.fieldView.value ).to.equal( 'bar' );
  337. } );
  338. it( 'should update #height on DOM "input" event', () => {
  339. labeledInput.fieldView.element.value = 'foo';
  340. labeledInput.fieldView.fire( 'input' );
  341. expect( view.height ).to.equal( 'foo' );
  342. labeledInput.fieldView.element.value = 'bar';
  343. labeledInput.fieldView.fire( 'input' );
  344. expect( view.height ).to.equal( 'bar' );
  345. } );
  346. } );
  347. } );
  348. describe( 'dimensions alignment row', () => {
  349. it( 'should be defined', () => {
  350. const row = view.element.childNodes[ 3 ].childNodes[ 1 ];
  351. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  352. expect( row.classList.contains( 'ck-table-properties-form__alignment-row' ) ).to.be.true;
  353. expect( row.childNodes[ 0 ].textContent ).to.equal( 'Alignment' );
  354. expect( row.childNodes[ 1 ] ).to.equal( view.alignmentToolbar.element );
  355. } );
  356. describe( 'alignment toolbar', () => {
  357. let toolbar;
  358. beforeEach( () => {
  359. toolbar = view.alignmentToolbar;
  360. } );
  361. it( 'should be defined', () => {
  362. expect( toolbar ).to.be.instanceOf( ToolbarView );
  363. } );
  364. it( 'should have an ARIA label', () => {
  365. expect( toolbar.ariaLabel ).to.equal( 'Table alignment toolbar' );
  366. } );
  367. it( 'should bring alignment buttons in the right order (left-to-right UI)', () => {
  368. expect( toolbar.items.map( ( { label } ) => label ) ).to.have.ordered.members( [
  369. 'Align table to the left',
  370. 'Center table',
  371. 'Align table to the right'
  372. ] );
  373. expect( toolbar.items.map( ( { isOn } ) => isOn ) ).to.have.ordered.members( [
  374. false, true, false
  375. ] );
  376. } );
  377. it( 'should bring alignment buttons in the right order (right-to-left UI)', () => {
  378. // Creates its own local instances of locale, view and toolbar.
  379. const locale = {
  380. t: val => val,
  381. uiLanguageDirection: 'rtl',
  382. contentLanguageDirection: 'rtl'
  383. };
  384. const view = new TablePropertiesView( locale, VIEW_OPTIONS );
  385. const toolbar = view.alignmentToolbar;
  386. expect( toolbar.items.map( ( { label } ) => label ) ).to.have.ordered.members( [
  387. 'Align table to the right',
  388. 'Center table',
  389. 'Align table to the left'
  390. ] );
  391. expect( toolbar.items.map( ( { isOn } ) => isOn ) ).to.have.ordered.members( [
  392. false, true, false
  393. ] );
  394. view.destroy();
  395. } );
  396. it( 'should change the #horizontalAlignment value', () => {
  397. toolbar.items.last.fire( 'execute' );
  398. expect( view.alignment ).to.equal( 'right' );
  399. expect( toolbar.items.last.isOn ).to.be.true;
  400. toolbar.items.first.fire( 'execute' );
  401. expect( view.alignment ).to.equal( 'left' );
  402. expect( toolbar.items.last.isOn ).to.be.false;
  403. expect( toolbar.items.first.isOn ).to.be.true;
  404. } );
  405. } );
  406. } );
  407. describe( 'action row', () => {
  408. it( 'should be defined', () => {
  409. const row = view.element.childNodes[ 4 ];
  410. expect( row.classList.contains( 'ck-form__row' ) ).to.be.true;
  411. expect( row.classList.contains( 'ck-table-form__action-row' ) ).to.be.true;
  412. expect( row.childNodes[ 0 ] ).to.equal( view.saveButtonView.element );
  413. expect( row.childNodes[ 1 ] ).to.equal( view.cancelButtonView.element );
  414. } );
  415. it( 'should have buttons with right properties', () => {
  416. expect( view.saveButtonView.label ).to.equal( 'Save' );
  417. expect( view.saveButtonView.type ).to.equal( 'submit' );
  418. expect( view.saveButtonView.withText ).to.be.true;
  419. expect( view.saveButtonView.class ).to.equal( 'ck-button-save' );
  420. expect( view.cancelButtonView.label ).to.equal( 'Cancel' );
  421. expect( view.cancelButtonView.withText ).to.be.true;
  422. expect( view.cancelButtonView.class ).to.equal( 'ck-button-cancel' );
  423. } );
  424. it( 'should make the cancel button fire the #cancel event when executed', () => {
  425. const spy = sinon.spy();
  426. view.on( 'cancel', spy );
  427. view.cancelButtonView.fire( 'execute' );
  428. expect( spy.calledOnce ).to.be.true;
  429. } );
  430. it( 'should make sure the #saveButtonView is disabled until text fields are without errors', () => {
  431. view.borderWidthInput.errorText = 'foo';
  432. view.borderColorInput.errorText = 'foo';
  433. view.backgroundInput.errorText = 'foo';
  434. view.widthInput.errorText = 'foo';
  435. view.heightInput.errorText = 'foo';
  436. expect( view.saveButtonView.isEnabled ).to.be.false;
  437. view.borderWidthInput.errorText = 'foo';
  438. view.borderColorInput.errorText = 'foo';
  439. view.backgroundInput.errorText = 'foo';
  440. view.widthInput.errorText = 'foo';
  441. view.heightInput.errorText = null;
  442. expect( view.saveButtonView.isEnabled ).to.be.false;
  443. view.borderWidthInput.errorText = 'foo';
  444. view.borderColorInput.errorText = 'foo';
  445. view.backgroundInput.errorText = 'foo';
  446. view.widthInput.errorText = null;
  447. view.heightInput.errorText = null;
  448. expect( view.saveButtonView.isEnabled ).to.be.false;
  449. view.borderWidthInput.errorText = 'foo';
  450. view.borderColorInput.errorText = 'foo';
  451. view.backgroundInput.errorText = null;
  452. view.widthInput.errorText = null;
  453. view.heightInput.errorText = null;
  454. expect( view.saveButtonView.isEnabled ).to.be.false;
  455. view.borderWidthInput.errorText = 'foo';
  456. view.borderColorInput.errorText = null;
  457. view.backgroundInput.errorText = null;
  458. view.widthInput.errorText = null;
  459. view.heightInput.errorText = null;
  460. expect( view.saveButtonView.isEnabled ).to.be.false;
  461. view.borderWidthInput.errorText = null;
  462. view.borderColorInput.errorText = null;
  463. view.backgroundInput.errorText = null;
  464. view.widthInput.errorText = null;
  465. view.heightInput.errorText = null;
  466. expect( view.saveButtonView.isEnabled ).to.be.true;
  467. } );
  468. } );
  469. } );
  470. it( 'should create #focusTracker instance', () => {
  471. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  472. } );
  473. it( 'should create #keystrokes instance', () => {
  474. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  475. } );
  476. it( 'should create #_focusCycler instance', () => {
  477. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  478. } );
  479. it( 'should create #_focusables view collection', () => {
  480. expect( view._focusables ).to.be.instanceOf( ViewCollection );
  481. } );
  482. } );
  483. describe( 'render()', () => {
  484. it( 'should register child views in #_focusables', () => {
  485. expect( view._focusables.map( f => f ) ).to.have.members( [
  486. view.borderStyleDropdown,
  487. view.borderColorInput,
  488. view.borderWidthInput,
  489. view.backgroundInput,
  490. view.widthInput,
  491. view.heightInput,
  492. view.alignmentToolbar,
  493. view.saveButtonView,
  494. view.cancelButtonView
  495. ] );
  496. } );
  497. it( 'should register child views\' #element in #focusTracker', () => {
  498. const spy = testUtils.sinon.spy( FocusTracker.prototype, 'add' );
  499. const view = new TablePropertiesView( { t: val => val }, VIEW_OPTIONS );
  500. view.render();
  501. sinon.assert.calledWith( spy, view.borderStyleDropdown.element );
  502. sinon.assert.calledWith( spy, view.borderColorInput.element );
  503. sinon.assert.calledWith( spy, view.borderWidthInput.element );
  504. sinon.assert.calledWith( spy, view.backgroundInput.element );
  505. sinon.assert.calledWith( spy, view.widthInput.element );
  506. sinon.assert.calledWith( spy, view.heightInput.element );
  507. sinon.assert.calledWith( spy, view.alignmentToolbar.element );
  508. sinon.assert.calledWith( spy, view.saveButtonView.element );
  509. sinon.assert.calledWith( spy, view.cancelButtonView.element );
  510. view.destroy();
  511. } );
  512. it( 'starts listening for #keystrokes coming from #element', () => {
  513. const view = new TablePropertiesView( { t: val => val }, VIEW_OPTIONS );
  514. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  515. view.render();
  516. sinon.assert.calledOnce( spy );
  517. sinon.assert.calledWithExactly( spy, view.element );
  518. } );
  519. describe( 'activates keyboard navigation for the form', () => {
  520. it( 'so "tab" focuses the next focusable item', () => {
  521. const keyEvtData = {
  522. keyCode: keyCodes.tab,
  523. preventDefault: sinon.spy(),
  524. stopPropagation: sinon.spy()
  525. };
  526. // Mock the border style dropdown button is focused.
  527. view.focusTracker.isFocused = true;
  528. view.focusTracker.focusedElement = view.borderStyleDropdown.element;
  529. const spy = sinon.spy( view.borderColorInput, 'focus' );
  530. view.keystrokes.press( keyEvtData );
  531. sinon.assert.calledOnce( keyEvtData.preventDefault );
  532. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  533. sinon.assert.calledOnce( spy );
  534. } );
  535. it( 'so "shift + tab" focuses the previous focusable item', () => {
  536. const keyEvtData = {
  537. keyCode: keyCodes.tab,
  538. shiftKey: true,
  539. preventDefault: sinon.spy(),
  540. stopPropagation: sinon.spy()
  541. };
  542. // Mock the border style dropdown button is focused.
  543. view.focusTracker.isFocused = true;
  544. view.focusTracker.focusedElement = view.borderStyleDropdown.element;
  545. const spy = sinon.spy( view.cancelButtonView, 'focus' );
  546. view.keystrokes.press( keyEvtData );
  547. sinon.assert.calledOnce( keyEvtData.preventDefault );
  548. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  549. sinon.assert.calledOnce( spy );
  550. } );
  551. } );
  552. } );
  553. describe( 'DOM bindings', () => {
  554. describe( 'submit event', () => {
  555. it( 'should trigger submit event', () => {
  556. const spy = sinon.spy();
  557. view.on( 'submit', spy );
  558. view.element.dispatchEvent( new Event( 'submit' ) );
  559. expect( spy.calledOnce ).to.be.true;
  560. } );
  561. } );
  562. } );
  563. describe( 'focus()', () => {
  564. it( 'focuses the #borderStyleDropdown', () => {
  565. const spy = sinon.spy( view.borderStyleDropdown, 'focus' );
  566. view.focus();
  567. sinon.assert.calledOnce( spy );
  568. } );
  569. } );
  570. } );
  571. } );