copyPaste.e2e.js 13 KB

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