outsideClickDeselects.spec.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. describe('settings', () => {
  2. describe('outsideClickDeselects', () => {
  3. const id = 'testContainer';
  4. beforeEach(function() {
  5. this.$container = $(`<div id="${id}"></div>`).appendTo('body');
  6. });
  7. afterEach(function() {
  8. if (this.$container) {
  9. destroy();
  10. this.$container.remove();
  11. }
  12. });
  13. it('should not deselect the currently selected cell after clicking on a scrollbar', () => {
  14. const hot = handsontable({
  15. outsideClickDeselects: false,
  16. minRows: 20,
  17. minCols: 2,
  18. width: 400,
  19. height: 100
  20. });
  21. selectCell(0, 0);
  22. const holderBoundingBox = hot.view.wt.wtTable.holder.getBoundingClientRect();
  23. const verticalScrollbarCoords = {
  24. x: holderBoundingBox.left + holderBoundingBox.width - 3,
  25. y: holderBoundingBox.top + (holderBoundingBox.height / 2)
  26. };
  27. const horizontalScrollbarCoords = {
  28. x: holderBoundingBox.left + (holderBoundingBox.width / 2),
  29. y: holderBoundingBox.top + holderBoundingBox.height - 3
  30. };
  31. $(hot.view.wt.wtTable.holder).simulate('mousedown', {
  32. clientX: verticalScrollbarCoords.x,
  33. clientY: verticalScrollbarCoords.y
  34. });
  35. expect(getSelected()).toEqual([[0, 0, 0, 0]]);
  36. $(hot.view.wt.wtTable.holder).simulate('mousedown', {
  37. clientX: horizontalScrollbarCoords.x,
  38. clientY: horizontalScrollbarCoords.y
  39. });
  40. expect(getSelected()).toEqual([[0, 0, 0, 0]]);
  41. });
  42. it('should not deselect currently selected cell', () => {
  43. handsontable({
  44. outsideClickDeselects: false
  45. });
  46. selectCell(0, 0);
  47. $('html').simulate('mousedown');
  48. expect(getSelected()).toEqual([[0, 0, 0, 0]]);
  49. });
  50. it('should not deselect currently selected cell (outsideClickDeselects as function)', () => {
  51. handsontable({
  52. outsideClickDeselects: () => false
  53. });
  54. selectCell(0, 0);
  55. $('html').simulate('mousedown');
  56. expect(getSelected()).toEqual([[0, 0, 0, 0]]);
  57. });
  58. it('should deselect currently selected cell', () => {
  59. handsontable({
  60. outsideClickDeselects: true
  61. });
  62. selectCell(0, 0);
  63. $('html').simulate('mousedown');
  64. expect(getSelected()).toBeUndefined();
  65. });
  66. it('should deselect currently selected cell (outsideClickDeselects as function)', () => {
  67. handsontable({
  68. outsideClickDeselects: () => true
  69. });
  70. selectCell(0, 0);
  71. $('html').simulate('mousedown');
  72. expect(getSelected()).toBeUndefined();
  73. });
  74. it('should allow to focus on external input when outsideClickDeselects is set as true', async() => {
  75. const textarea = $('<input type="text">').prependTo($('body'));
  76. handsontable({
  77. outsideClickDeselects: true
  78. });
  79. selectCell(0, 0);
  80. // It is necessary to fire event simulation in the next event loop cycle due to the autofocus editable element in setImmediate function.
  81. await sleep(0);
  82. textarea.simulate('mousedown');
  83. textarea.focus();
  84. expect(document.activeElement).toBe(textarea[0]);
  85. await sleep(50);
  86. expect(document.activeElement).toBe(textarea[0]);
  87. textarea.remove();
  88. });
  89. it('should allow to focus on external input when outsideClickDeselects is set as true (outsideClickDeselects as function)', async() => {
  90. const textarea = $('<input type="text">').prependTo($('body'));
  91. handsontable({
  92. outsideClickDeselects: () => true
  93. });
  94. selectCell(0, 0);
  95. await sleep(0);
  96. textarea.simulate('mousedown');
  97. textarea.focus();
  98. expect(document.activeElement).toBe(textarea[0]);
  99. await sleep(50);
  100. expect(document.activeElement).toBe(textarea[0]);
  101. textarea.remove();
  102. });
  103. it('should allow to focus on external input when outsideClickDeselects is set as false', async() => {
  104. const textarea = $('<input type="text">').prependTo($('body'));
  105. handsontable({
  106. outsideClickDeselects: false
  107. });
  108. selectCell(0, 0);
  109. await sleep(0);
  110. textarea.simulate('mousedown');
  111. textarea.focus();
  112. expect(document.activeElement).toBe(textarea[0]);
  113. await sleep(50);
  114. expect(document.activeElement).toBe(textarea[0]);
  115. textarea.remove();
  116. });
  117. it('should allow to focus on external input when outsideClickDeselects is set as false (outsideClickDeselects as function)', async() => {
  118. const textarea = $('<input type="text">').prependTo($('body'));
  119. handsontable({
  120. outsideClickDeselects: () => false
  121. });
  122. selectCell(0, 0);
  123. await sleep(0);
  124. textarea.simulate('mousedown');
  125. textarea.focus();
  126. expect(document.activeElement).toBe(textarea[0]);
  127. await sleep(50);
  128. expect(document.activeElement).toBe(textarea[0]);
  129. textarea.remove();
  130. });
  131. it('should allow to type in external input while holding current selection information', async() => {
  132. const textarea = $('<textarea></textarea>').prependTo($('body'));
  133. let keyPressed;
  134. handsontable({
  135. outsideClickDeselects: false
  136. });
  137. selectCell(0, 0);
  138. textarea.focus();
  139. textarea.simulate('mousedown');
  140. textarea.simulate('mouseup');
  141. textarea.on('keydown', (event) => {
  142. keyPressed = event.keyCode;
  143. });
  144. const LETTER_A_KEY = 97;
  145. $(document.activeElement).simulate('keydown', {
  146. keyCode: LETTER_A_KEY
  147. });
  148. // textarea should receive the event and be an active element
  149. expect(keyPressed).toEqual(LETTER_A_KEY);
  150. expect(document.activeElement).toBe(textarea[0]);
  151. // should preserve selection, close editor and save changes
  152. expect(getSelected()).toEqual([[0, 0, 0, 0]]);
  153. expect(getDataAtCell(0, 0)).toBeNull();
  154. await sleep(50);
  155. $(document.activeElement).simulate('keydown', {
  156. keyCode: LETTER_A_KEY
  157. });
  158. expect(getSelected()).toEqual([[0, 0, 0, 0]]);
  159. expect(getDataAtCell(0, 0)).toBeNull();
  160. textarea.remove();
  161. });
  162. it('should allow to type in external input while holding current selection information (outsideClickDeselects as function)', async() => {
  163. const textarea = $('<textarea></textarea>').prependTo($('body'));
  164. let keyPressed;
  165. handsontable({
  166. outsideClickDeselects: () => false
  167. });
  168. selectCell(0, 0);
  169. textarea.focus();
  170. textarea.simulate('mousedown');
  171. textarea.simulate('mouseup');
  172. textarea.on('keydown', (event) => {
  173. keyPressed = event.keyCode;
  174. });
  175. const LETTER_A_KEY = 97;
  176. $(document.activeElement).simulate('keydown', {
  177. keyCode: LETTER_A_KEY
  178. });
  179. // textarea should receive the event and be an active element
  180. expect(keyPressed).toEqual(LETTER_A_KEY);
  181. expect(document.activeElement).toBe(textarea[0]);
  182. // should preserve selection, close editor and save changes
  183. expect(getSelected()).toEqual([[0, 0, 0, 0]]);
  184. expect(getDataAtCell(0, 0)).toBeNull();
  185. await sleep(50);
  186. $(document.activeElement).simulate('keydown', {
  187. keyCode: LETTER_A_KEY
  188. });
  189. expect(getSelected()).toEqual([[0, 0, 0, 0]]);
  190. expect(getDataAtCell(0, 0)).toBeNull();
  191. textarea.remove();
  192. });
  193. xit('should allow to type in external input after opening cell editor', async() => {
  194. const textarea = $('<textarea></textarea>').prependTo($('body'));
  195. let keyPressed;
  196. handsontable({
  197. outsideClickDeselects: false
  198. });
  199. selectCell(0, 0);
  200. keyDown('enter');
  201. document.activeElement.value = 'Foo';
  202. textarea.focus();
  203. textarea.simulate('mousedown');
  204. textarea.simulate('mouseup');
  205. textarea.on('keydown', (event) => {
  206. keyPressed = event.keyCode;
  207. });
  208. const LETTER_A_KEY = 97;
  209. $(document.activeElement).simulate('keydown', {
  210. keyCode: LETTER_A_KEY
  211. });
  212. // textarea should receive the event and be an active element
  213. expect(keyPressed).toEqual(LETTER_A_KEY);
  214. expect(document.activeElement).toBe(textarea[0]);
  215. // should preserve selection, close editor and save changes
  216. expect(getSelected()).toEqual([[0, 0, 0, 0]]);
  217. expect(getDataAtCell(0, 0)).toEqual('Foo');
  218. await sleep(50);
  219. $(document.activeElement).simulate('keydown', {
  220. keyCode: LETTER_A_KEY
  221. });
  222. expect(getSelected()).toEqual([[0, 0, 0, 0]]);
  223. expect(getDataAtCell(0, 0)).toEqual('Foo');
  224. textarea.remove();
  225. });
  226. it('should allow to type in external input after opening cell editor (outsideClickDeselects as function)', async() => {
  227. const textarea = $('<textarea></textarea>').prependTo($('body'));
  228. let keyPressed;
  229. handsontable({
  230. outsideClickDeselects: () => false
  231. });
  232. selectCell(0, 0);
  233. keyDown('enter');
  234. document.activeElement.value = 'Foo';
  235. textarea.focus();
  236. textarea.simulate('mousedown');
  237. textarea.simulate('mouseup');
  238. textarea.on('keydown', (event) => {
  239. keyPressed = event.keyCode;
  240. });
  241. const LETTER_A_KEY = 97;
  242. $(document.activeElement).simulate('keydown', {
  243. keyCode: LETTER_A_KEY
  244. });
  245. // textarea should receive the event and be an active element
  246. expect(keyPressed).toEqual(LETTER_A_KEY);
  247. expect(document.activeElement).toBe(textarea[0]);
  248. // should preserve selection, close editor and save changes
  249. expect(getSelected()).toEqual([[0, 0, 0, 0]]);
  250. expect(getDataAtCell(0, 0)).toEqual('Foo');
  251. await sleep(50);
  252. $(document.activeElement).simulate('keydown', {
  253. keyCode: LETTER_A_KEY
  254. });
  255. expect(getSelected()).toEqual([[0, 0, 0, 0]]);
  256. expect(getDataAtCell(0, 0)).toEqual('Foo');
  257. textarea.remove();
  258. });
  259. });
  260. });