columns.spec.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. describe('settings', () => {
  2. describe('columns', () => {
  3. var id = 'testContainer';
  4. var arrayOfArrays = function() {
  5. return [
  6. ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
  7. ['2008', 10, 11, 12, 13],
  8. ['2009', 20, 11, 14, 13],
  9. ['2010', 30, 15, 12, 13]
  10. ];
  11. };
  12. var arrayOfObjects = function() {
  13. return [
  14. {id: 1, name: 'Ted', lastName: 'Right', date: '01/01/2015'},
  15. {id: 2, name: 'Frank', lastName: 'Honest', date: '01/01/15'},
  16. {id: 3, name: 'Joan', lastName: 'Well', date: '41/01/2015'},
  17. {id: 4, name: 'Sid', lastName: 'Strong', date: '01/51/2015'},
  18. {id: 5, name: 'Jane', lastName: 'Neat', date: '01/01/2015'},
  19. {id: 6, name: 'Chuck', lastName: 'Jackson', date: '01/01/15'},
  20. {id: 7, name: 'Meg', lastName: 'Jansen', date: '41/01/2015'},
  21. {id: 8, name: 'Rob', lastName: 'Norris', date: '01/51/2015'},
  22. {id: 9, name: 'Sean', lastName: 'O\'Hara', date: '01/01/2015'},
  23. {id: 10, name: 'Eve', lastName: 'Branson', date: '01/01/15'}
  24. ];
  25. };
  26. beforeEach(function() {
  27. this.$container = $(`<div id="${id}"></div>`).appendTo('body');
  28. });
  29. afterEach(function() {
  30. if (this.$container) {
  31. destroy();
  32. this.$container.remove();
  33. }
  34. });
  35. describe('as an array of objects', () => {
  36. it('should not throw exception when passed columns array is empty (data source as array of arrays)', () => {
  37. var hot = handsontable({
  38. data: arrayOfArrays(),
  39. columns: [
  40. {data: 0},
  41. {data: 1},
  42. {data: 2}
  43. ]
  44. });
  45. expect(() => {
  46. hot.updateSettings({columns: []});
  47. }).not.toThrow();
  48. });
  49. it('should not throw exception when passed columns array is empty (data source as array of objects)', () => {
  50. var hot = handsontable({
  51. data: arrayOfObjects(),
  52. columns: [
  53. {data: 'id'},
  54. {data: 'name'},
  55. {data: 'lastName'}
  56. ],
  57. });
  58. expect(() => {
  59. hot.updateSettings({columns: []});
  60. }).not.toThrow();
  61. });
  62. });
  63. describe('as a function', () => {
  64. describe('init', () => {
  65. it('should render only these columns which are not `null`', () => {
  66. var hot = handsontable({
  67. data: arrayOfArrays(),
  68. columns(column) {
  69. return [1, 2].indexOf(column) > -1 ? {} : null;
  70. }
  71. });
  72. expect(hot.getData()[0].length).toEqual(2);
  73. });
  74. it('should properly bind default data when is not defined (data source as array of arrays)', () => {
  75. var hot = handsontable({
  76. data: arrayOfArrays(),
  77. columns(column) {
  78. return [1, 2].indexOf(column) > -1 ? {} : null;
  79. }
  80. });
  81. expect(hot.getDataAtCell(0, 0)).toEqual('');
  82. expect(hot.getDataAtCell(0, 1)).toEqual('Kia');
  83. });
  84. it('should properly bind default data when is not defined (data source as array of objects)', () => {
  85. var hot = handsontable({
  86. data: arrayOfObjects(),
  87. columns(column) {
  88. return [1, 2].indexOf(column) > -1 ? {} : null;
  89. }
  90. });
  91. expect(hot.getDataAtCell(0, 0)).toEqual(null);
  92. expect(hot.getDataAtCell(0, 1)).toEqual(null);
  93. });
  94. it('should properly bind defined data (data source as array of arrays)', () => {
  95. var hot = handsontable({
  96. data: arrayOfArrays(),
  97. columns(column) {
  98. return [1, 2].indexOf(column) > -1 ? {data: column + 1} : null;
  99. }
  100. });
  101. expect(hot.getDataAtCell(0, 0)).toEqual('Nissan');
  102. expect(hot.getDataAtCell(0, 1)).toEqual('Toyota');
  103. });
  104. it('should properly bind defined data (data source as array of objects)', () => {
  105. var hot = handsontable({
  106. data: arrayOfObjects(),
  107. columns(column) {
  108. var keys = ['id', 'name', 'lastName'];
  109. return [1, 2].indexOf(column) > -1 ? {data: keys[column - 1]} : null;
  110. }
  111. });
  112. expect(hot.getDataAtCell(0, 0)).toEqual(1);
  113. expect(hot.getDataAtCell(0, 1)).toEqual('Ted');
  114. });
  115. });
  116. describe('updateSettings', () => {
  117. it('should not throw exception when passed columns function without return anything (data source as array of arrays) when columns is a function', () => {
  118. var hot = handsontable({
  119. data: arrayOfArrays(),
  120. columns(column) {
  121. return [0, 1, 2].indexOf(column) > -1 ? {data: column} : null;
  122. }
  123. });
  124. expect(() => {
  125. hot.updateSettings({columns() {}});
  126. }).not.toThrow();
  127. });
  128. it('should not throw exception when passed columns function without return anything (data source as array of objects) when columns is a function', () => {
  129. var hot = handsontable({
  130. data: arrayOfObjects(),
  131. columns(column) {
  132. var keys = ['id', 'name', 'lasName'];
  133. return [0, 1, 2].indexOf(column) > -1 ? {data: keys[column]} : null;
  134. }
  135. });
  136. expect(() => {
  137. hot.updateSettings({columns() {}});
  138. }).not.toThrow();
  139. });
  140. });
  141. describe('editors', () => {
  142. it('should properly bind defined editors', () => {
  143. handsontable({
  144. data: [
  145. ['Joe'],
  146. ['Timothy'],
  147. ['Margaret'],
  148. ['Jerry']
  149. ],
  150. columns(column) {
  151. return column === 0 ? { editor: Handsontable.editors.PasswordEditor } : null;
  152. }
  153. });
  154. selectCell(0, 0);
  155. keyDown('enter');
  156. var editor = $('.handsontableInput');
  157. expect(editor.is(':visible')).toBe(true);
  158. expect(editor.is(':password')).toBe(true);
  159. });
  160. });
  161. describe('renderers', () => {
  162. it('should properly bind defined renderer', () => {
  163. handsontable({
  164. data: [[true], [false], [true]],
  165. columns(column) {
  166. return column === 0 ? { type: 'checkbox' } : null;
  167. }
  168. });
  169. expect($(getRenderedValue(0, 0)).is(':checkbox')).toBe(true);
  170. expect($(getRenderedValue(1, 0)).is(':checkbox')).toBe(true);
  171. expect($(getRenderedValue(2, 0)).is(':checkbox')).toBe(true);
  172. });
  173. });
  174. describe('validators', () => {
  175. it('should properly bind defined validator', (done) => {
  176. var onAfterValidate = jasmine.createSpy('onAfterValidate');
  177. handsontable({
  178. data: arrayOfObjects(),
  179. columns(column) {
  180. var settings = [
  181. {data: 'date', type: 'date'},
  182. {data: 'name'},
  183. {data: 'lastName'}
  184. ];
  185. return [0, 1, 2].indexOf(column) > -1 ? settings[column] : null;
  186. },
  187. afterValidate: onAfterValidate
  188. });
  189. setDataAtCell(0, 0, '');
  190. setTimeout(() => {
  191. expect(onAfterValidate).toHaveBeenCalledWith(true, '', 0, 'date', undefined, undefined);
  192. done();
  193. }, 100);
  194. });
  195. });
  196. });
  197. });
  198. });