checkboxRenderer.spec.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. describe('CheckboxRenderer', () => {
  2. var id = 'testContainer';
  3. beforeEach(function() {
  4. this.$container = $(`<div id="${id}" style="width: 300px; height: 200px;"></div>`).appendTo('body');
  5. });
  6. afterEach(function() {
  7. if (this.$container) {
  8. destroy();
  9. this.$container.remove();
  10. }
  11. });
  12. it('should render values as checkboxes', () => {
  13. handsontable({
  14. data: [[true], [false], [true]],
  15. columns: [
  16. {type: 'checkbox'}
  17. ]
  18. });
  19. expect($(getRenderedValue(0, 0)).is(':checkbox')).toBe(true);
  20. expect($(getRenderedValue(1, 0)).is(':checkbox')).toBe(true);
  21. expect($(getRenderedValue(2, 0)).is(':checkbox')).toBe(true);
  22. });
  23. it('should render check checkboxes for cell which value is true', () => {
  24. handsontable({
  25. data: [[true], [false], [true]],
  26. columns: [
  27. {type: 'checkbox' }
  28. ]
  29. });
  30. expect($(getRenderedContent(0, 0)).prop('checked')).toBe(true);
  31. expect($(getRenderedContent(1, 0)).prop('checked')).toBe(false);
  32. expect($(getRenderedContent(2, 0)).prop('checked')).toBe(true);
  33. });
  34. it('should use templates to check appropriate checkboxes', () => {
  35. handsontable({
  36. data: [['yes'], ['no'], ['yes']],
  37. columns: [
  38. {
  39. type: 'checkbox',
  40. checkedTemplate: 'yes',
  41. uncheckedTemplate: 'no'
  42. }
  43. ]
  44. });
  45. expect($(getRenderedContent(0, 0)).prop('checked')).toBe(true);
  46. expect($(getRenderedContent(1, 0)).prop('checked')).toBe(false);
  47. expect($(getRenderedContent(2, 0)).prop('checked')).toBe(true);
  48. });
  49. it('should select cell after checkbox click', function() {
  50. var hot = handsontable({
  51. data: [[true], [false], [true]],
  52. columns: [
  53. {type: 'checkbox'}
  54. ]
  55. });
  56. hot.selectCell(0, 0);
  57. this.$container.find(':checkbox').eq(2).simulate('mousedown');
  58. expect(hot.getSelected()).toEqual([2, 0, 2, 0]);
  59. });
  60. it('should select cell after label click', function() {
  61. var hot = handsontable({
  62. data: [[true], [false], [true]],
  63. columns: [
  64. {type: 'checkbox', label: {position: 'before', value: 'Sure? '}}
  65. ]
  66. });
  67. hot.selectCell(0, 0);
  68. this.$container.find('td label').eq(2).simulate('mousedown');
  69. expect(hot.getSelected()).toEqual([2, 0, 2, 0]);
  70. });
  71. it('should reverse selection in checkboxes', function() {
  72. handsontable({
  73. data: [[true], [false], [true]],
  74. columns: [
  75. {type: 'checkbox' }
  76. ]
  77. });
  78. this.$container.find(':checkbox').eq(0).simulate('click');
  79. this.$container.find(':checkbox').eq(1).simulate('click');
  80. this.$container.find(':checkbox').eq(2).simulate('click');
  81. expect(getData()).toEqual([[false], [true], [false]]);
  82. });
  83. it('shouldn\'t uncheck checkboxes', function() {
  84. handsontable({
  85. data: [[true], [true], [true]],
  86. columns: [
  87. {type: 'checkbox', readOnly: true}
  88. ]
  89. });
  90. this.$container.find(':checkbox').trigger('click');
  91. expect(getData()).toEqual([[true], [true], [true]]);
  92. });
  93. it('should check single box after hitting space', function() {
  94. handsontable({
  95. data: [[true], [true], [true]],
  96. columns: [
  97. {type: 'checkbox'}
  98. ]
  99. });
  100. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  101. addHook('afterChange', afterChangeCallback);
  102. var checkboxes = this.$container.find(':checkbox');
  103. expect(checkboxes.eq(0).prop('checked')).toBe(true);
  104. expect(checkboxes.eq(1).prop('checked')).toBe(true);
  105. expect(checkboxes.eq(2).prop('checked')).toBe(true);
  106. expect(getData()).toEqual([[true], [true], [true]]);
  107. selectCell(0, 0);
  108. // this.$container.find(':checkbox').eq(0).simulate('click');
  109. // this.$container.simulate('keydown',{
  110. // keyCode: 32
  111. // });
  112. keyDown('space');
  113. checkboxes = this.$container.find(':checkbox');
  114. expect(checkboxes.eq(0).prop('checked')).toBe(false);
  115. expect(checkboxes.eq(1).prop('checked')).toBe(true);
  116. expect(checkboxes.eq(2).prop('checked')).toBe(true);
  117. expect(getData()).toEqual([[false], [true], [true]]);
  118. expect(afterChangeCallback.calls.count()).toEqual(1);
  119. expect(afterChangeCallback).toHaveBeenCalledWith([[0, 0, true, false]], 'edit', undefined, undefined, undefined, undefined);
  120. });
  121. it('should not check single box after hitting space, if cell is readOnly', function() {
  122. handsontable({
  123. data: [[true], [true], [true]],
  124. columns: [
  125. {type: 'checkbox', readOnly: true}
  126. ]
  127. });
  128. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  129. addHook('afterChange', afterChangeCallback);
  130. var checkboxes = this.$container.find(':checkbox');
  131. expect(checkboxes.eq(0).prop('checked')).toBe(true);
  132. expect(checkboxes.eq(1).prop('checked')).toBe(true);
  133. expect(checkboxes.eq(2).prop('checked')).toBe(true);
  134. expect(getData()).toEqual([[true], [true], [true]]);
  135. selectCell(0, 0);
  136. keyDown('space');
  137. checkboxes = this.$container.find(':checkbox');
  138. expect(checkboxes.eq(0).prop('checked')).toBe(true);
  139. expect(checkboxes.eq(1).prop('checked')).toBe(true);
  140. expect(checkboxes.eq(2).prop('checked')).toBe(true);
  141. expect(getData()).toEqual([[true], [true], [true]]);
  142. expect(afterChangeCallback).not.toHaveBeenCalled();
  143. });
  144. it('should not check single box after hitting space, if last column is readOnly (#3562)', function() {
  145. handsontable({
  146. data: [[true, true], [false, false], [true, true]],
  147. columns: [
  148. {type: 'checkbox'},
  149. {type: 'checkbox', readOnly: true}
  150. ]
  151. });
  152. selectCell(0, 0);
  153. keyDown('space');
  154. selectCell(0, 1);
  155. keyDown('space');
  156. selectCell(1, 0);
  157. keyDown('space');
  158. selectCell(1, 1);
  159. keyDown('space');
  160. var checkboxes = this.$container.find(':checkbox');
  161. // column 0
  162. expect(checkboxes.eq(0).prop('checked')).toBe(false);
  163. expect(checkboxes.eq(2).prop('checked')).toBe(true);
  164. expect(checkboxes.eq(4).prop('checked')).toBe(true);
  165. // column 1
  166. expect(checkboxes.eq(1).prop('checked')).toBe(true);
  167. expect(checkboxes.eq(3).prop('checked')).toBe(false);
  168. expect(checkboxes.eq(5).prop('checked')).toBe(true);
  169. expect(getData()).toEqual([[false, true], [true, false], [true, true]]);
  170. });
  171. it('should change checkboxes values properly when data contains null or/and undefined', () => {
  172. handsontable({
  173. data: [[null], [undefined]],
  174. colHeaders: true,
  175. columns: [
  176. {
  177. type: 'checkbox'
  178. }
  179. ]
  180. });
  181. selectCell(0, 0, 1, 0);
  182. keyDown('space');
  183. expect(getDataAtCol(0)).toEqual([true, true]);
  184. selectCell(0, 0, 1, 0);
  185. keyDown('space');
  186. expect(getDataAtCol(0)).toEqual([false, false]);
  187. });
  188. it('should change checkboxes values for cells below the viewport (hot initialized by startRows) #4037', () => {
  189. handsontable({
  190. startRows: 200,
  191. colHeaders: true,
  192. columns: [
  193. {
  194. type: 'checkbox'
  195. }
  196. ]
  197. });
  198. selectCell(0, 0, 199, 0);
  199. keyDown('space');
  200. expect(getDataAtCell(199, 0)).toEqual(true);
  201. });
  202. it('should reverse checkboxes state after hitting space, when multiple cells are selected', function() {
  203. var hot = handsontable({
  204. data: [[true], [false], [true]],
  205. columns: [
  206. {type: 'checkbox'}
  207. ]
  208. });
  209. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  210. addHook('afterChange', afterChangeCallback);
  211. var checkboxes = this.$container.find(':checkbox');
  212. expect(checkboxes.eq(0).prop('checked')).toBe(true);
  213. expect(checkboxes.eq(1).prop('checked')).toBe(false);
  214. expect(checkboxes.eq(2).prop('checked')).toBe(true);
  215. expect(getData()).toEqual([[true], [false], [true]]);
  216. selectCell(0, 0, 2, 0);
  217. keyDown('space');
  218. checkboxes = this.$container.find(':checkbox');
  219. expect(checkboxes.eq(0).prop('checked')).toBe(false);
  220. expect(checkboxes.eq(1).prop('checked')).toBe(true);
  221. expect(checkboxes.eq(2).prop('checked')).toBe(false);
  222. expect(getData()).toEqual([[false], [true], [false]]);
  223. expect(afterChangeCallback.calls.count()).toEqual(1);
  224. expect(afterChangeCallback).toHaveBeenCalledWith([[0, 0, true, false], [1, 0, false, true], [2, 0, true, false]], 'edit', undefined, undefined, undefined, undefined);
  225. });
  226. it('should reverse checkboxes state after hitting space, when multiple cells are selected and selStart > selEnd', function() {
  227. handsontable({
  228. data: [[true], [false], [true]],
  229. columns: [
  230. {type: 'checkbox'}
  231. ]
  232. });
  233. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  234. addHook('afterChange', afterChangeCallback);
  235. var checkboxes = this.$container.find(':checkbox');
  236. expect(checkboxes.eq(0).prop('checked')).toBe(true);
  237. expect(checkboxes.eq(1).prop('checked')).toBe(false);
  238. expect(checkboxes.eq(2).prop('checked')).toBe(true);
  239. expect(getData()).toEqual([[true], [false], [true]]);
  240. selectCell(2, 0, 0, 0); // selStart = [2,0], selEnd = [0,0]
  241. keyDown('space');
  242. checkboxes = this.$container.find(':checkbox');
  243. expect(checkboxes.eq(0).prop('checked')).toBe(false);
  244. expect(checkboxes.eq(1).prop('checked')).toBe(true);
  245. expect(checkboxes.eq(2).prop('checked')).toBe(false);
  246. expect(getData()).toEqual([[false], [true], [false]]);
  247. expect(afterChangeCallback.calls.count()).toEqual(1);
  248. expect(afterChangeCallback).toHaveBeenCalledWith([[0, 0, true, false], [1, 0, false, true], [2, 0, true, false]], 'edit', undefined, undefined, undefined, undefined);
  249. });
  250. it('should open cell editors of cell that does not have checkboxRenderer (#1199)', () => {
  251. var hot = handsontable({
  252. data: [[true, 'B0'], [true, 'B1'], [true, 'B2']],
  253. columns: [
  254. {type: 'checkbox'},
  255. {type: 'text'}
  256. ]
  257. });
  258. selectCell(0, 1);
  259. expect(hot.getActiveEditor().isOpened()).toBe(false);
  260. keyDown('space');
  261. expect(hot.getActiveEditor().isOpened()).toBe(true);
  262. });
  263. it('double click on checkbox cell should invert the value', () => {
  264. handsontable({
  265. data: [
  266. [true],
  267. [false],
  268. [true]
  269. ],
  270. columns: [
  271. {type: 'checkbox'}
  272. ]
  273. });
  274. selectCell(0, 0);
  275. mouseDoubleClick(getCell(0, 0));
  276. expect(getDataAtCell(0, 0)).toBe(false);
  277. mouseDoubleClick(getCell(0, 0));
  278. expect(getDataAtCell(0, 0)).toBe(true);
  279. mouseDoubleClick(getCell(0, 0));
  280. expect(getDataAtCell(0, 0)).toBe(false);
  281. });
  282. it('should change checkbox state from checked to unchecked after hitting ENTER', function() {
  283. handsontable({
  284. data: [[true], [true], [true]],
  285. columns: [
  286. {type: 'checkbox'}
  287. ]
  288. });
  289. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  290. addHook('afterChange', afterChangeCallback);
  291. var checkboxes = this.$container.find(':checkbox');
  292. expect(checkboxes.eq(0).prop('checked')).toBe(true);
  293. expect(checkboxes.eq(1).prop('checked')).toBe(true);
  294. expect(checkboxes.eq(2).prop('checked')).toBe(true);
  295. expect(getData()).toEqual([[true], [true], [true]]);
  296. selectCell(0, 0);
  297. keyDown('enter');
  298. checkboxes = this.$container.find(':checkbox');
  299. expect(checkboxes.eq(0).prop('checked')).toBe(false);
  300. expect(checkboxes.eq(1).prop('checked')).toBe(true);
  301. expect(checkboxes.eq(2).prop('checked')).toBe(true);
  302. expect(getData()).toEqual([[false], [true], [true]]);
  303. expect(afterChangeCallback.calls.count()).toEqual(1);
  304. expect(afterChangeCallback).toHaveBeenCalledWith([[0, 0, true, false]], 'edit', undefined, undefined, undefined, undefined);
  305. });
  306. it('should change checkbox state from checked to unchecked after hitting ENTER using custom check/uncheck templates', function() {
  307. handsontable({
  308. data: [['yes'], ['yes'], ['no']],
  309. columns: [
  310. {
  311. type: 'checkbox',
  312. checkedTemplate: 'yes',
  313. uncheckedTemplate: 'no'
  314. }
  315. ]
  316. });
  317. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  318. addHook('afterChange', afterChangeCallback);
  319. var checkboxes = this.$container.find(':checkbox');
  320. expect(checkboxes.eq(0).prop('checked')).toBe(true);
  321. expect(checkboxes.eq(1).prop('checked')).toBe(true);
  322. expect(checkboxes.eq(2).prop('checked')).toBe(false);
  323. expect(getData()).toEqual([['yes'], ['yes'], ['no']]);
  324. selectCell(0, 0);
  325. keyDown('enter');
  326. checkboxes = this.$container.find(':checkbox');
  327. expect(checkboxes.eq(0).prop('checked')).toBe(false);
  328. expect(checkboxes.eq(1).prop('checked')).toBe(true);
  329. expect(checkboxes.eq(2).prop('checked')).toBe(false);
  330. expect(getData()).toEqual([['no'], ['yes'], ['no']]);
  331. expect(afterChangeCallback.calls.count()).toEqual(1);
  332. expect(afterChangeCallback).toHaveBeenCalledWith([[0, 0, 'yes', 'no']], 'edit', undefined, undefined, undefined, undefined);
  333. });
  334. it('should change checkbox state from checked to unchecked after hitting ENTER using custom check/uncheck templates in numeric format', function() {
  335. handsontable({
  336. data: [[1], [1], [0]],
  337. columns: [
  338. {
  339. type: 'checkbox',
  340. checkedTemplate: 1,
  341. uncheckedTemplate: 0
  342. }
  343. ]
  344. });
  345. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  346. addHook('afterChange', afterChangeCallback);
  347. var checkboxes = this.$container.find(':checkbox');
  348. expect(checkboxes.eq(0).prop('checked')).toBe(true);
  349. expect(checkboxes.eq(1).prop('checked')).toBe(true);
  350. expect(checkboxes.eq(2).prop('checked')).toBe(false);
  351. expect(getData()).toEqual([[1], [1], [0]]);
  352. selectCell(0, 0);
  353. keyDown('enter');
  354. checkboxes = this.$container.find(':checkbox');
  355. expect(checkboxes.eq(0).prop('checked')).toBe(false);
  356. expect(checkboxes.eq(1).prop('checked')).toBe(true);
  357. expect(checkboxes.eq(2).prop('checked')).toBe(false);
  358. expect(getData()).toEqual([[0], [1], [0]]);
  359. expect(afterChangeCallback.calls.count()).toEqual(1);
  360. expect(afterChangeCallback).toHaveBeenCalledWith([[0, 0, 1, 0]], 'edit', undefined, undefined, undefined, undefined);
  361. });
  362. it('should change checkbox state to unchecked after hitting DELETE', function() {
  363. handsontable({
  364. data: [[true], [false], [true]],
  365. columns: [
  366. { type: 'checkbox'}
  367. ]
  368. });
  369. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  370. addHook('afterChange', afterChangeCallback);
  371. var checkboxes = this.$container.find(':checkbox');
  372. expect(checkboxes.eq(0).prop('checked')).toBe(true);
  373. expect(checkboxes.eq(1).prop('checked')).toBe(false);
  374. expect(checkboxes.eq(2).prop('checked')).toBe(true);
  375. expect(getData()).toEqual([[true], [false], [true]]);
  376. selectCell(0, 0);
  377. keyDown('delete');
  378. selectCell(0, 1);
  379. keyDown('delete');
  380. checkboxes = this.$container.find(':checkbox');
  381. expect(checkboxes.eq(0).prop('checked')).toBe(false);
  382. expect(checkboxes.eq(1).prop('checked')).toBe(false);
  383. expect(checkboxes.eq(2).prop('checked')).toBe(true);
  384. expect(getData()).toEqual([[false], [false], [true]]);
  385. expect(afterChangeCallback.calls.count()).toEqual(2);
  386. expect(afterChangeCallback).toHaveBeenCalledWith([[0, 0, true, false]], 'edit', undefined, undefined, undefined, undefined);
  387. });
  388. it('should change checkbox notte to unchecked after hitting BACKSPACE', function() {
  389. handsontable({
  390. data: [[true], [false], [true]],
  391. columns: [
  392. { type: 'checkbox'}
  393. ]
  394. });
  395. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  396. addHook('afterChange', afterChangeCallback);
  397. var checkboxes = this.$container.find(':checkbox');
  398. expect(checkboxes.eq(0).prop('checked')).toBe(true);
  399. expect(checkboxes.eq(1).prop('checked')).toBe(false);
  400. expect(checkboxes.eq(2).prop('checked')).toBe(true);
  401. expect(getData()).toEqual([[true], [false], [true]]);
  402. selectCell(0, 0);
  403. keyDown('backspace');
  404. selectCell(0, 1);
  405. keyDown('backspace');
  406. checkboxes = this.$container.find(':checkbox');
  407. expect(checkboxes.eq(0).prop('checked')).toBe(false);
  408. expect(checkboxes.eq(1).prop('checked')).toBe(false);
  409. expect(checkboxes.eq(2).prop('checked')).toBe(true);
  410. expect(getData()).toEqual([[false], [false], [true]]);
  411. expect(afterChangeCallback.calls.count()).toEqual(2);
  412. expect(afterChangeCallback).toHaveBeenCalledWith([[0, 0, true, false]], 'edit', undefined, undefined, undefined, undefined);
  413. });
  414. it('should change notkbox state to unchecked after hitting DELETE (from #bad-value# state)', () => {
  415. handsontable({
  416. data: [['foo'], ['bar']],
  417. columns: [
  418. {type: 'checkbox'}
  419. ]
  420. });
  421. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  422. addHook('afterChange', afterChangeCallback);
  423. expect(getDataAtCell(0, 0)).toBe('foo');
  424. expect(getDataAtCell(1, 0)).toBe('bar');
  425. selectCell(0, 0);
  426. keyDown('delete');
  427. selectCell(1, 0);
  428. keyDown('delete');
  429. expect(getDataAtCell(0, 0)).toBe(false);
  430. expect(getDataAtCell(1, 0)).toBe(false);
  431. expect(getData()).toEqual([[false], [false]]);
  432. expect(afterChangeCallback.calls.count()).toEqual(2);
  433. expect(afterChangeCallback).toHaveBeenCalledWith([[0, 0, 'foo', false]], 'edit', undefined, undefined, undefined, undefined);
  434. });
  435. it('should change checkbox note to unchecked after hitting BACKSPACE (from #bad-value# state)', () => {
  436. handsontable({
  437. data: [['foo'], ['bar']],
  438. columns: [
  439. {type: 'checkbox'}
  440. ]
  441. });
  442. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  443. addHook('afterChange', afterChangeCallback);
  444. expect(getDataAtCell(0, 0)).toBe('foo');
  445. expect(getDataAtCell(1, 0)).toBe('bar');
  446. selectCell(0, 0);
  447. keyDown('backspace');
  448. selectCell(1, 0);
  449. keyDown('backspace');
  450. expect(getDataAtCell(0, 0)).toBe(false);
  451. expect(getDataAtCell(1, 0)).toBe(false);
  452. expect(getData()).toEqual([[false], [false]]);
  453. expect(afterChangeCallback.calls.count()).toEqual(2);
  454. expect(afterChangeCallback).toHaveBeenCalledWith([[0, 0, 'foo', false]], 'edit', undefined, undefined, undefined, undefined);
  455. });
  456. it('shouldn\'t change checkbo notate after hitting other keys then DELETE or BACKSPACE (from #bad-value# state)', () => {
  457. handsontable({
  458. data: [['foo'], ['bar']],
  459. columns: [
  460. {type: 'checkbox'}
  461. ]
  462. });
  463. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  464. addHook('afterChange', afterChangeCallback);
  465. expect(getDataAtCell(0, 0)).toBe('foo');
  466. selectCell(0, 0);
  467. keyDown('space');
  468. selectCell(0, 0);
  469. keyDown('c');
  470. expect(getDataAtCell(0, 0)).toBe('foo');
  471. expect(getData()).toEqual([['foo'], ['bar']]);
  472. expect(afterChangeCallback.calls.count()).toEqual(0);
  473. });
  474. it('should not change checkbox state after hitting other keys then SPACE, ENTER, DELETE or BACKSPACE', () => {
  475. handsontable({
  476. data: [[false], [true], [true]],
  477. columns: [
  478. {type: 'checkbox'}
  479. ]
  480. });
  481. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  482. addHook('afterChange', afterChangeCallback);
  483. selectCell(0, 0);
  484. keyDown('space');
  485. expect(getDataAtCell(0, 0)).toBe(true);
  486. selectCell(0, 0);
  487. keyDown('c');
  488. expect(getDataAtCell(0, 0)).toBe(true);
  489. expect(afterChangeCallback.calls.count()).toEqual(1);
  490. });
  491. it('should add label on the beginning of a checkbox element', () => {
  492. handsontable({
  493. data: [{checked: true, label: 'myLabel'}, {checked: false, label: 'myLabel'}],
  494. columns: [
  495. {type: 'checkbox', data: 'checked', label: {position: 'before', property: 'label'}}
  496. ]
  497. });
  498. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  499. addHook('afterChange', afterChangeCallback);
  500. selectCell(0, 0);
  501. keyDown('space');
  502. expect(getDataAtCell(0, 0)).toBe(false);
  503. expect(getDataAtCell(1, 0)).toBe(false);
  504. expect(afterChangeCallback.calls.count()).toEqual(1);
  505. expect(getCell(0, 0).querySelector('label').firstChild.textContent).toEqual('myLabel');
  506. });
  507. it('should add label on the end of a checkbox element', () => {
  508. handsontable({
  509. data: [{checked: true, label: 'myLabel'}, {checked: false, label: 'myLabel'}],
  510. columns: [
  511. {type: 'checkbox', data: 'checked', label: {position: 'after', property: 'label'}}
  512. ]
  513. });
  514. var afterChangeCallback = jasmine.createSpy('afterChangeCallback');
  515. addHook('afterChange', afterChangeCallback);
  516. selectCell(0, 0);
  517. keyDown('space');
  518. expect(getDataAtCell(0, 0)).toBe(false);
  519. expect(getDataAtCell(1, 0)).toBe(false);
  520. expect(afterChangeCallback.calls.count()).toEqual(1);
  521. expect(getCell(0, 0).querySelector('label').lastChild.textContent).toEqual('myLabel');
  522. });
  523. it('should not add label when value is incorrect (#bad-value)', () => {
  524. handsontable({
  525. data: [{checked: 1, label: 'myLabel'}, {checked: 0, label: 'myLabel'}],
  526. columns: [
  527. {type: 'checkbox', data: 'checked', label: {position: 'after', property: 'label'}}
  528. ]
  529. });
  530. expect(getCell(0, 0).querySelector('label')).toBe(null);
  531. });
  532. it('by default should add label on the end of a checkbox element', () => {
  533. handsontable({
  534. data: [{checked: true, label: {test: 'Baz'}}, {checked: false, label: {test: 'Baz'}}],
  535. columns: [
  536. {type: 'checkbox', data: 'checked', label: {property: 'label.test'}}
  537. ]
  538. });
  539. expect(getCell(0, 0).querySelector('label').lastChild.textContent).toEqual('Baz');
  540. });
  541. it('should add label with text filled from `value` label setting (passed as string)', () => {
  542. handsontable({
  543. data: [{checked: true}, {checked: false}],
  544. columns: [
  545. {type: 'checkbox', data: 'checked', label: {value: 'myLabel'}}
  546. ]
  547. });
  548. expect(getCell(0, 0).querySelector('label').lastChild.textContent).toEqual('myLabel');
  549. });
  550. it('should add label with text filled from `value` label setting (passed as function)', () => {
  551. var labelFunction = jasmine.createSpy();
  552. labelFunction.and.returnValue('myLabel');
  553. handsontable({
  554. autoRowSize: false,
  555. autoColumnSize: false,
  556. data: [{checked: true}, {checked: false}],
  557. columns: [
  558. {type: 'checkbox', data: 'checked', label: {value: labelFunction}}
  559. ]
  560. });
  561. expect(labelFunction.calls.count()).toBe(2);
  562. expect(labelFunction.calls.argsFor(0)).toEqual([0, 0, 'checked', true]);
  563. expect(labelFunction.calls.argsFor(1)).toEqual([1, 0, 'checked', false]);
  564. expect(getCell(0, 0).querySelector('label').lastChild.textContent).toEqual('myLabel');
  565. });
  566. describe('CheckboxRenderer with ContextMenu', () => {
  567. it('should add class name `htRight` after set align in contextMenu', (done) => {
  568. handsontable({
  569. startRows: 1,
  570. startCols: 1,
  571. contextMenu: ['alignment'],
  572. cells() {
  573. return {
  574. type: 'checkbox'
  575. };
  576. },
  577. height: 100
  578. });
  579. selectCell(0, 0);
  580. contextMenu();
  581. var menu = $('.htContextMenu .ht_master .htCore').find('tbody td').not('.htSeparator');
  582. menu.simulate('mouseover');
  583. setTimeout(() => {
  584. var contextSubMenu = $(`.htContextMenuSub_${menu.text()}`).find('tbody td').eq(2);
  585. contextSubMenu.simulate('mousedown');
  586. contextSubMenu.simulate('mouseup');
  587. expect($('.handsontable.ht_master .htRight').length).toBe(1);
  588. done();
  589. }, 500);
  590. });
  591. });
  592. });