copyPaste.e2e.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. 'use strict';
  2. describe('CopyPaste', function () {
  3. var 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 remove additional new line from copied text (only safari)', function () {
  14. var getData = jasmine.createSpy().and.returnValue('a\nb\n\n');
  15. var preventDefault = jasmine.createSpy();
  16. var hot = handsontable();
  17. $('.copyPaste')[0].onpaste({ clipboardData: { getData: getData },
  18. preventDefault: preventDefault
  19. });
  20. if (Handsontable.helper.isSafari()) {
  21. expect($('.copyPaste')[0].value).toEqual('a\nb\n');
  22. expect(getData).toHaveBeenCalledWith('Text');
  23. expect(preventDefault).toHaveBeenCalled();
  24. } else if (Handsontable.helper.isChrome()) {
  25. expect($('.copyPaste')[0].value).toBe('a\nb\n\n');
  26. expect(getData).toHaveBeenCalledWith('Text');
  27. expect(preventDefault).toHaveBeenCalled();
  28. }
  29. });
  30. it('should allow blocking cutting cells by stopping the immediate propagation', function (done) {
  31. var onCut = jasmine.createSpy();
  32. var hot = handsontable({
  33. data: [['2012', 10, 11, 12, 13, 15, 16], ['2013', 10, 11, 12, 13, 15, 16]],
  34. beforeKeyDown: function beforeKeyDown(event) {
  35. if (event.ctrlKey && event.keyCode === Handsontable.helper.KEY_CODES.X) {
  36. event.isImmediatePropagationEnabled = false;
  37. }
  38. }
  39. });
  40. hot.copyPaste.copyPasteInstance.cutCallbacks.push(onCut);
  41. selectCell(0, 0);
  42. keyDown('ctrl+x');
  43. setTimeout(function () {
  44. expect(onCut).not.toHaveBeenCalled();
  45. done();
  46. }, 100);
  47. });
  48. describe('enabling/disabing plugin', function () {
  49. it('should enable copyPaste by default', function () {
  50. var hot = handsontable();
  51. expect(hot.copyPaste).toBeDefined();
  52. });
  53. it('should create copyPaste div if enabled', function () {
  54. expect($('#CopyPasteDiv').length).toEqual(0);
  55. var hot = handsontable();
  56. selectCell(0, 0);
  57. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT); // copyPaste div isn't created until you click CTRL
  58. expect($('#CopyPasteDiv').length).toEqual(1);
  59. });
  60. it('should not create copyPaste div if disabled', function () {
  61. expect($('#CopyPasteDiv').length).toEqual(0);
  62. var hot = handsontable({
  63. copyPaste: false
  64. });
  65. selectCell(0, 0);
  66. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  67. expect($('#CopyPasteDiv').length).toEqual(0);
  68. });
  69. it('should not create copyPaste property if plugin is disabled', function () {
  70. var hot = handsontable({
  71. copyPaste: false
  72. });
  73. expect(hot.copyPaste).toBeUndefined();
  74. });
  75. it('should enable/disable plugin using updateSettings', function () {
  76. var hot = handsontable();
  77. expect(hot.copyPaste).toBeDefined();
  78. updateSettings({
  79. copyPaste: false
  80. });
  81. expect(hot.copyPaste).toBe(null);
  82. });
  83. it('should remove copyPaste div if plugin has been disabled using updateSetting', function () {
  84. expect($('#CopyPasteDiv').length).toEqual(0);
  85. var hot = handsontable();
  86. selectCell(0, 0);
  87. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  88. expect($('#CopyPasteDiv').length).toEqual(1);
  89. updateSettings({
  90. copyPaste: false
  91. });
  92. expect($('#CopyPasteDiv').length).toEqual(0);
  93. selectCell(0, 0);
  94. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  95. expect($('#CopyPasteDiv').length).toEqual(0);
  96. });
  97. });
  98. describe('setting values copyable', function () {
  99. it('should set copyable text when selecting a single cell and hitting ctrl', function () {
  100. handsontable({
  101. data: Handsontable.helper.createSpreadsheetData(2, 2)
  102. });
  103. var copyPasteTextarea = $('textarea.copyPaste');
  104. expect(copyPasteTextarea.val().length).toEqual(0);
  105. selectCell(0, 0);
  106. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  107. expect(copyPasteTextarea.val()).toEqual('A1');
  108. });
  109. it('should set copyable text when selecting a single cell and hitting left command', function () {
  110. handsontable({
  111. data: Handsontable.helper.createSpreadsheetData(2, 2)
  112. });
  113. var copyPasteTextarea = $('textarea.copyPaste');
  114. expect(copyPasteTextarea.val().length).toEqual(0);
  115. selectCell(0, 0);
  116. keyDownUp(Handsontable.helper.KEY_CODES.COMMAND_LEFT);
  117. expect(copyPasteTextarea.val()).toEqual('A1');
  118. });
  119. it('should set copyable text when selecting a single cell and hitting right command', function () {
  120. handsontable({
  121. data: Handsontable.helper.createSpreadsheetData(2, 2)
  122. });
  123. var copyPasteTextarea = $('textarea.copyPaste');
  124. expect(copyPasteTextarea.val().length).toEqual(0);
  125. selectCell(0, 0);
  126. keyDownUp(Handsontable.helper.KEY_CODES.COMMAND_RIGHT);
  127. expect(copyPasteTextarea.val()).toEqual('A1');
  128. });
  129. it('should set copyable text when selecting multiple cells and hitting ctrl', function () {
  130. handsontable({
  131. data: Handsontable.helper.createSpreadsheetData(2, 2)
  132. });
  133. var copyPasteTextarea = $('textarea.copyPaste');
  134. expect(copyPasteTextarea.val().length).toEqual(0);
  135. selectCell(0, 0, 1, 0);
  136. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  137. expect(copyPasteTextarea.val()).toEqual('A1\nA2');
  138. });
  139. it('should set copyable text when selecting all cells with CTRL+A', function (done) {
  140. handsontable({
  141. data: Handsontable.helper.createSpreadsheetData(2, 2)
  142. });
  143. var copyPasteTextarea = $('textarea.copyPaste');
  144. expect(copyPasteTextarea.val().length).toEqual(0);
  145. selectCell(0, 0);
  146. $(document.activeElement).simulate('keydown', { keyCode: Handsontable.helper.KEY_CODES.A, ctrlKey: true });
  147. setTimeout(function () {
  148. expect(getSelected()).toEqual([0, 0, 1, 1]);
  149. expect(copyPasteTextarea.val()).toEqual('A1\tB1\nA2\tB2');
  150. done();
  151. }, 10);
  152. });
  153. it('should not throw error when no cell is selected (#1221)', function () {
  154. handsontable({
  155. data: Handsontable.helper.createSpreadsheetData(2, 2)
  156. });
  157. selectCell(0, 0);
  158. deselectCell();
  159. function keydownCtrl() {
  160. $(document).simulate('keydown', {
  161. keyCode: Handsontable.helper.KEY_CODES.COMMAND_LEFT
  162. });
  163. }
  164. // expect no to throw any exception
  165. expect(keydownCtrl).not.toThrow();
  166. });
  167. it('should set copyable text when selecting a single cell with specified type and hitting ctrl (#1300)', function () {
  168. handsontable({
  169. data: [['A', 1], ['B', 2]],
  170. columns: [{
  171. type: 'text'
  172. }, {
  173. type: 'numeric'
  174. }]
  175. });
  176. var copyPasteTextarea = $('textarea.copyPaste');
  177. expect(copyPasteTextarea.val().length).toEqual(0);
  178. selectCell(0, 0, 1, 1);
  179. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  180. expect(copyPasteTextarea.val()).toEqual('A\t1\nB\t2');
  181. });
  182. it('should set copyable text when selecting a single cell with editor type as false (#2574)', function () {
  183. handsontable({
  184. data: [['A', 1], ['B', 2]],
  185. columns: [{
  186. type: 'text'
  187. }, {
  188. editor: false
  189. }]
  190. });
  191. var copyPasteTextarea = $('textarea.copyPaste');
  192. expect(copyPasteTextarea.val().length).toEqual(0);
  193. selectCell(1, 1, 1, 1);
  194. keyDownUp(Handsontable.helper.KEY_CODES.CONTROL_LEFT);
  195. expect(copyPasteTextarea.val()).toEqual('2');
  196. });
  197. describe('working with multiple tables', function () {
  198. beforeEach(function () {
  199. this.$container2 = $('<div id="' + id + '2"></div>').appendTo('body');
  200. });
  201. afterEach(function () {
  202. if (this.$container2) {
  203. this.$container2.handsontable('destroy');
  204. this.$container2.remove();
  205. }
  206. });
  207. it('should disable copyPaste only in particular table', function () {
  208. var hot1 = handsontable();
  209. var hot2 = this.$container2.handsontable({
  210. copyPaste: false
  211. });
  212. expect(hot1.copyPaste).toBeDefined();
  213. expect(hot2.copyPaste).toBeUndefined();
  214. });
  215. it('should create only one CopyPasteDiv regardless of the number of tables', function () {
  216. var hot1 = handsontable();
  217. var hot2 = this.$container2.handsontable();
  218. expect($('#CopyPasteDiv').length).toEqual(1);
  219. });
  220. it('should leave CopyPasteDiv as long as at least one table has copyPaste enabled', function () {
  221. var hot1 = handsontable();
  222. var hot2 = this.$container2.handsontable().handsontable('getInstance');
  223. expect($('#CopyPasteDiv').length).toEqual(1);
  224. hot1.updateSettings({
  225. copyPaste: false
  226. });
  227. expect($('#CopyPasteDiv').length).toEqual(1);
  228. hot2.updateSettings({
  229. copyPaste: false
  230. });
  231. expect($('#CopyPasteDiv').length).toEqual(0);
  232. });
  233. });
  234. });
  235. describe('hooks', function () {
  236. it('should call beforeCut and afterCut during cutting out operation', function () {
  237. var beforeCutSpy = jasmine.createSpy('beforeCut');
  238. var afterCutSpy = jasmine.createSpy('afterCut');
  239. var hot = handsontable({
  240. data: Handsontable.helper.createSpreadsheetData(2, 2),
  241. beforeCut: beforeCutSpy,
  242. afterCut: afterCutSpy
  243. });
  244. selectCell(0, 0);
  245. keyDown('ctrl');
  246. keyDown('ctrl+x');
  247. expect(beforeCutSpy.calls.count()).toEqual(1);
  248. expect(beforeCutSpy).toHaveBeenCalledWith([['A1']], [{ startRow: 0, startCol: 0, endRow: 0, endCol: 0 }], void 0, void 0, void 0, void 0);
  249. expect(afterCutSpy.calls.count()).toEqual(1);
  250. expect(afterCutSpy).toHaveBeenCalledWith([['A1']], [{ startRow: 0, startCol: 0, endRow: 0, endCol: 0 }], void 0, void 0, void 0, void 0);
  251. });
  252. it('should call beforeCopy and afterCopy during copying operation', function () {
  253. var beforeCopySpy = jasmine.createSpy('beforeCopy');
  254. var afterCopySpy = jasmine.createSpy('afterCopy');
  255. var hot = handsontable({
  256. data: Handsontable.helper.createSpreadsheetData(2, 2),
  257. beforeCopy: beforeCopySpy,
  258. afterCopy: afterCopySpy
  259. });
  260. selectCell(0, 0);
  261. keyDown('ctrl');
  262. keyDown('ctrl+c');
  263. expect(beforeCopySpy.calls.count()).toEqual(1);
  264. expect(beforeCopySpy).toHaveBeenCalledWith([['A1']], [{ startRow: 0, startCol: 0, endRow: 0, endCol: 0 }], void 0, void 0, void 0, void 0);
  265. expect(afterCopySpy.calls.count()).toEqual(1);
  266. expect(afterCopySpy).toHaveBeenCalledWith([['A1']], [{ startRow: 0, startCol: 0, endRow: 0, endCol: 0 }], void 0, void 0, void 0, void 0);
  267. });
  268. it('should call beforePaste and afterPaste during copying operation', function (done) {
  269. var beforePasteSpy = jasmine.createSpy('beforePaste');
  270. var afterPasteSpy = jasmine.createSpy('afterPaste');
  271. var hot = handsontable({
  272. data: Handsontable.helper.createSpreadsheetData(2, 2),
  273. beforePaste: beforePasteSpy,
  274. afterPaste: afterPasteSpy
  275. });
  276. selectCell(0, 0);
  277. keyDown('ctrl');
  278. hot.copyPaste.triggerPaste(null, 'Kia');
  279. setTimeout(function () {
  280. expect(beforePasteSpy.calls.count()).toEqual(1);
  281. expect(beforePasteSpy).toHaveBeenCalledWith([['Kia']], [{ startRow: 0, startCol: 0, endRow: 0, endCol: 0 }], void 0, void 0, void 0, void 0);
  282. expect(afterPasteSpy.calls.count()).toEqual(1);
  283. expect(afterPasteSpy).toHaveBeenCalledWith([['Kia']], [{ startRow: 0, startCol: 0, endRow: 0, endCol: 0 }], void 0, void 0, void 0, void 0);
  284. done();
  285. }, 60);
  286. });
  287. it('should be possible to block cutting out', function () {
  288. var afterCutSpy = jasmine.createSpy('afterCut');
  289. var hot = handsontable({
  290. data: Handsontable.helper.createSpreadsheetData(2, 2),
  291. beforeCut: function beforeCut() {
  292. return false;
  293. },
  294. afterCut: afterCutSpy
  295. });
  296. selectCell(0, 0);
  297. keyDown('ctrl');
  298. keyDown('ctrl+x');
  299. expect(afterCutSpy.calls.count()).toEqual(0);
  300. });
  301. it('should be possible to block copying', function () {
  302. var afterCopySpy = jasmine.createSpy('afterCopy');
  303. var hot = handsontable({
  304. data: Handsontable.helper.createSpreadsheetData(2, 2),
  305. beforeCopy: function beforeCopy() {
  306. return false;
  307. },
  308. afterCopy: afterCopySpy
  309. });
  310. selectCell(0, 0);
  311. keyDown('ctrl');
  312. keyDown('ctrl+c');
  313. expect(afterCopySpy.calls.count()).toEqual(0);
  314. });
  315. it('should be possible to block pasting', function (done) {
  316. var afterPasteSpy = jasmine.createSpy('afterPaste');
  317. var hot = handsontable({
  318. data: Handsontable.helper.createSpreadsheetData(2, 2),
  319. beforePaste: function beforePaste() {
  320. return false;
  321. },
  322. afterPaste: afterPasteSpy
  323. });
  324. selectCell(0, 0);
  325. keyDown('ctrl');
  326. hot.copyPaste.triggerPaste(null, 'Kia');
  327. setTimeout(function () {
  328. expect(afterPasteSpy.calls.count()).toEqual(0);
  329. done();
  330. }, 60);
  331. });
  332. });
  333. });