selectRows.spec.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. describe('Core.selectRows', () => {
  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 row visually (default selectionMode, without headers)', () => {
  12. handsontable({
  13. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  14. });
  15. selectRows(2);
  16. expect(`
  17. | : : : |
  18. | : : : |
  19. | A : 0 : 0 : 0 |
  20. | : : : |
  21. | : : : |
  22. | : : : |
  23. `).toBeMatchToSelectionPattern();
  24. });
  25. it('should mark single row visually (default selectionMode)', () => {
  26. handsontable({
  27. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  28. colHeaders: true,
  29. rowHeaders: true,
  30. });
  31. selectRows(2);
  32. expect(`
  33. | ║ - : - : - : - |
  34. |===:===:===:===:===|
  35. | ║ : : : |
  36. | ║ : : : |
  37. | * ║ A : 0 : 0 : 0 |
  38. | ║ : : : |
  39. | ║ : : : |
  40. | ║ : : : |
  41. `).toBeMatchToSelectionPattern();
  42. });
  43. it('should mark non-contiquous selection when CTRL key is pressed', () => {
  44. handsontable({
  45. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  46. colHeaders: true,
  47. rowHeaders: true,
  48. });
  49. selectRows(2);
  50. keyDown('ctrl');
  51. selectRows(0);
  52. expect(`
  53. | ║ - : - : - : - |
  54. |===:===:===:===:===|
  55. | * ║ A : 0 : 0 : 0 |
  56. | ║ : : : |
  57. | * ║ 0 : 0 : 0 : 0 |
  58. | ║ : : : |
  59. | ║ : : : |
  60. | ║ : : : |
  61. `).toBeMatchToSelectionPattern();
  62. });
  63. it('should mark range of the rows visually (default selectionMode)', () => {
  64. handsontable({
  65. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  66. colHeaders: true,
  67. rowHeaders: true,
  68. });
  69. selectRows(2, 3);
  70. expect(`
  71. | ║ - : - : - : - |
  72. |===:===:===:===:===|
  73. | ║ : : : |
  74. | ║ : : : |
  75. | * ║ A : 0 : 0 : 0 |
  76. | * ║ 0 : 0 : 0 : 0 |
  77. | ║ : : : |
  78. | ║ : : : |
  79. `).toBeMatchToSelectionPattern();
  80. });
  81. it('should mark range of the rows visually (default selectionMode, reversed selection)', () => {
  82. handsontable({
  83. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  84. colHeaders: true,
  85. rowHeaders: true,
  86. });
  87. selectRows(3, 2);
  88. expect(`
  89. | ║ - : - : - : - |
  90. |===:===:===:===:===|
  91. | ║ : : : |
  92. | ║ : : : |
  93. | * ║ 0 : 0 : 0 : 0 |
  94. | * ║ A : 0 : 0 : 0 |
  95. | ║ : : : |
  96. | ║ : : : |
  97. `).toBeMatchToSelectionPattern();
  98. });
  99. it('should mark only single cell visually when selectionMode is set as `single', () => {
  100. handsontable({
  101. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  102. colHeaders: true,
  103. rowHeaders: true,
  104. selectionMode: 'single',
  105. });
  106. selectRows(2);
  107. expect(`
  108. | ║ - : : : |
  109. |===:===:===:===:===|
  110. | ║ : : : |
  111. | ║ : : : |
  112. | - ║ # : : : |
  113. | ║ : : : |
  114. | ║ : : : |
  115. | ║ : : : |
  116. `).toBeMatchToSelectionPattern();
  117. });
  118. it('should mark the range of the rows visually when selectionMode is set as `range`', () => {
  119. handsontable({
  120. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  121. colHeaders: true,
  122. rowHeaders: true,
  123. selectionMode: 'range',
  124. });
  125. selectRows(1, 2);
  126. expect(`
  127. | ║ - : - : - : - |
  128. |===:===:===:===:===|
  129. | ║ : : : |
  130. | * ║ A : 0 : 0 : 0 |
  131. | * ║ 0 : 0 : 0 : 0 |
  132. | ║ : : : |
  133. | ║ : : : |
  134. | ║ : : : |
  135. `).toBeMatchToSelectionPattern();
  136. });
  137. it('should not deselect current selection when selectRows is called without arguments', () => {
  138. handsontable({
  139. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  140. });
  141. selectCell(1, 1); // Initial selection.
  142. expect(getSelected()).toEqual([[1, 1, 1, 1]]);
  143. selectRows();
  144. expect(getSelected()).toEqual([[1, 1, 1, 1]]);
  145. });
  146. it('should not deselect current selection when selectRows is called with negative values', () => {
  147. handsontable({
  148. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  149. });
  150. let wasSelected = selectCell(0, 0, 2, 2); // Initial selection.
  151. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  152. expect(wasSelected).toBe(true);
  153. wasSelected = selectRows(0, -1);
  154. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  155. expect(wasSelected).toBe(false);
  156. wasSelected = selectRows(-1, 0);
  157. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  158. expect(wasSelected).toBe(false);
  159. wasSelected = selectRows(-3, -1);
  160. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  161. expect(wasSelected).toBe(false);
  162. wasSelected = selectRows(-2);
  163. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  164. expect(wasSelected).toBe(false);
  165. });
  166. it('should not deselect current selection when selectRows is called with coordinates beyond the table data range', () => {
  167. handsontable({
  168. data: Handsontable.helper.createSpreadsheetObjectData(3, 4),
  169. });
  170. let wasSelected = selectCell(0, 0, 2, 2); // Initial selection.
  171. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  172. expect(wasSelected).toBe(true);
  173. wasSelected = selectRows(3, 4);
  174. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  175. expect(wasSelected).toBe(false);
  176. wasSelected = selectRows(0, 4);
  177. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  178. expect(wasSelected).toBe(false);
  179. wasSelected = selectRows(4);
  180. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  181. expect(wasSelected).toBe(false);
  182. wasSelected = selectRows(200);
  183. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  184. expect(wasSelected).toBe(false);
  185. });
  186. it('should not deselect current selection when selectRows is called with undefined column property', () => {
  187. handsontable({
  188. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  189. });
  190. let wasSelected = selectCell(0, 0, 2, 2); // Initial selection.
  191. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  192. expect(wasSelected).toBe(true);
  193. wasSelected = selectRows(0, 'notExistProp');
  194. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  195. expect(wasSelected).toBe(false);
  196. wasSelected = selectRows('notExistProp');
  197. expect(getSelected()).toEqual([[0, 0, 2, 2]]);
  198. expect(wasSelected).toBe(false);
  199. });
  200. it('should select only one row when two the same arguments are passed', () => {
  201. handsontable({
  202. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  203. });
  204. const wasSelected = selectRows(1, 1);
  205. expect(getSelected()).toEqual([[1, 0, 1, 3]]);
  206. expect(wasSelected).toBe(true);
  207. });
  208. it('should select range of rows when the coordinates are passed in reversed order (from right to left)', () => {
  209. handsontable({
  210. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  211. });
  212. const wasSelected = selectRows(2, 1);
  213. expect(getSelected()).toEqual([[2, 0, 1, 3]]);
  214. expect(wasSelected).toBe(true);
  215. });
  216. it('should not the scroll the viewport when row is selected', () => {
  217. const hot = handsontable({
  218. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  219. height: 300,
  220. width: 300,
  221. });
  222. selectCell(1, 15); // Scroll to the bottom of the Hot viewport.
  223. const scrollTop = hot.view.wt.wtTable.holder.scrollTop;
  224. selectRows(1);
  225. expect(hot.view.wt.wtTable.holder.scrollTop).toBe(scrollTop);
  226. });
  227. it('should fire hooks with proper context', () => {
  228. const {
  229. afterSelection,
  230. afterSelectionByProp,
  231. afterSelectionEnd,
  232. afterSelectionEndByProp,
  233. beforeSetRangeStart,
  234. beforeSetRangeStartOnly,
  235. beforeSetRangeEnd,
  236. } = jasmine.createSpyObj('hooks', [
  237. 'afterSelection',
  238. 'afterSelectionByProp',
  239. 'afterSelectionEnd',
  240. 'afterSelectionEndByProp',
  241. 'beforeSetRangeStart',
  242. 'beforeSetRangeStartOnly',
  243. 'beforeSetRangeEnd',
  244. ]);
  245. const hot = handsontable({
  246. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  247. height: 300,
  248. width: 300,
  249. afterSelection,
  250. afterSelectionByProp,
  251. afterSelectionEnd,
  252. afterSelectionEndByProp,
  253. beforeSetRangeStart,
  254. beforeSetRangeStartOnly,
  255. beforeSetRangeEnd,
  256. });
  257. selectRows(1, 2);
  258. expect(afterSelection.calls.first().object).toBe(hot);
  259. expect(afterSelectionByProp.calls.first().object).toBe(hot);
  260. expect(afterSelectionEnd.calls.first().object).toBe(hot);
  261. expect(afterSelectionEndByProp.calls.first().object).toBe(hot);
  262. expect(beforeSetRangeStartOnly.calls.first().object).toBe(hot);
  263. });
  264. it('should fire hooks with proper arguments when a single row is selected', () => {
  265. const {
  266. afterSelection,
  267. afterSelectionByProp,
  268. afterSelectionEnd,
  269. afterSelectionEndByProp,
  270. beforeSetRangeStart,
  271. beforeSetRangeStartOnly,
  272. beforeSetRangeEnd,
  273. } = jasmine.createSpyObj('hooks', [
  274. 'afterSelection',
  275. 'afterSelectionByProp',
  276. 'afterSelectionEnd',
  277. 'afterSelectionEndByProp',
  278. 'beforeSetRangeStart',
  279. 'beforeSetRangeStartOnly',
  280. 'beforeSetRangeEnd',
  281. ]);
  282. handsontable({
  283. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  284. height: 300,
  285. width: 300,
  286. afterSelection,
  287. afterSelectionByProp,
  288. afterSelectionEnd,
  289. afterSelectionEndByProp,
  290. beforeSetRangeStart,
  291. beforeSetRangeStartOnly,
  292. beforeSetRangeEnd,
  293. });
  294. selectRows(1);
  295. expect(afterSelection.calls.count()).toBe(1);
  296. expect(afterSelection.calls.argsFor(0)).toEqual([1, 0, 1, 19, jasmine.any(Object), 0]);
  297. expect(afterSelectionByProp.calls.count()).toBe(1);
  298. expect(afterSelectionByProp.calls.argsFor(0)).toEqual([1, 'prop0', 1, 'prop19', jasmine.any(Object), 0]);
  299. expect(afterSelectionEnd.calls.count()).toBe(1);
  300. expect(afterSelectionEnd.calls.argsFor(0)).toEqual([1, 0, 1, 19, 0, void 0]);
  301. expect(afterSelectionEndByProp.calls.count()).toBe(1);
  302. expect(afterSelectionEndByProp.calls.argsFor(0)).toEqual([1, 'prop0', 1, 'prop19', 0, void 0]);
  303. expect(beforeSetRangeStart.calls.count()).toBe(0);
  304. expect(beforeSetRangeStartOnly.calls.count()).toBe(1);
  305. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].row).toBe(1);
  306. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].col).toBe(0);
  307. });
  308. it('should fire hooks with proper arguments when range of the columns are selected', () => {
  309. const {
  310. afterSelection,
  311. afterSelectionByProp,
  312. afterSelectionEnd,
  313. afterSelectionEndByProp,
  314. beforeSetRangeStart,
  315. beforeSetRangeStartOnly,
  316. beforeSetRangeEnd,
  317. } = jasmine.createSpyObj('hooks', [
  318. 'afterSelection',
  319. 'afterSelectionByProp',
  320. 'afterSelectionEnd',
  321. 'afterSelectionEndByProp',
  322. 'beforeSetRangeStart',
  323. 'beforeSetRangeStartOnly',
  324. 'beforeSetRangeEnd',
  325. ]);
  326. handsontable({
  327. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  328. height: 300,
  329. width: 300,
  330. afterSelection,
  331. afterSelectionByProp,
  332. afterSelectionEnd,
  333. afterSelectionEndByProp,
  334. beforeSetRangeStart,
  335. beforeSetRangeStartOnly,
  336. beforeSetRangeEnd,
  337. });
  338. selectRows(1, 2);
  339. expect(afterSelection.calls.count()).toBe(1);
  340. expect(afterSelection.calls.argsFor(0)).toEqual([1, 0, 2, 19, jasmine.any(Object), 0]);
  341. expect(afterSelectionByProp.calls.count()).toBe(1);
  342. expect(afterSelectionByProp.calls.argsFor(0)).toEqual([1, 'prop0', 2, 'prop19', jasmine.any(Object), 0]);
  343. expect(afterSelectionEnd.calls.count()).toBe(1);
  344. expect(afterSelectionEnd.calls.argsFor(0)).toEqual([1, 0, 2, 19, 0, void 0]);
  345. expect(afterSelectionEndByProp.calls.count()).toBe(1);
  346. expect(afterSelectionEndByProp.calls.argsFor(0)).toEqual([1, 'prop0', 2, 'prop19', 0, void 0]);
  347. expect(beforeSetRangeStart.calls.count()).toBe(0);
  348. expect(beforeSetRangeStartOnly.calls.count()).toBe(1);
  349. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].row).toBe(1);
  350. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].col).toBe(0);
  351. });
  352. });