editor.spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. describe('settings', () => {
  2. describe('editor', () => {
  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. describe('defined in constructor', () => {
  14. it('should use text editor by default', () => {
  15. const textEditorPrototype = Handsontable.editors.TextEditor.prototype;
  16. spyOn(textEditorPrototype, 'init').and.callThrough();
  17. handsontable();
  18. selectCell(0, 0);
  19. expect(textEditorPrototype.init).toHaveBeenCalled();
  20. });
  21. it('should use editor from predefined string', () => {
  22. const textEditorPrototype = Handsontable.editors.TextEditor.prototype;
  23. const checkboxEditorPrototype = Handsontable.editors.CheckboxEditor.prototype;
  24. spyOn(textEditorPrototype, 'init');
  25. spyOn(checkboxEditorPrototype, 'init');
  26. handsontable({
  27. columns: [
  28. {
  29. editor: 'checkbox'
  30. }
  31. ]
  32. });
  33. selectCell(0, 0);
  34. expect(textEditorPrototype.init).not.toHaveBeenCalled();
  35. expect(checkboxEditorPrototype.init).toHaveBeenCalled();
  36. });
  37. it('should use editor from predefined string when columns is a function', () => {
  38. const textEditorPrototype = Handsontable.editors.TextEditor.prototype;
  39. const checkboxEditorPrototype = Handsontable.editors.CheckboxEditor.prototype;
  40. spyOn(textEditorPrototype, 'init');
  41. spyOn(checkboxEditorPrototype, 'init');
  42. handsontable({
  43. columns(column) {
  44. return column === 0 ? { editor: 'checkbox' } : null;
  45. }
  46. });
  47. selectCell(0, 0);
  48. expect(textEditorPrototype.init).not.toHaveBeenCalled();
  49. expect(checkboxEditorPrototype.init).toHaveBeenCalled();
  50. });
  51. it('should use editor class passed directly', () => {
  52. const customEditor = jasmine.createSpy('customEditor');
  53. customEditor.and.callFake(function() {
  54. this.prepare = function() {};
  55. this.isOpened = function() {};
  56. });
  57. handsontable({
  58. columns: [
  59. {
  60. editor: customEditor
  61. }
  62. ]
  63. });
  64. selectCell(0, 0);
  65. expect(customEditor).toHaveBeenCalled();
  66. });
  67. it('should use editor class passed directly when columns is a function', () => {
  68. const customEditor = jasmine.createSpy('customEditor');
  69. customEditor.and.callFake(function() {
  70. this.prepare = function() {};
  71. this.isOpened = function() {};
  72. });
  73. handsontable({
  74. columns(column) {
  75. return column === 0 ? { editor: customEditor } : null;
  76. }
  77. });
  78. selectCell(0, 0);
  79. expect(customEditor).toHaveBeenCalled();
  80. });
  81. it('should use editor from custom string', () => {
  82. const customEditor = jasmine.createSpy('customEditor');
  83. customEditor.and.callFake(function() {
  84. this.prepare = function() {};
  85. this.isOpened = function() {};
  86. });
  87. Handsontable.editors.registerEditor('myEditor', customEditor);
  88. handsontable({
  89. columns: [
  90. {
  91. editor: 'myEditor'
  92. }
  93. ]
  94. });
  95. selectCell(0, 0);
  96. expect(customEditor).toHaveBeenCalled();
  97. });
  98. it('should use editor from custom string when columns is a function', () => {
  99. const customEditor = jasmine.createSpy('customEditor');
  100. customEditor.and.callFake(function() {
  101. this.prepare = function() {};
  102. this.isOpened = function() {};
  103. });
  104. Handsontable.editors.registerEditor('myEditor', customEditor);
  105. handsontable({
  106. columns(column) {
  107. return column === 0 ? { editor: 'myEditor' } : null;
  108. },
  109. });
  110. selectCell(0, 0);
  111. expect(customEditor).toHaveBeenCalled();
  112. });
  113. });
  114. });
  115. });