index.spec.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. describe('editors', () => {
  2. const id = 'testContainer';
  3. const {
  4. registerEditor,
  5. getEditor,
  6. } = Handsontable.editors;
  7. beforeEach(function() {
  8. this.$container = $(`<div id="${id}"></div>`).appendTo('body');
  9. });
  10. afterEach(function() {
  11. if (this.$container) {
  12. destroy();
  13. this.$container.remove();
  14. }
  15. });
  16. it('should register custom editor', () => {
  17. class MyEditor extends Handsontable.editors.BaseEditor {
  18. init() {
  19. this.TEXTAREA = document.createElement('TEXTAREA');
  20. this.TEXTAREA_PARENT = document.createElement('DIV');
  21. this.TEXTAREA_PARENT.appendChild(this.TEXTAREA);
  22. this.instance.rootElement.appendChild(this.TEXTAREA_PARENT);
  23. }
  24. getValue() {
  25. return `--${this.TEXTAREA.value}--`;
  26. }
  27. setValue(value) {
  28. this.TEXTAREA.value = value;
  29. }
  30. open() {}
  31. close() {}
  32. focus() {
  33. this.TEXTAREA.focus();
  34. }
  35. }
  36. registerEditor('myEditor', MyEditor);
  37. handsontable({
  38. data: [
  39. [1, 6, 10],
  40. ],
  41. columns: [{
  42. editor: 'myEditor',
  43. }],
  44. });
  45. selectCell(0, 0);
  46. keyDown('enter');
  47. document.activeElement.value = 'hello';
  48. destroyEditor();
  49. expect(getDataAtCell(0, 0)).toBe('--hello--');
  50. });
  51. it('should retrieve predefined editors by its names', () => {
  52. expect(getEditor('autocomplete')).toBeFunction();
  53. expect(getEditor('base')).toBeFunction();
  54. expect(getEditor('checkbox')).toBeFunction();
  55. expect(getEditor('date')).toBeFunction();
  56. expect(getEditor('dropdown')).toBeFunction();
  57. expect(getEditor('handsontable')).toBeFunction();
  58. expect(getEditor('numeric')).toBeFunction();
  59. expect(getEditor('password')).toBeFunction();
  60. expect(getEditor('select')).toBeFunction();
  61. expect(getEditor('text')).toBeFunction();
  62. });
  63. it('should retrieve custom editor by its names', () => {
  64. class MyEditor {}
  65. registerEditor('myEditor', MyEditor);
  66. expect(getEditor('myEditor')).toBe(MyEditor);
  67. });
  68. it('should reset previous value when printable character was entered to selected, non-empty cell', async() => {
  69. handsontable({
  70. data: [
  71. { id: 10, name: 'Cup' },
  72. { id: 23, name: 'Newspaper' },
  73. { id: 31, name: 'Car' }
  74. ],
  75. columns: [
  76. { data: 'id', type: 'numeric' },
  77. { data: 'name' },
  78. ]
  79. });
  80. selectCell(0, 0);
  81. keyDownUp('1'.charCodeAt(0));
  82. destroyEditor();
  83. await sleep(100);
  84. expect(getCell(0, 0).innerHTML).not.toEqual('10');
  85. selectCell(0, 1);
  86. keyDownUp('a'.charCodeAt(0));
  87. destroyEditor();
  88. await sleep(100);
  89. expect(getCell(1, 0).innerHTML).not.toEqual('Cup');
  90. });
  91. });