selectColumns.spec.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. describe('Core.selectColumns', () => {
  2. beforeEach(function() {
  3. this.$container = $('<div id="testContainer"></div>').appendTo('body');
  4. });
  5. afterEach(function() {
  6. if (this.$container) {
  7. destroy();
  8. this.$container.remove();
  9. }
  10. });
  11. it('should mark single column visually (default selectionMode, without headers)', () => {
  12. handsontable({
  13. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  14. colHeaders: false,
  15. rowHeaders: false,
  16. });
  17. selectColumns(2);
  18. expect(`
  19. | : : A : |
  20. | : : 0 : |
  21. | : : 0 : |
  22. | : : 0 : |
  23. | : : 0 : |
  24. | : : 0 : |
  25. `).toBeMatchToSelectionPattern();
  26. });
  27. it('should mark single column visually (default selectionMode)', () => {
  28. handsontable({
  29. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  30. colHeaders: true,
  31. rowHeaders: true,
  32. });
  33. selectColumns(2);
  34. expect(`
  35. | ║ : : * : |
  36. |===:===:===:===:===|
  37. | - ║ : : A : |
  38. | - ║ : : 0 : |
  39. | - ║ : : 0 : |
  40. | - ║ : : 0 : |
  41. | - ║ : : 0 : |
  42. | - ║ : : 0 : |
  43. `).toBeMatchToSelectionPattern();
  44. });
  45. it('should mark non-contiquous selection when CTRL key is pressed', () => {
  46. handsontable({
  47. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  48. colHeaders: true,
  49. rowHeaders: true,
  50. });
  51. selectColumns(2);
  52. keyDown('ctrl');
  53. selectColumns(0);
  54. expect(`
  55. | ║ * : : * : |
  56. |===:===:===:===:===|
  57. | - ║ A : : 0 : |
  58. | - ║ 0 : : 0 : |
  59. | - ║ 0 : : 0 : |
  60. | - ║ 0 : : 0 : |
  61. | - ║ 0 : : 0 : |
  62. | - ║ 0 : : 0 : |
  63. `).toBeMatchToSelectionPattern();
  64. });
  65. it('should mark single column visually (default selectionMode, fixedColumnsLeft enabled)', () => {
  66. handsontable({
  67. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  68. colHeaders: true,
  69. rowHeaders: true,
  70. fixedColumnsLeft: 2,
  71. });
  72. selectColumns(1, 2);
  73. expect(`
  74. | ║ : * | * : |
  75. |===:===:===:===:===|
  76. | - ║ : A | 0 : |
  77. | - ║ : 0 | 0 : |
  78. | - ║ : 0 | 0 : |
  79. | - ║ : 0 | 0 : |
  80. | - ║ : 0 | 0 : |
  81. | - ║ : 0 | 0 : |
  82. `).toBeMatchToSelectionPattern();
  83. });
  84. it('should mark single column visually (default selectionMode) using column property', () => {
  85. handsontable({
  86. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  87. colHeaders: true,
  88. rowHeaders: true,
  89. });
  90. selectColumns('prop2');
  91. expect(`
  92. | ║ : : * : |
  93. |===:===:===:===:===|
  94. | - ║ : : A : |
  95. | - ║ : : 0 : |
  96. | - ║ : : 0 : |
  97. | - ║ : : 0 : |
  98. | - ║ : : 0 : |
  99. | - ║ : : 0 : |
  100. `).toBeMatchToSelectionPattern();
  101. });
  102. it('should mark range of the columns visually (default selectionMode)', () => {
  103. handsontable({
  104. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  105. colHeaders: true,
  106. rowHeaders: true,
  107. });
  108. selectColumns(2, 3);
  109. expect(`
  110. | ║ : : * : * |
  111. |===:===:===:===:===|
  112. | - ║ : : A : 0 |
  113. | - ║ : : 0 : 0 |
  114. | - ║ : : 0 : 0 |
  115. | - ║ : : 0 : 0 |
  116. | - ║ : : 0 : 0 |
  117. | - ║ : : 0 : 0 |
  118. `).toBeMatchToSelectionPattern();
  119. });
  120. it('should mark range of the columns visually (default selectionMode, reversed selection)', () => {
  121. handsontable({
  122. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  123. colHeaders: true,
  124. rowHeaders: true,
  125. });
  126. selectColumns(3, 2);
  127. expect(`
  128. | ║ : : * : * |
  129. |===:===:===:===:===|
  130. | - ║ : : 0 : A |
  131. | - ║ : : 0 : 0 |
  132. | - ║ : : 0 : 0 |
  133. | - ║ : : 0 : 0 |
  134. | - ║ : : 0 : 0 |
  135. | - ║ : : 0 : 0 |
  136. `).toBeMatchToSelectionPattern();
  137. });
  138. it('should mark range of the columns visually (default selectionMode) using column property', () => {
  139. handsontable({
  140. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  141. colHeaders: true,
  142. rowHeaders: true,
  143. });
  144. selectColumns('prop2', 'prop3');
  145. expect(`
  146. | ║ : : * : * |
  147. |===:===:===:===:===|
  148. | - ║ : : A : 0 |
  149. | - ║ : : 0 : 0 |
  150. | - ║ : : 0 : 0 |
  151. | - ║ : : 0 : 0 |
  152. | - ║ : : 0 : 0 |
  153. | - ║ : : 0 : 0 |
  154. `).toBeMatchToSelectionPattern();
  155. });
  156. it('should mark range of the columns visually (default selectionMode, reversed selection) using column property', () => {
  157. handsontable({
  158. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  159. colHeaders: true,
  160. rowHeaders: true,
  161. });
  162. selectColumns('prop3', 'prop2');
  163. expect(`
  164. | ║ : : * : * |
  165. |===:===:===:===:===|
  166. | - ║ : : 0 : A |
  167. | - ║ : : 0 : 0 |
  168. | - ║ : : 0 : 0 |
  169. | - ║ : : 0 : 0 |
  170. | - ║ : : 0 : 0 |
  171. | - ║ : : 0 : 0 |
  172. `).toBeMatchToSelectionPattern();
  173. });
  174. it('should mark only single cell visually when selectionMode is set as `single', () => {
  175. handsontable({
  176. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  177. colHeaders: true,
  178. rowHeaders: true,
  179. selectionMode: 'single',
  180. });
  181. selectColumns(2);
  182. expect(`
  183. | ║ : : - : |
  184. |===:===:===:===:===|
  185. | - ║ : : # : |
  186. | ║ : : : |
  187. | ║ : : : |
  188. | ║ : : : |
  189. | ║ : : : |
  190. | ║ : : : |
  191. `).toBeMatchToSelectionPattern();
  192. });
  193. it('should mark the range of the columns visually when selectionMode is set as `range`', () => {
  194. handsontable({
  195. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  196. colHeaders: true,
  197. rowHeaders: true,
  198. selectionMode: 'range',
  199. });
  200. selectColumns(1, 2);
  201. expect(`
  202. | ║ : * : * : |
  203. |===:===:===:===:===|
  204. | - ║ : A : 0 : |
  205. | - ║ : 0 : 0 : |
  206. | - ║ : 0 : 0 : |
  207. | - ║ : 0 : 0 : |
  208. | - ║ : 0 : 0 : |
  209. | - ║ : 0 : 0 : |
  210. `).toBeMatchToSelectionPattern();
  211. });
  212. it('should not deselect current selection when selectColumns is called without arguments', () => {
  213. handsontable({
  214. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  215. });
  216. selectCell(1, 1); // Initial selection.
  217. expect(getSelected()).toEqual([[1, 1, 1, 1]]);
  218. selectColumns();
  219. expect(getSelected()).toEqual([[1, 1, 1, 1]]);
  220. });
  221. it('should not deselect current selection when selectColumns is called with negative values', () => {
  222. handsontable({
  223. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  224. });
  225. let wasSelected = selectCell(0, 0, 2, 2); // Initial selection.
  226. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  227. expect(wasSelected).toBe(true);
  228. wasSelected = selectColumns(0, -1);
  229. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  230. expect(wasSelected).toBe(false);
  231. wasSelected = selectColumns(-1, 0);
  232. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  233. expect(wasSelected).toBe(false);
  234. wasSelected = selectColumns(-3, -1);
  235. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  236. expect(wasSelected).toBe(false);
  237. wasSelected = selectColumns(-2);
  238. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  239. expect(wasSelected).toBe(false);
  240. });
  241. it('should not deselect current selection when selectColumns is called with coordinates beyond the table data range', () => {
  242. handsontable({
  243. data: Handsontable.helper.createSpreadsheetObjectData(3, 4),
  244. });
  245. let wasSelected = selectCell(0, 0, 2, 2); // Initial selection.
  246. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  247. expect(wasSelected).toBe(true);
  248. wasSelected = selectColumns(3, 4);
  249. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  250. expect(wasSelected).toBe(false);
  251. wasSelected = selectColumns(0, 4);
  252. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  253. expect(wasSelected).toBe(false);
  254. wasSelected = selectColumns(4);
  255. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  256. expect(wasSelected).toBe(false);
  257. wasSelected = selectColumns(200);
  258. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  259. expect(wasSelected).toBe(false);
  260. });
  261. it('should not deselect current selection when selectColumns is called with undefined column property', () => {
  262. handsontable({
  263. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  264. });
  265. let wasSelected = selectCell(0, 0, 2, 2); // Initial selection.
  266. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  267. expect(wasSelected).toBe(true);
  268. wasSelected = selectColumns(0, 'notExistProp');
  269. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  270. expect(wasSelected).toBe(false);
  271. wasSelected = selectColumns('notExistProp');
  272. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  273. expect(wasSelected).toBe(false);
  274. });
  275. it('should select only one column when two the same arguments are passed', () => {
  276. handsontable({
  277. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  278. });
  279. const wasSelected = selectColumns(1, 1);
  280. expect(getSelected()).toEqual([[0, 1, 5, 1]]);
  281. expect(wasSelected).toBe(true);
  282. });
  283. it('should select only one column when two the same arguments are passed (column property)', () => {
  284. handsontable({
  285. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  286. });
  287. const wasSelected = selectColumns(1, 'prop1');
  288. expect(getSelected()).toEqual([[0, 1, 5, 1]]);
  289. expect(wasSelected).toBe(true);
  290. });
  291. it('should select range of columns when the coordinates are passed in reversed order (from right to left)', () => {
  292. handsontable({
  293. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  294. });
  295. const wasSelected = selectColumns(2, 1);
  296. expect(getSelected()).toEqual([[0, 2, 5, 1]]);
  297. expect(wasSelected).toBe(true);
  298. });
  299. it('should select range of columns when the coordinates are passed in reversed order (from right to left using column property)', () => {
  300. handsontable({
  301. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  302. });
  303. const wasSelected = selectColumns('prop2', 'prop1');
  304. expect(getSelected()).toEqual([[0, 2, 5, 1]]);
  305. expect(wasSelected).toBe(true);
  306. });
  307. it('should not the scroll the viewport when column is selected', () => {
  308. const hot = handsontable({
  309. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  310. height: 300,
  311. width: 300,
  312. });
  313. selectCell(15, 1); // Scroll to the bottom of the Hot viewport.
  314. const scrollTop = hot.view.wt.wtTable.holder.scrollTop;
  315. selectColumns(1);
  316. expect(hot.view.wt.wtTable.holder.scrollTop).toBe(scrollTop);
  317. });
  318. it('should fire hooks with proper context', () => {
  319. const {
  320. afterSelection,
  321. afterSelectionByProp,
  322. afterSelectionEnd,
  323. afterSelectionEndByProp,
  324. beforeSetRangeStart,
  325. beforeSetRangeStartOnly,
  326. beforeSetRangeEnd,
  327. } = jasmine.createSpyObj('hooks', [
  328. 'afterSelection',
  329. 'afterSelectionByProp',
  330. 'afterSelectionEnd',
  331. 'afterSelectionEndByProp',
  332. 'beforeSetRangeStart',
  333. 'beforeSetRangeStartOnly',
  334. 'beforeSetRangeEnd',
  335. ]);
  336. const hot = handsontable({
  337. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  338. height: 300,
  339. width: 300,
  340. afterSelection,
  341. afterSelectionByProp,
  342. afterSelectionEnd,
  343. afterSelectionEndByProp,
  344. beforeSetRangeStart,
  345. beforeSetRangeStartOnly,
  346. beforeSetRangeEnd,
  347. });
  348. selectColumns(1, 2);
  349. expect(afterSelection.calls.first().object).toBe(hot);
  350. expect(afterSelectionByProp.calls.first().object).toBe(hot);
  351. expect(afterSelectionEnd.calls.first().object).toBe(hot);
  352. expect(afterSelectionEndByProp.calls.first().object).toBe(hot);
  353. expect(beforeSetRangeStartOnly.calls.first().object).toBe(hot);
  354. });
  355. it('should fire hooks with proper arguments when a single column is selected', () => {
  356. const {
  357. afterSelection,
  358. afterSelectionByProp,
  359. afterSelectionEnd,
  360. afterSelectionEndByProp,
  361. beforeSetRangeStart,
  362. beforeSetRangeStartOnly,
  363. beforeSetRangeEnd,
  364. } = jasmine.createSpyObj('hooks', [
  365. 'afterSelection',
  366. 'afterSelectionByProp',
  367. 'afterSelectionEnd',
  368. 'afterSelectionEndByProp',
  369. 'beforeSetRangeStart',
  370. 'beforeSetRangeStartOnly',
  371. 'beforeSetRangeEnd',
  372. ]);
  373. handsontable({
  374. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  375. height: 300,
  376. width: 300,
  377. afterSelection,
  378. afterSelectionByProp,
  379. afterSelectionEnd,
  380. afterSelectionEndByProp,
  381. beforeSetRangeStart,
  382. beforeSetRangeStartOnly,
  383. beforeSetRangeEnd,
  384. });
  385. selectColumns(1);
  386. expect(afterSelection.calls.count()).toBe(1);
  387. expect(afterSelection.calls.argsFor(0)).toEqual([0, 1, 19, 1, jasmine.any(Object), 0]);
  388. expect(afterSelectionByProp.calls.count()).toBe(1);
  389. expect(afterSelectionByProp.calls.argsFor(0)).toEqual([0, 'prop1', 19, 'prop1', jasmine.any(Object), 0]);
  390. expect(afterSelectionEnd.calls.count()).toBe(1);
  391. expect(afterSelectionEnd.calls.argsFor(0)).toEqual([0, 1, 19, 1, 0, void 0]);
  392. expect(afterSelectionEndByProp.calls.count()).toBe(1);
  393. expect(afterSelectionEndByProp.calls.argsFor(0)).toEqual([0, 'prop1', 19, 'prop1', 0, void 0]);
  394. expect(beforeSetRangeStart.calls.count()).toBe(0);
  395. expect(beforeSetRangeStartOnly.calls.count()).toBe(1);
  396. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].row).toBe(0);
  397. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].col).toBe(1);
  398. });
  399. it('should fire hooks with proper arguments when range of the columns are selected', () => {
  400. const {
  401. afterSelection,
  402. afterSelectionByProp,
  403. afterSelectionEnd,
  404. afterSelectionEndByProp,
  405. beforeSetRangeStart,
  406. beforeSetRangeStartOnly,
  407. beforeSetRangeEnd,
  408. } = jasmine.createSpyObj('hooks', [
  409. 'afterSelection',
  410. 'afterSelectionByProp',
  411. 'afterSelectionEnd',
  412. 'afterSelectionEndByProp',
  413. 'beforeSetRangeStart',
  414. 'beforeSetRangeStartOnly',
  415. 'beforeSetRangeEnd',
  416. ]);
  417. handsontable({
  418. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  419. height: 300,
  420. width: 300,
  421. afterSelection,
  422. afterSelectionByProp,
  423. afterSelectionEnd,
  424. afterSelectionEndByProp,
  425. beforeSetRangeStart,
  426. beforeSetRangeStartOnly,
  427. beforeSetRangeEnd,
  428. });
  429. selectColumns(1, 2);
  430. expect(afterSelection.calls.count()).toBe(1);
  431. expect(afterSelection.calls.argsFor(0)).toEqual([0, 1, 19, 2, jasmine.any(Object), 0]);
  432. expect(afterSelectionByProp.calls.count()).toBe(1);
  433. expect(afterSelectionByProp.calls.argsFor(0)).toEqual([0, 'prop1', 19, 'prop2', jasmine.any(Object), 0]);
  434. expect(afterSelectionEnd.calls.count()).toBe(1);
  435. expect(afterSelectionEnd.calls.argsFor(0)).toEqual([0, 1, 19, 2, 0, void 0]);
  436. expect(afterSelectionEndByProp.calls.count()).toBe(1);
  437. expect(afterSelectionEndByProp.calls.argsFor(0)).toEqual([0, 'prop1', 19, 'prop2', 0, void 0]);
  438. expect(beforeSetRangeStart.calls.count()).toBe(0);
  439. expect(beforeSetRangeStartOnly.calls.count()).toBe(1);
  440. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].row).toBe(0);
  441. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].col).toBe(1);
  442. });
  443. });