editor.spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. describe('settings', () => {
  2. describe('editor', () => {
  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. describe('defined in constructor', () => {
  14. it('should use text editor by default', () => {
  15. var 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. var textEditorPrototype = Handsontable.editors.TextEditor.prototype;
  23. var 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. var textEditorPrototype = Handsontable.editors.TextEditor.prototype;
  39. var 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. var customEditor = jasmine.createSpy('customEditor');
  53. customEditor.and.callFake(function() {
  54. this.prepare = function() {};
  55. });
  56. handsontable({
  57. columns: [
  58. {
  59. editor: customEditor
  60. }
  61. ]
  62. });
  63. selectCell(0, 0);
  64. expect(customEditor).toHaveBeenCalled();
  65. });
  66. it('should use editor class passed directly when columns is a function', () => {
  67. var customEditor = jasmine.createSpy('customEditor');
  68. customEditor.and.callFake(function() {
  69. this.prepare = function() {};
  70. });
  71. handsontable({
  72. columns(column) {
  73. return column === 0 ? {editor: customEditor} : null;
  74. }
  75. });
  76. selectCell(0, 0);
  77. expect(customEditor).toHaveBeenCalled();
  78. });
  79. it('should use editor from custom string', () => {
  80. var customEditor = jasmine.createSpy('customEditor');
  81. customEditor.and.callFake(function() {
  82. this.prepare = function() {};
  83. });
  84. Handsontable.editors.registerEditor('myEditor', customEditor);
  85. handsontable({
  86. columns: [
  87. {
  88. editor: 'myEditor'
  89. }
  90. ]
  91. });
  92. selectCell(0, 0);
  93. expect(customEditor).toHaveBeenCalled();
  94. });
  95. it('should use editor from custom string when columns is a function', () => {
  96. var customEditor = jasmine.createSpy('customEditor');
  97. customEditor.and.callFake(function() {
  98. this.prepare = function() {};
  99. });
  100. Handsontable.editors.registerEditor('myEditor', customEditor);
  101. handsontable({
  102. columns(column) {
  103. return column === 0 ? {editor: 'myEditor'} : null;
  104. },
  105. });
  106. selectCell(0, 0);
  107. expect(customEditor).toHaveBeenCalled();
  108. });
  109. });
  110. });
  111. });