selectCells.spec.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. describe('Core.selectCells', () => {
  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 cell visually (default selectionMode, without headers)', () => {
  12. handsontable({
  13. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  14. colHeaders: false,
  15. rowHeaders: false,
  16. });
  17. selectCells([[2, 2]]);
  18. expect(`
  19. | : : : |
  20. | : : : |
  21. | : : # : |
  22. | : : : |
  23. | : : : |
  24. | : : : |
  25. `).toBeMatchToSelectionPattern();
  26. });
  27. it('should mark single cell visually (default selectionMode)', () => {
  28. handsontable({
  29. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  30. colHeaders: true,
  31. rowHeaders: true,
  32. });
  33. selectCells([[2, 2]]);
  34. expect(`
  35. | ║ : : - : |
  36. |===:===:===:===:===|
  37. | ║ : : : |
  38. | ║ : : : |
  39. | - ║ : : # : |
  40. | ║ : : : |
  41. | ║ : : : |
  42. | ║ : : : |
  43. `).toBeMatchToSelectionPattern();
  44. });
  45. it('should mark range of the cells visually (default selectionMode)', () => {
  46. handsontable({
  47. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  48. colHeaders: true,
  49. rowHeaders: true,
  50. });
  51. selectCells([[1, 2, 2, 3]]);
  52. expect(`
  53. | ║ : : - : - |
  54. |===:===:===:===:===|
  55. | ║ : : : |
  56. | - ║ : : A : 0 |
  57. | - ║ : : 0 : 0 |
  58. | ║ : : : |
  59. | ║ : : : |
  60. | ║ : : : |
  61. `).toBeMatchToSelectionPattern();
  62. });
  63. it('should mark non-contiguous cells visually (default selectionMode)', () => {
  64. handsontable({
  65. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  66. colHeaders: true,
  67. rowHeaders: true,
  68. });
  69. selectCells([
  70. [0, 0],
  71. [5, 1, 2, 2],
  72. [4, 3, 1, 2],
  73. [3, 0, 3, 2],
  74. [4, 2],
  75. [4, 2]
  76. ]);
  77. expect(`
  78. | ║ - : - : - : - |
  79. |===:===:===:===:===|
  80. | - ║ 0 : : : |
  81. | - ║ : : 0 : 0 |
  82. | - ║ : 0 : 1 : 0 |
  83. | - ║ 0 : 1 : 2 : 0 |
  84. | - ║ : 0 : D : 0 |
  85. | - ║ : 0 : 0 : |
  86. `).toBeMatchToSelectionPattern();
  87. });
  88. it('should mark single cell visually when selectionMode is set as `single', () => {
  89. handsontable({
  90. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  91. selectionMode: 'single',
  92. colHeaders: true,
  93. rowHeaders: true,
  94. });
  95. selectCells([[2, 2]]);
  96. expect(`
  97. | ║ : : - : |
  98. |===:===:===:===:===|
  99. | ║ : : : |
  100. | ║ : : : |
  101. | - ║ : : # : |
  102. | ║ : : : |
  103. | ║ : : : |
  104. | ║ : : : |
  105. `).toBeMatchToSelectionPattern();
  106. });
  107. it('should not mark range of the cells visually when selectionMode is set as `single', () => {
  108. handsontable({
  109. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  110. selectionMode: 'single',
  111. colHeaders: true,
  112. rowHeaders: true,
  113. });
  114. selectCells([[1, 2, 2, 3]]);
  115. expect(`
  116. | ║ : : - : |
  117. |===:===:===:===:===|
  118. | ║ : : : |
  119. | - ║ : : # : |
  120. | ║ : : : |
  121. | ║ : : : |
  122. | ║ : : : |
  123. | ║ : : : |
  124. `).toBeMatchToSelectionPattern();
  125. });
  126. it('should not mark non-contiguous cells visually when selectionMode is set as `single', () => {
  127. handsontable({
  128. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  129. selectionMode: 'single',
  130. colHeaders: true,
  131. rowHeaders: true,
  132. });
  133. selectCells([
  134. [0, 0],
  135. [5, 1, 2, 2],
  136. [4, 3, 1, 2],
  137. [3, 0, 3, 2],
  138. [4, 2],
  139. [4, 2]
  140. ]);
  141. expect(`
  142. | ║ : : - : |
  143. |===:===:===:===:===|
  144. | ║ : : : |
  145. | ║ : : : |
  146. | ║ : : : |
  147. | ║ : : : |
  148. | - ║ : : # : |
  149. | ║ : : : |
  150. `).toBeMatchToSelectionPattern();
  151. });
  152. it('should mark single cell visually when selectionMode is set as `range', () => {
  153. handsontable({
  154. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  155. selectionMode: 'range',
  156. colHeaders: true,
  157. rowHeaders: true,
  158. });
  159. selectCells([[2, 2]]);
  160. expect(`
  161. | ║ : : - : |
  162. |===:===:===:===:===|
  163. | ║ : : : |
  164. | ║ : : : |
  165. | - ║ : : # : |
  166. | ║ : : : |
  167. | ║ : : : |
  168. | ║ : : : |
  169. `).toBeMatchToSelectionPattern();
  170. });
  171. it('should mark range of the cells visually when selectionMode is set as `range', () => {
  172. handsontable({
  173. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  174. selectionMode: 'range',
  175. colHeaders: true,
  176. rowHeaders: true,
  177. });
  178. selectCells([[1, 2, 2, 3]]);
  179. expect(`
  180. | ║ : : - : - |
  181. |===:===:===:===:===|
  182. | ║ : : : |
  183. | - ║ : : A : 0 |
  184. | - ║ : : 0 : 0 |
  185. | ║ : : : |
  186. | ║ : : : |
  187. | ║ : : : |
  188. `).toBeMatchToSelectionPattern();
  189. });
  190. it('should not mark non-contiguous cells visually when selectionMode is set as `range', () => {
  191. handsontable({
  192. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  193. selectionMode: 'range',
  194. });
  195. selectCells([
  196. [0, 0],
  197. [5, 1, 2, 2],
  198. [4, 3, 1, 2],
  199. [3, 0, 3, 2],
  200. [4, 2],
  201. [4, 2]
  202. ]);
  203. expect(`
  204. | : : : |
  205. | : : : |
  206. | : : : |
  207. | : : : |
  208. | : : # : |
  209. | : : : |
  210. `).toBeMatchToSelectionPattern();
  211. });
  212. it('should mark the headers when whole column and row is selected', () => {
  213. handsontable({
  214. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  215. colHeaders: true,
  216. rowHeaders: true,
  217. });
  218. selectCells([[0, 1, 5, 1], [0, 3, 5, 3]]);
  219. expect(`
  220. | ║ : - : : - |
  221. |===:===:===:===:===|
  222. | - ║ : 0 : : A |
  223. | - ║ : 0 : : 0 |
  224. | - ║ : 0 : : 0 |
  225. | - ║ : 0 : : 0 |
  226. | - ║ : 0 : : 0 |
  227. | - ║ : 0 : : 0 |
  228. `).toBeMatchToSelectionPattern();
  229. selectCells([[1, 0, 1, 3], [3, 0, 3, 3]]);
  230. expect(`
  231. | ║ - : - : - : - |
  232. |===:===:===:===:===|
  233. | ║ : : : |
  234. | - ║ 0 : 0 : 0 : 0 |
  235. | ║ : : : |
  236. | - ║ A : 0 : 0 : 0 |
  237. | ║ : : : |
  238. | ║ : : : |
  239. `).toBeMatchToSelectionPattern();
  240. });
  241. it('should not deselect current selection when sellectCells is called without arguments', () => {
  242. handsontable({
  243. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  244. });
  245. selectCells([[0, 0, 2, 2], [1, 1, 3, 3]]); // Initial selection.
  246. selectCells();
  247. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  248. });
  249. it('should throw an exception when the coordinates are passed in invalid format', () => {
  250. handsontable({
  251. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  252. });
  253. selectCells([[0, 0, 2, 2], [1, 1, 3, 3]]); // Initial selection.
  254. expect(() => selectCells(1)).toThrow();
  255. expect(() => selectCells([1])).toThrow();
  256. expect(() => selectCells('prop0')).toThrow();
  257. expect(() => selectCells(['prop0'])).toThrow();
  258. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  259. });
  260. it('should not deselect current selection when sellectCells is called with one argument', () => {
  261. handsontable({
  262. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  263. });
  264. const wasSelected = selectCells([[0, 0, 2, 2], [1, 1, 3, 3]]); // Initial selection.
  265. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  266. expect(wasSelected).toBe(true);
  267. /* eslint-disable no-empty */
  268. try {
  269. selectCells([[1]]);
  270. } catch (ex) {}
  271. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  272. });
  273. it('should not deselect current selection when sellectCells is called with negative values', () => {
  274. handsontable({
  275. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  276. });
  277. let wasSelected = selectCells([[0, 0, 2, 2], [1, 1, 3, 3]]); // Initial selection.
  278. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  279. expect(wasSelected).toBe(true);
  280. wasSelected = selectCells([[0, -1, 0, 0]]);
  281. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  282. expect(wasSelected).toBe(false);
  283. wasSelected = selectCells([[-1, 0, 0, 0]]);
  284. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  285. expect(wasSelected).toBe(false);
  286. wasSelected = selectCells([[0, 0, -1, 0]]);
  287. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  288. expect(wasSelected).toBe(false);
  289. wasSelected = selectCells([[0, 0, 0, -1]]);
  290. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  291. expect(wasSelected).toBe(false);
  292. wasSelected = selectCells([[0, 0, 0, 0], [0, 0, 0, -1]]);
  293. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  294. expect(wasSelected).toBe(false);
  295. });
  296. it('should not deselect current selection when sellectCells is called with coordinates beyond the table data range', () => {
  297. handsontable({
  298. data: Handsontable.helper.createSpreadsheetObjectData(3, 4),
  299. });
  300. let wasSelected = selectCells([[0, 0, 2, 2], [1, 1, 2, 2]]); // Initial selection.
  301. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 2, 2]]);
  302. expect(wasSelected).toBe(true);
  303. wasSelected = selectCells([[3, 0]]);
  304. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 2, 2]]);
  305. expect(wasSelected).toBe(false);
  306. wasSelected = selectCells([[0, 4]]);
  307. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 2, 2]]);
  308. expect(wasSelected).toBe(false);
  309. wasSelected = selectCells([[0, 0, 3, 0]]);
  310. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 2, 2]]);
  311. expect(wasSelected).toBe(false);
  312. wasSelected = selectCells([[0, 0, 0, 4]]);
  313. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 2, 2]]);
  314. expect(wasSelected).toBe(false);
  315. wasSelected = selectCells([[0, 0], [0, 0, 0, 4]]);
  316. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 2, 2]]);
  317. expect(wasSelected).toBe(false);
  318. });
  319. it('should not deselect current selection when sellectCells is called with undefined column property', () => {
  320. handsontable({
  321. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  322. });
  323. let wasSelected = selectCells([[0, 0, 2, 2], [1, 1, 3, 3]]); // Initial selection.
  324. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  325. expect(wasSelected).toBe(true);
  326. wasSelected = selectCells([[0, 'notExistProp']]);
  327. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  328. expect(wasSelected).toBe(false);
  329. wasSelected = selectCells([[0, 0, 0, 'notExistProp']]);
  330. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  331. expect(wasSelected).toBe(false);
  332. wasSelected = selectCells([[1, 1], [0, 0, 0, 'notExistProp']]);
  333. expect(getSelected()).toEqual([[0, 0, 2, 2], [1, 1, 3, 3]]);
  334. expect(wasSelected).toBe(false);
  335. });
  336. it('should select only one cell when two arguments are passed', () => {
  337. handsontable({
  338. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  339. });
  340. const wasSelected = selectCells([[1, 1]]);
  341. expect(getSelected()).toEqual([[1, 1, 1, 1]]);
  342. expect(wasSelected).toBe(true);
  343. });
  344. it('should select only one cell when two arguments are passed (column property)', () => {
  345. handsontable({
  346. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  347. });
  348. const wasSelected = selectCells([[1, 'prop1']]);
  349. expect(getSelected()).toEqual([[1, 1, 1, 1]]);
  350. expect(wasSelected).toBe(true);
  351. });
  352. it('should select range of cells when at least the three arguments are passed', () => {
  353. handsontable({
  354. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  355. });
  356. const wasSelected = selectCells([[0, 0, 1]]);
  357. expect(getSelected()).toEqual([[0, 0, 1, 0]]);
  358. expect(wasSelected).toBe(true);
  359. });
  360. it('should select range of cells when at least the three arguments are passed (column property)', () => {
  361. handsontable({
  362. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  363. });
  364. const wasSelected = selectCells([[0, 'prop0', 1]]);
  365. expect(getSelected()).toEqual([[0, 0, 1, 0]]);
  366. expect(wasSelected).toBe(true);
  367. });
  368. it('should select range of cells when the four arguments are passed', () => {
  369. handsontable({
  370. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  371. });
  372. const wasSelected = selectCells([[1, 1, 2, 3]]);
  373. expect(getSelected()).toEqual([[1, 1, 2, 3]]);
  374. expect(wasSelected).toBe(true);
  375. });
  376. it('should select range of cells when the four arguments are passed (column property)', () => {
  377. handsontable({
  378. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  379. });
  380. const wasSelected = selectCells([[1, 'prop1', 2, 'prop3']]);
  381. expect(getSelected()).toEqual([[1, 1, 2, 3]]);
  382. expect(wasSelected).toBe(true);
  383. });
  384. it('should select multiple cells when the multiple ranges are passed', () => {
  385. handsontable({
  386. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  387. });
  388. const wasSelected = selectCells([
  389. [0, 0],
  390. [5, 1, 2, 2],
  391. [4, 3, 1, 2],
  392. [3, 0, 3, 2],
  393. [4, 2],
  394. [4, 2]
  395. ]);
  396. expect(getSelected()).toEqual([[0, 0, 0, 0], [5, 1, 2, 2], [4, 3, 1, 2], [3, 0, 3, 2], [4, 2, 4, 2], [4, 2, 4, 2]]);
  397. expect(wasSelected).toBe(true);
  398. });
  399. it('should select multiple cells when the multiple ranges are passed (column property)', () => {
  400. handsontable({
  401. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  402. });
  403. const wasSelected = selectCells([
  404. [0, 'prop0'],
  405. [5, 'prop1', 2, 'prop2'],
  406. [4, 'prop3', 1, 'prop2'],
  407. [3, 'prop0', 3, 'prop2'],
  408. [4, 'prop2'],
  409. [4, 'prop2']
  410. ]);
  411. expect(getSelected()).toEqual([[0, 0, 0, 0], [5, 1, 2, 2], [4, 3, 1, 2], [3, 0, 3, 2], [4, 2, 4, 2], [4, 2, 4, 2]]);
  412. expect(wasSelected).toBe(true);
  413. });
  414. it('should select range of cells when the coordinates are passed in reversed order (from right-bottom to left-top)', () => {
  415. handsontable({
  416. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  417. });
  418. const wasSelected = selectCells([[2, 3, 1, 1]]);
  419. expect(getSelected()).toEqual([[2, 3, 1, 1]]);
  420. expect(wasSelected).toBe(true);
  421. });
  422. it('should select range of cells when the coordinates are passed in reversed order (from right-bottom to left-top using column property)', () => {
  423. handsontable({
  424. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  425. });
  426. const wasSelected = selectCells([[2, 'prop3', 1, 'prop1']]);
  427. expect(getSelected()).toEqual([[2, 3, 1, 1]]);
  428. expect(wasSelected).toBe(true);
  429. });
  430. it('should select range of cells when the coordinates are passed in reversed order (from left-bottom to right-top)', () => {
  431. handsontable({
  432. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  433. });
  434. const wasSelected = selectCells([[2, 1, 1, 3]]);
  435. expect(getSelected()).toEqual([[2, 1, 1, 3]]);
  436. expect(wasSelected).toBe(true);
  437. });
  438. it('should select range of cells when the coordinates are passed in reversed order (from left-bottom to right-top using column property)', () => {
  439. handsontable({
  440. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  441. });
  442. const wasSelected = selectCells([[2, 'prop1', 1, 'prop3']]);
  443. expect(getSelected()).toEqual([[2, 1, 1, 3]]);
  444. expect(wasSelected).toBe(true);
  445. });
  446. it('should select range of cells when the coordinates are passed in reversed order (from right-top to left-bottom)', () => {
  447. handsontable({
  448. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  449. });
  450. const wasSelected = selectCells([[1, 3, 2, 1]]);
  451. expect(getSelected()).toEqual([[1, 3, 2, 1]]);
  452. expect(wasSelected).toBe(true);
  453. });
  454. it('should select range of cells when the coordinates are passed in reversed order (from right-top to left-bottom using column property)', () => {
  455. handsontable({
  456. data: Handsontable.helper.createSpreadsheetObjectData(6, 4),
  457. });
  458. const wasSelected = selectCells([[1, 'prop3', 2, 'prop1']]);
  459. expect(getSelected()).toEqual([[1, 3, 2, 1]]);
  460. expect(wasSelected).toBe(true);
  461. });
  462. it('should by default scroll the viewport to the selected cell (bottom of the viewport)', () => {
  463. handsontable({
  464. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  465. height: 300,
  466. width: 300,
  467. });
  468. selectCells([[15, 0]]);
  469. expect(getCell(15, 0)).toBeVisibleAtBottomOfViewport();
  470. });
  471. it('should by default scroll the viewport to the selected cell using column props (bottom of the viewport)', () => {
  472. handsontable({
  473. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  474. height: 300,
  475. width: 300,
  476. });
  477. selectCells([[15, 'prop0']]);
  478. expect(getCell(15, 0)).toBeVisibleAtBottomOfViewport();
  479. });
  480. it('should by default scroll the viewport to the selected cell (right of the viewport)', () => {
  481. handsontable({
  482. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  483. height: 300,
  484. width: 300,
  485. });
  486. selectCells([[5, 15]]);
  487. expect(getCell(5, 15)).toBeVisibleAtRightOfViewport();
  488. });
  489. it('should by default scroll the viewport to the selected cell using column props (right of the viewport)', () => {
  490. handsontable({
  491. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  492. height: 300,
  493. width: 300,
  494. });
  495. selectCells([[5, 'prop15']]);
  496. expect(getCell(5, 15)).toBeVisibleAtRightOfViewport();
  497. });
  498. it('should by default scroll the viewport to the selected cell (left of the viewport)', () => {
  499. handsontable({
  500. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  501. height: 300,
  502. width: 300,
  503. });
  504. selectCell(5, 15); // Scroll to the right of the table.
  505. selectCells([[5, 0]]);
  506. expect(getCell(5, 0)).toBeVisibleAtLeftOfViewport();
  507. });
  508. it('should by default scroll the viewport to the selected cell using column props (left of the viewport)', () => {
  509. handsontable({
  510. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  511. height: 300,
  512. width: 300,
  513. });
  514. selectCell(5, 15); // Scroll to the right of the table.
  515. selectCells([[5, 'prop0']]);
  516. expect(getCell(5, 0)).toBeVisibleAtLeftOfViewport();
  517. });
  518. it('should by default scroll the viewport to the selected cell (top of the viewport)', () => {
  519. handsontable({
  520. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  521. height: 300,
  522. width: 300,
  523. });
  524. selectCell(19, 0); // Scroll to the bottom of the table.
  525. selectCells([[1, 0]]);
  526. expect(getCell(1, 0)).toBeVisibleAtTopOfViewport();
  527. });
  528. it('should by default scroll the viewport to the selected cell using column props (top of the viewport)', () => {
  529. handsontable({
  530. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  531. height: 300,
  532. width: 300,
  533. });
  534. selectCell(19, 0); // Scroll to the bottom of the table.
  535. selectCells([[1, 'prop0']]);
  536. expect(getCell(1, 0)).toBeVisibleAtTopOfViewport();
  537. });
  538. it('should not the scroll the viewport when `false` argument is passed', () => {
  539. handsontable({
  540. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  541. height: 300,
  542. width: 300,
  543. });
  544. selectCells([[15, 0]], false);
  545. expect(getCell(15, 0)).not.toBeVisibleInViewport();
  546. });
  547. it('should by default change the listener to handsontable instance from the action was triggered', () => {
  548. const afterListen = jasmine.createSpy();
  549. handsontable({
  550. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  551. height: 300,
  552. width: 300,
  553. afterListen,
  554. });
  555. selectCells([[15, 0]]);
  556. expect(afterListen).toHaveBeenCalled();
  557. });
  558. it('should not change the listening state when `false` argument is passed', () => {
  559. const afterListen = jasmine.createSpy();
  560. handsontable({
  561. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  562. height: 300,
  563. width: 300,
  564. afterListen,
  565. });
  566. selectCells([[15, 0]], true, false);
  567. expect(afterListen).not.toHaveBeenCalled();
  568. });
  569. it('should fire hooks with proper context', () => {
  570. const {
  571. afterSelection,
  572. afterSelectionByProp,
  573. afterSelectionEnd,
  574. afterSelectionEndByProp,
  575. beforeSetRangeStart,
  576. beforeSetRangeStartOnly,
  577. beforeSetRangeEnd,
  578. } = jasmine.createSpyObj('hooks', [
  579. 'afterSelection',
  580. 'afterSelectionByProp',
  581. 'afterSelectionEnd',
  582. 'afterSelectionEndByProp',
  583. 'beforeSetRangeStart',
  584. 'beforeSetRangeStartOnly',
  585. 'beforeSetRangeEnd',
  586. ]);
  587. const hot = handsontable({
  588. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  589. height: 300,
  590. width: 300,
  591. afterSelection,
  592. afterSelectionByProp,
  593. afterSelectionEnd,
  594. afterSelectionEndByProp,
  595. beforeSetRangeStart,
  596. beforeSetRangeStartOnly,
  597. beforeSetRangeEnd,
  598. });
  599. selectCells([[1, 2, 2, 4], [2, 1, 3, 2], [7, 7], [8, 4, 0, 4], [2, 4]]);
  600. expect(afterSelection.calls.first().object).toBe(hot);
  601. expect(afterSelectionByProp.calls.first().object).toBe(hot);
  602. expect(afterSelectionEnd.calls.first().object).toBe(hot);
  603. expect(afterSelectionEndByProp.calls.first().object).toBe(hot);
  604. expect(beforeSetRangeStartOnly.calls.first().object).toBe(hot);
  605. });
  606. it('should fire hooks with proper arguments when a single cell is selected', () => {
  607. const {
  608. afterSelection,
  609. afterSelectionByProp,
  610. afterSelectionEnd,
  611. afterSelectionEndByProp,
  612. beforeSetRangeStart,
  613. beforeSetRangeStartOnly,
  614. beforeSetRangeEnd,
  615. } = jasmine.createSpyObj('hooks', [
  616. 'afterSelection',
  617. 'afterSelectionByProp',
  618. 'afterSelectionEnd',
  619. 'afterSelectionEndByProp',
  620. 'beforeSetRangeStart',
  621. 'beforeSetRangeStartOnly',
  622. 'beforeSetRangeEnd',
  623. ]);
  624. handsontable({
  625. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  626. height: 300,
  627. width: 300,
  628. afterSelection,
  629. afterSelectionByProp,
  630. afterSelectionEnd,
  631. afterSelectionEndByProp,
  632. beforeSetRangeStart,
  633. beforeSetRangeStartOnly,
  634. beforeSetRangeEnd,
  635. });
  636. selectCells([[1, 2]]);
  637. expect(afterSelection.calls.count()).toBe(1);
  638. expect(afterSelection.calls.argsFor(0)).toEqual([1, 2, 1, 2, jasmine.any(Object), 0]);
  639. expect(afterSelectionByProp.calls.count()).toBe(1);
  640. expect(afterSelectionByProp.calls.argsFor(0)).toEqual([1, 'prop2', 1, 'prop2', jasmine.any(Object), 0]);
  641. expect(afterSelectionEnd.calls.count()).toBe(1);
  642. expect(afterSelectionEnd.calls.argsFor(0)).toEqual([1, 2, 1, 2, 0, void 0]);
  643. expect(afterSelectionEndByProp.calls.count()).toBe(1);
  644. expect(afterSelectionEndByProp.calls.argsFor(0)).toEqual([1, 'prop2', 1, 'prop2', 0, void 0]);
  645. expect(beforeSetRangeStart.calls.count()).toBe(0);
  646. expect(beforeSetRangeStartOnly.calls.count()).toBe(1);
  647. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].row).toBe(1);
  648. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].col).toBe(2);
  649. });
  650. it('should fire hooks with proper arguments when range of the cells are selected', () => {
  651. const {
  652. afterSelection,
  653. afterSelectionByProp,
  654. afterSelectionEnd,
  655. afterSelectionEndByProp,
  656. beforeSetRangeStart,
  657. beforeSetRangeStartOnly,
  658. beforeSetRangeEnd,
  659. } = jasmine.createSpyObj('hooks', [
  660. 'afterSelection',
  661. 'afterSelectionByProp',
  662. 'afterSelectionEnd',
  663. 'afterSelectionEndByProp',
  664. 'beforeSetRangeStart',
  665. 'beforeSetRangeStartOnly',
  666. 'beforeSetRangeEnd',
  667. ]);
  668. handsontable({
  669. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  670. height: 300,
  671. width: 300,
  672. afterSelection,
  673. afterSelectionByProp,
  674. afterSelectionEnd,
  675. afterSelectionEndByProp,
  676. beforeSetRangeStart,
  677. beforeSetRangeStartOnly,
  678. beforeSetRangeEnd,
  679. });
  680. selectCells([[1, 2, 2, 4]]);
  681. expect(afterSelection.calls.count()).toBe(1);
  682. expect(afterSelection.calls.argsFor(0)).toEqual([1, 2, 2, 4, jasmine.any(Object), 0]);
  683. expect(afterSelectionByProp.calls.count()).toBe(1);
  684. expect(afterSelectionByProp.calls.argsFor(0)).toEqual([1, 'prop2', 2, 'prop4', jasmine.any(Object), 0]);
  685. expect(afterSelectionEnd.calls.count()).toBe(1);
  686. expect(afterSelectionEnd.calls.argsFor(0)).toEqual([1, 2, 2, 4, 0, void 0]);
  687. expect(afterSelectionEndByProp.calls.count()).toBe(1);
  688. expect(afterSelectionEndByProp.calls.argsFor(0)).toEqual([1, 'prop2', 2, 'prop4', 0, void 0]);
  689. expect(beforeSetRangeStart.calls.count()).toBe(0);
  690. expect(beforeSetRangeStartOnly.calls.count()).toBe(1);
  691. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].row).toBe(1);
  692. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].col).toBe(2);
  693. });
  694. it('should fire hooks with proper arguments when the non-contiguous selection is added', () => {
  695. const {
  696. afterSelection,
  697. afterSelectionByProp,
  698. afterSelectionEnd,
  699. afterSelectionEndByProp,
  700. beforeSetRangeStart,
  701. beforeSetRangeStartOnly,
  702. beforeSetRangeEnd,
  703. } = jasmine.createSpyObj('hooks', [
  704. 'afterSelection',
  705. 'afterSelectionByProp',
  706. 'afterSelectionEnd',
  707. 'afterSelectionEndByProp',
  708. 'beforeSetRangeStart',
  709. 'beforeSetRangeStartOnly',
  710. 'beforeSetRangeEnd',
  711. ]);
  712. handsontable({
  713. data: Handsontable.helper.createSpreadsheetObjectData(20, 20),
  714. height: 300,
  715. width: 300,
  716. afterSelection,
  717. afterSelectionByProp,
  718. afterSelectionEnd,
  719. afterSelectionEndByProp,
  720. beforeSetRangeStart,
  721. beforeSetRangeStartOnly,
  722. beforeSetRangeEnd,
  723. });
  724. selectCells([[1, 2, 2, 4], [2, 1, 3, 2], [7, 7], [8, 4, 0, 4], [2, 4]]);
  725. expect(afterSelection.calls.count()).toBe(5);
  726. expect(afterSelection.calls.argsFor(0)).toEqual([1, 2, 2, 4, jasmine.any(Object), 0]);
  727. expect(afterSelection.calls.argsFor(1)).toEqual([2, 1, 3, 2, jasmine.any(Object), 1]);
  728. expect(afterSelection.calls.argsFor(2)).toEqual([7, 7, 7, 7, jasmine.any(Object), 2]);
  729. expect(afterSelection.calls.argsFor(3)).toEqual([8, 4, 0, 4, jasmine.any(Object), 3]);
  730. expect(afterSelection.calls.argsFor(4)).toEqual([2, 4, 2, 4, jasmine.any(Object), 4]);
  731. expect(afterSelectionByProp.calls.count()).toBe(5);
  732. expect(afterSelectionByProp.calls.argsFor(0)).toEqual([1, 'prop2', 2, 'prop4', jasmine.any(Object), 0]);
  733. expect(afterSelectionByProp.calls.argsFor(1)).toEqual([2, 'prop1', 3, 'prop2', jasmine.any(Object), 1]);
  734. expect(afterSelectionByProp.calls.argsFor(2)).toEqual([7, 'prop7', 7, 'prop7', jasmine.any(Object), 2]);
  735. expect(afterSelectionByProp.calls.argsFor(3)).toEqual([8, 'prop4', 0, 'prop4', jasmine.any(Object), 3]);
  736. expect(afterSelectionByProp.calls.argsFor(4)).toEqual([2, 'prop4', 2, 'prop4', jasmine.any(Object), 4]);
  737. expect(afterSelectionEnd.calls.count()).toBe(5);
  738. expect(afterSelectionEnd.calls.argsFor(0)).toEqual([1, 2, 2, 4, 0, void 0]);
  739. expect(afterSelectionEnd.calls.argsFor(1)).toEqual([2, 1, 3, 2, 1, void 0]);
  740. expect(afterSelectionEnd.calls.argsFor(2)).toEqual([7, 7, 7, 7, 2, void 0]);
  741. expect(afterSelectionEnd.calls.argsFor(3)).toEqual([8, 4, 0, 4, 3, void 0]);
  742. expect(afterSelectionEnd.calls.argsFor(4)).toEqual([2, 4, 2, 4, 4, void 0]);
  743. expect(afterSelectionEndByProp.calls.count()).toBe(5);
  744. expect(afterSelectionEndByProp.calls.argsFor(0)).toEqual([1, 'prop2', 2, 'prop4', 0, void 0]);
  745. expect(afterSelectionEndByProp.calls.argsFor(1)).toEqual([2, 'prop1', 3, 'prop2', 1, void 0]);
  746. expect(afterSelectionEndByProp.calls.argsFor(2)).toEqual([7, 'prop7', 7, 'prop7', 2, void 0]);
  747. expect(afterSelectionEndByProp.calls.argsFor(3)).toEqual([8, 'prop4', 0, 'prop4', 3, void 0]);
  748. expect(afterSelectionEndByProp.calls.argsFor(4)).toEqual([2, 'prop4', 2, 'prop4', 4, void 0]);
  749. expect(beforeSetRangeStart.calls.count()).toBe(0);
  750. expect(beforeSetRangeStartOnly.calls.count()).toBe(5);
  751. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].row).toBe(1);
  752. expect(beforeSetRangeStartOnly.calls.argsFor(0)[0].col).toBe(2);
  753. expect(beforeSetRangeStartOnly.calls.argsFor(1)[0].row).toBe(2);
  754. expect(beforeSetRangeStartOnly.calls.argsFor(1)[0].col).toBe(1);
  755. expect(beforeSetRangeStartOnly.calls.argsFor(2)[0].row).toBe(7);
  756. expect(beforeSetRangeStartOnly.calls.argsFor(2)[0].col).toBe(7);
  757. expect(beforeSetRangeStartOnly.calls.argsFor(3)[0].row).toBe(8);
  758. expect(beforeSetRangeStartOnly.calls.argsFor(3)[0].col).toBe(4);
  759. expect(beforeSetRangeStartOnly.calls.argsFor(4)[0].row).toBe(2);
  760. expect(beforeSetRangeStartOnly.calls.argsFor(4)[0].col).toBe(4);
  761. });
  762. });