e5c080b1624955ef890cd63d3129630811964409254cc0a7dce671b76dbf8141dc9dff49440854733362a74599f3b870ffb82df324f2bd2cc487778dd62bd0 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. describe('Core_keepEmptyRows', () => {
  2. var 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. var arrayOfNestedObjects = function() {
  13. return [
  14. {id: 1,
  15. name: {
  16. first: 'Ted',
  17. last: 'Right'
  18. },
  19. address: 'Street Name',
  20. zip: '80410',
  21. city: 'City Name'},
  22. {id: 2,
  23. name: {
  24. first: 'Frank',
  25. last: 'Honest'
  26. },
  27. address: 'Street Name',
  28. zip: '80410',
  29. city: 'City Name'},
  30. {id: 3,
  31. name: {
  32. first: 'Joan',
  33. last: 'Well'
  34. },
  35. address: 'Street Name',
  36. zip: '80410',
  37. city: 'City Name'}
  38. ];
  39. };
  40. it('should remove columns if needed', function() {
  41. handsontable({
  42. data: arrayOfNestedObjects(),
  43. columns: [
  44. {data: 'id'},
  45. {data: 'name.first'}
  46. ]
  47. });
  48. expect(this.$container.find('tbody tr:first td').length).toEqual(2);
  49. });
  50. it('should remove columns if needed when columns is a function', function() {
  51. handsontable({
  52. data: arrayOfNestedObjects(),
  53. columns(column) {
  54. var colMeta = {};
  55. if (column === 0) {
  56. colMeta.data = 'id';
  57. } else if (column === 1) {
  58. colMeta.data = 'name.first';
  59. } else {
  60. colMeta = null;
  61. }
  62. return colMeta;
  63. }
  64. });
  65. expect(this.$container.find('tbody tr:first td').length).toEqual(2);
  66. });
  67. it('should create columns if needed', function() {
  68. handsontable({
  69. data: arrayOfNestedObjects(),
  70. columns: [
  71. {data: 'id'},
  72. {data: 'name.first'},
  73. {data: 'name.last'},
  74. {data: 'address'},
  75. {data: 'zip'},
  76. {data: 'city'}
  77. ]
  78. });
  79. expect(this.$container.find('tbody tr:first td').length).toEqual(6);
  80. });
  81. it('should create columns if needed when columns is a function', function() {
  82. handsontable({
  83. data: arrayOfNestedObjects(),
  84. columns(column) {
  85. var colMeta = {};
  86. if (column === 0) {
  87. colMeta.data = 'id';
  88. } else if (column === 1) {
  89. colMeta.data = 'name.first';
  90. } else if (column === 2) {
  91. colMeta.data = 'name.last';
  92. } else if (column === 3) {
  93. colMeta.data = 'address';
  94. } else if (column === 4) {
  95. colMeta.data = 'zip';
  96. } else if (column === 5) {
  97. colMeta.data = 'city';
  98. } else {
  99. colMeta = null;
  100. }
  101. return colMeta;
  102. }
  103. });
  104. expect(this.$container.find('tbody tr:first td').length).toEqual(6);
  105. });
  106. it('should create spare cols and rows on init (array data source)', () => {
  107. handsontable({
  108. data: [
  109. ['one', 'two'],
  110. ['three', 'four']
  111. ],
  112. minCols: 4,
  113. minRows: 4,
  114. minSpareRows: 4,
  115. minSpareCols: 4
  116. });
  117. expect(countCells()).toEqual(36);
  118. });
  119. it('should create spare cols and rows on init (object data source)', function() {
  120. handsontable({
  121. data: arrayOfNestedObjects(),
  122. minRows: 4,
  123. minSpareRows: 1
  124. });
  125. expect(countRows()).toEqual(4);
  126. expect(countCols()).toEqual(6); // because arrayOfNestedObjects has 6 nested properites and they should be figured out if dataSchema/columns is not given
  127. expect(this.$container.find('tbody tr:first td:last').text()).toEqual('City Name');
  128. });
  129. it('should create new row when last cell in last row is edited', () => {
  130. var data = [
  131. ['one', 'two'],
  132. ['three', 'four']
  133. ];
  134. handsontable({
  135. data,
  136. minRows: 4,
  137. minCols: 4,
  138. minSpareRows: 1
  139. });
  140. setDataAtCell(3, 3, 'test');
  141. expect(data.length).toEqual(5);
  142. });
  143. it('should create new col when last cell in last row is edited', () => {
  144. var data = [
  145. ['one', 'two'],
  146. ['three', 'four']
  147. ];
  148. handsontable({
  149. data,
  150. minRows: 4,
  151. minCols: 4,
  152. minSpareCols: 1
  153. });
  154. setDataAtCell(3, 3, 'test');
  155. expect(countCols()).toEqual(5);
  156. });
  157. it('should create new row when last cell in last row is edited by autocomplete', (done) => {
  158. var data = [
  159. {id: 1, color: 'orange' }
  160. ];
  161. var syncSources = jasmine.createSpy('syncSources');
  162. syncSources.and.callFake((query, process) => {
  163. process(['red', 'dark-yellow', 'yellow', 'light-yellow', 'black']);
  164. });
  165. handsontable({
  166. data,
  167. startRows: 5,
  168. colHeaders: true,
  169. minSpareRows: 1,
  170. columns: [
  171. {data: 'id', type: 'text'},
  172. {
  173. data: 'color',
  174. editor: 'autocomplete',
  175. source: syncSources
  176. }
  177. ]
  178. });
  179. selectCell(1, 1);
  180. keyDownUp('enter');
  181. setTimeout(() => {
  182. keyDown('arrow_down');
  183. keyDownUp('enter');
  184. expect(data.length).toEqual(3);
  185. done();
  186. }, 200);
  187. });
  188. it('should create new row when last cell in last row is edited by autocomplete when columns is a function', (done) => {
  189. var data = [
  190. {id: 1, color: 'orange' }
  191. ];
  192. var syncSources = jasmine.createSpy('syncSources');
  193. syncSources.and.callFake((query, process) => {
  194. process(['red', 'dark-yellow', 'yellow', 'light-yellow', 'black']);
  195. });
  196. handsontable({
  197. data,
  198. startRows: 5,
  199. colHeaders: true,
  200. minSpareRows: 1,
  201. columns(column) {
  202. var colMeta = {};
  203. if (column === 0) {
  204. colMeta.data = 'id';
  205. colMeta.type = 'text';
  206. } else if (column === 1) {
  207. colMeta.data = 'color';
  208. colMeta.editor = 'autocomplete';
  209. colMeta.source = syncSources;
  210. } else {
  211. colMeta = null;
  212. }
  213. return colMeta;
  214. }
  215. });
  216. selectCell(1, 1);
  217. keyDownUp('enter');
  218. setTimeout(() => {
  219. keyDown('arrow_down');
  220. keyDownUp('enter');
  221. expect(data.length).toEqual(3);
  222. done();
  223. }, 200);
  224. });
  225. it('should not create more rows that maxRows', () => {
  226. handsontable({
  227. startRows: 4,
  228. maxRows: 6,
  229. minSpareRows: 1
  230. });
  231. setDataAtCell(3, 0, 'test');
  232. setDataAtCell(4, 0, 'test');
  233. setDataAtCell(5, 0, 'test');
  234. expect(countRows()).toEqual(6);
  235. });
  236. it('should not create more cols that maxCols', () => {
  237. handsontable({
  238. startCols: 4,
  239. maxCols: 6,
  240. minSpareCols: 1
  241. });
  242. setDataAtCell(0, 3, 'test');
  243. setDataAtCell(0, 4, 'test');
  244. setDataAtCell(0, 5, 'test');
  245. expect(countCols()).toEqual(6);
  246. });
  247. it('should ignore minCols if columns is set', () => {
  248. handsontable({
  249. startCols: 1,
  250. minCols: 6,
  251. columns: [
  252. {},
  253. {}
  254. ]
  255. });
  256. expect(countCols()).toEqual(2);
  257. });
  258. it('should ignore minCols if columns is set when columns is a function', () => {
  259. handsontable({
  260. startCols: 1,
  261. minCols: 6,
  262. columns(column) {
  263. var colMeta = {};
  264. if ([0, 1].indexOf(column) < 0) {
  265. colMeta = null;
  266. }
  267. return colMeta;
  268. }
  269. });
  270. expect(countCols()).toEqual(1);
  271. });
  272. it('columns should have priority over startCols', () => {
  273. handsontable({
  274. startCols: 3,
  275. minCols: 6,
  276. columns: [
  277. {},
  278. {}
  279. ]
  280. });
  281. expect(countCols()).toEqual(2);
  282. });
  283. it('columns should have priority over startCols when columns is a function', () => {
  284. handsontable({
  285. startCols: 3,
  286. minCols: 6,
  287. columns(column) {
  288. var colMeta = {};
  289. if ([0, 1].indexOf(column) < 0) {
  290. colMeta = null;
  291. }
  292. return colMeta;
  293. }
  294. });
  295. expect(countCols()).toEqual(2);
  296. });
  297. });