Core_beforechange.spec.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. describe('Core_beforechange', () => {
  2. const 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('this.rootElement should point to handsontable rootElement', () => {
  13. let output = null;
  14. handsontable({
  15. beforeChange() {
  16. output = this.rootElement;
  17. }
  18. });
  19. setDataAtCell(0, 0, 'test');
  20. expect(output).toEqual(spec().$container[0]);
  21. });
  22. it('should remove change from stack', () => {
  23. let output = null;
  24. handsontable({
  25. data: [['a', 'b'], ['c', 'd']],
  26. beforeChange(changes) {
  27. changes[1] = null;
  28. },
  29. afterChange(changes) {
  30. output = changes;
  31. }
  32. });
  33. setDataAtCell([[0, 0, 'test'], [1, 0, 'test'], [1, 1, 'test']]);
  34. expect(getDataAtCell(0, 0)).toEqual('test');
  35. expect(getDataAtCell(1, 0)).toEqual('c');
  36. expect(getDataAtCell(1, 1)).toEqual('test');
  37. expect(output).toEqual([[0, 0, 'a', 'test'], [1, 1, 'd', 'test']]);
  38. });
  39. it('should drop all changes when beforeChange return false', () => {
  40. handsontable({
  41. data: [['a', 'b'], ['c', 'd']],
  42. beforeChange() {
  43. return false;
  44. }
  45. });
  46. setDataAtCell([[0, 0, 'test'], [1, 0, 'test'], [1, 1, 'test']]);
  47. expect(getDataAtCell(0, 0)).toEqual('a');
  48. expect(getDataAtCell(1, 0)).toEqual('c');
  49. expect(getDataAtCell(1, 1)).toEqual('d');
  50. });
  51. function beforechangeOnKeyFactory(keyCode) {
  52. return function() {
  53. let called = false;
  54. handsontable({
  55. beforeChange(changes) {
  56. if (changes[0][2] === 'test' && changes[0][3] === '') {
  57. called = true;
  58. }
  59. }
  60. });
  61. setDataAtCell(0, 0, 'test');
  62. selectCell(0, 0);
  63. keyDown(keyCode);
  64. expect(called).toEqual(true);
  65. };
  66. }
  67. it('should be called on Delete key', beforechangeOnKeyFactory(46)); // 46 = Delete key
  68. it('should be called on Backspace key', beforechangeOnKeyFactory(8)); // 8 = Backspace key
  69. });