ec28f208d3c52d3fc80ad3bded2b787fab1b2fd8e66abe2b05403e6805f7841d8a06dad9fee6c693762dea512d2e9736c254cc8c5976affc0eb5e496da3150 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. describe('Core_onKeyDown', () => {
  2. var id = 'testContainer';
  3. beforeEach(function() {
  4. this.$container = $(`<div id="${id}"></div>`).appendTo('body');
  5. });
  6. afterEach(function() {
  7. if (this.$container) {
  8. destroy();
  9. this.$container.remove();
  10. }
  11. });
  12. it('should advance to next cell when TAB is pressed', () => {
  13. // https://github.com/handsontable/handsontable/issues/151
  14. handsontable();
  15. selectCell(0, 0);
  16. keyDownUp('tab');
  17. expect(getSelected()).toEqual([0, 1, 0, 1]);
  18. });
  19. it('should advance to previous cell when shift+TAB is pressed', () => {
  20. handsontable();
  21. selectCell(1, 1);
  22. keyDownUp('shift+tab');
  23. expect(getSelected()).toEqual([1, 0, 1, 0]);
  24. });
  25. describe('while editing (quick edit mode)', () => {
  26. it('should finish editing and advance to next cell when TAB is pressed', () => {
  27. // https://github.com/handsontable/handsontable/issues/215
  28. handsontable();
  29. selectCell(1, 1);
  30. keyDownUp('x'); // value to cell trigger quick edit mode
  31. keyProxy().val('Ted');
  32. keyDownUp('tab');
  33. expect(getData()[1][1]).toEqual('Ted');
  34. expect(getSelected()).toEqual([1, 2, 1, 2]);
  35. });
  36. it('should finish editing and advance to lower cell when enter is pressed', () => {
  37. // https://github.com/handsontable/handsontable/issues/215
  38. handsontable();
  39. selectCell(1, 1);
  40. keyDownUp('x'); // value to cell trigger quick edit mode
  41. keyProxy().val('Ted');
  42. keyDownUp('enter');
  43. expect(getData()[1][1]).toEqual('Ted');
  44. expect(getSelected()).toEqual([2, 1, 2, 1]);
  45. });
  46. it('should finish editing and advance to higher cell when shift+enter is pressed', () => {
  47. // https://github.com/handsontable/handsontable/issues/215
  48. handsontable();
  49. selectCell(1, 1);
  50. keyDownUp('x'); // trigger quick edit mode
  51. keyProxy().val('Ted');
  52. keyDownUp('shift+enter');
  53. expect(getData()[1][1]).toEqual('Ted');
  54. expect(getSelected()).toEqual([0, 1, 0, 1]);
  55. });
  56. it('should finish editing and advance to lower cell when down arrow is pressed', () => {
  57. handsontable();
  58. selectCell(1, 1);
  59. keyDownUp('x');
  60. keyProxy().val('Ted');
  61. keyDownUp('arrow_down');
  62. expect(getData()[1][1]).toEqual('Ted');
  63. expect(getSelected()).toEqual([2, 1, 2, 1]);
  64. });
  65. it('should finish editing and advance to higher cell when up arrow is pressed', () => {
  66. handsontable();
  67. selectCell(1, 1);
  68. keyDownUp('x');
  69. keyProxy().val('Ted');
  70. keyDownUp('arrow_up');
  71. expect(getData()[1][1]).toEqual('Ted');
  72. expect(getSelected()).toEqual([0, 1, 0, 1]);
  73. });
  74. it('should finish editing and advance to right cell when right arrow is pressed', () => {
  75. handsontable();
  76. selectCell(1, 1);
  77. keyDownUp('x');
  78. keyProxy().val('Ted');
  79. keyDownUp('arrow_right');
  80. keyDownUp('arrow_right');
  81. keyDownUp('arrow_right');
  82. keyDownUp('arrow_right');
  83. expect(getData()[1][1]).toEqual('Ted');
  84. expect(getSelected()).toEqual([1, 4, 1, 4]);
  85. });
  86. it('should finish editing and advance to left cell when left arrow is pressed', () => {
  87. handsontable();
  88. selectCell(1, 1);
  89. keyDownUp('x');
  90. keyProxy().val('Ted');
  91. Handsontable.dom.setCaretPosition(keyProxy()[0], 0, 0);
  92. keyDownUp('arrow_left');
  93. keyDownUp('arrow_left');
  94. keyDownUp('arrow_left');
  95. keyDownUp('arrow_left');
  96. keyDownUp('arrow_left');
  97. expect(getData()[1][1]).toEqual('Ted');
  98. expect(getSelected()).toEqual([1, 0, 1, 0]);
  99. });
  100. it('should finish editing and advance to lower cell when enter is pressed (with sync validator)', (done) => {
  101. var onAfterValidate = jasmine.createSpy('onAfterValidate');
  102. handsontable({
  103. validator(val, cb) {
  104. cb(true);
  105. },
  106. afterValidate: onAfterValidate
  107. });
  108. selectCell(1, 1);
  109. keyDownUp('x');
  110. keyProxy().val('Ted');
  111. onAfterValidate.calls.reset();
  112. keyDownUp('enter');
  113. setTimeout(() => {
  114. expect(onAfterValidate).toHaveBeenCalled();
  115. expect(getData()[1][1]).toEqual('Ted');
  116. expect(getSelected()).toEqual([2, 1, 2, 1]);
  117. done();
  118. }, 200);
  119. });
  120. it('should finish editing and advance to lower cell when enter is pressed (with async validator)', (done) => {
  121. var onAfterValidate = jasmine.createSpy('onAfterValidate');
  122. handsontable({
  123. validator(val, cb) {
  124. setTimeout(() => {
  125. cb(true);
  126. }, 10);
  127. },
  128. afterValidate: onAfterValidate
  129. });
  130. selectCell(1, 1);
  131. keyDownUp('x');
  132. keyProxy().val('Ted');
  133. onAfterValidate.calls.reset();
  134. keyDownUp('enter');
  135. setTimeout(() => {
  136. expect(onAfterValidate).toHaveBeenCalled();
  137. expect(getData()[1][1]).toEqual('Ted');
  138. expect(getSelected()).toEqual([2, 1, 2, 1]);
  139. done();
  140. }, 200);
  141. });
  142. });
  143. describe('while editing (full edit mode)', () => {
  144. it('should finish editing and advance to next cell when TAB is pressed', () => {
  145. // https://github.com/handsontable/handsontable/issues/215
  146. handsontable();
  147. selectCell(1, 1);
  148. keyDownUp('enter');
  149. keyProxy().val('Ted');
  150. keyDownUp('tab');
  151. expect(getData()[1][1]).toEqual('Ted');
  152. expect(getSelected()).toEqual([1, 2, 1, 2]);
  153. });
  154. it('should finish editing and advance to lower cell when enter is pressed', () => {
  155. // https://github.com/handsontable/handsontable/issues/215
  156. handsontable();
  157. selectCell(1, 1);
  158. keyDownUp('enter');
  159. keyProxy().val('Ted');
  160. keyDownUp('enter');
  161. expect(getData()[1][1]).toEqual('Ted');
  162. expect(getSelected()).toEqual([2, 1, 2, 1]);
  163. });
  164. it('should finish editing and advance to higher cell when shift+enter is pressed', () => {
  165. // https://github.com/handsontable/handsontable/issues/215
  166. handsontable();
  167. selectCell(1, 1);
  168. keyDownUp('enter');
  169. keyProxy().val('Ted');
  170. keyDownUp('shift+enter');
  171. expect(getData()[1][1]).toEqual('Ted');
  172. expect(getSelected()).toEqual([0, 1, 0, 1]);
  173. });
  174. it('shouldn\'t finish editing and advance to lower cell when down arrow is pressed', () => {
  175. handsontable();
  176. selectCell(1, 1);
  177. keyDownUp('enter');
  178. keyProxy().val('Ted');
  179. keyDownUp('arrow_down');
  180. expect(getData()[1][1]).toEqual(null);
  181. expect(getSelected()).toEqual([1, 1, 1, 1]);
  182. });
  183. it('shouldn\'t finish editing and advance to higher cell when up arrow is pressed', () => {
  184. handsontable();
  185. selectCell(1, 1);
  186. keyDownUp('enter');
  187. keyProxy().val('Ted');
  188. keyDownUp('arrow_up');
  189. expect(getData()[1][1]).toEqual(null);
  190. expect(getSelected()).toEqual([1, 1, 1, 1]);
  191. });
  192. it('shouldn\'t finish editing and advance to right cell when right arrow is pressed', () => {
  193. handsontable();
  194. selectCell(1, 1);
  195. keyDownUp('enter');
  196. keyProxy().val('Ted');
  197. keyDownUp('arrow_right');
  198. keyDownUp('arrow_right');
  199. keyDownUp('arrow_right');
  200. keyDownUp('arrow_right');
  201. expect(getData()[1][1]).toEqual(null);
  202. expect(getSelected()).toEqual([1, 1, 1, 1]);
  203. });
  204. it('shouldn\'t finish editing and advance to left cell when left arrow is pressed', () => {
  205. handsontable();
  206. selectCell(1, 1);
  207. keyDownUp('enter');
  208. keyProxy().val('Ted');
  209. keyDownUp('arrow_left');
  210. keyDownUp('arrow_left');
  211. keyDownUp('arrow_left');
  212. keyDownUp('arrow_left');
  213. expect(getData()[1][1]).toEqual(null);
  214. expect(getSelected()).toEqual([1, 1, 1, 1]);
  215. });
  216. it('should finish editing and advance to lower cell when enter is pressed (with sync validator)', (done) => {
  217. var onAfterValidate = jasmine.createSpy('onAfterValidate');
  218. handsontable({
  219. validator(val, cb) {
  220. cb(true);
  221. },
  222. afterValidate: onAfterValidate
  223. });
  224. selectCell(1, 1);
  225. keyDownUp('enter');
  226. keyProxy().val('Ted');
  227. onAfterValidate.calls.reset();
  228. keyDownUp('enter');
  229. setTimeout(() => {
  230. expect(onAfterValidate).toHaveBeenCalled();
  231. expect(getData()[1][1]).toEqual('Ted');
  232. expect(getSelected()).toEqual([2, 1, 2, 1]);
  233. done();
  234. }, 200);
  235. });
  236. it('should finish editing and advance to lower cell when enter is pressed (with async validator)', (done) => {
  237. var onAfterValidate = jasmine.createSpy('onAfterValidate');
  238. handsontable({
  239. validator(val, cb) {
  240. setTimeout(() => {
  241. cb(true);
  242. }, 10);
  243. },
  244. afterValidate: onAfterValidate
  245. });
  246. selectCell(1, 1);
  247. keyDownUp('enter');
  248. keyProxy().val('Ted');
  249. onAfterValidate.calls.reset();
  250. keyDownUp('enter');
  251. setTimeout(() => {
  252. expect(onAfterValidate).toHaveBeenCalled();
  253. expect(getData()[1][1]).toEqual('Ted');
  254. expect(getSelected()).toEqual([2, 1, 2, 1]);
  255. done();
  256. }, 200);
  257. });
  258. });
  259. });