3b13d9792b0177caf37810e944811aab2e13c27710e07222191c683f200f44a22371ac64b22c54ee6c6ab00450b91748013b4f23600542c6511f479dbf00f8 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. describe('Core_setDataAtCell', () => {
  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. {id: 2,
  20. name: {
  21. first: 'Frank',
  22. last: 'Honest'
  23. }},
  24. {id: 3,
  25. name: {
  26. first: 'Joan',
  27. last: 'Well'
  28. }}
  29. ];
  30. };
  31. var htmlText = 'Ben & Jerry\'s';
  32. it('HTML special chars should be preserved in data map but escaped in DOM', () => {
  33. // https://github.com/handsontable/handsontable/issues/147
  34. handsontable();
  35. var td = setDataAtCell(0, 0, htmlText);
  36. selectCell(0, 0);
  37. $(td).simulate('dblclick');
  38. deselectCell();
  39. expect(getDataAtCell(0, 0)).toEqual(htmlText);
  40. });
  41. it('should correctly paste string that contains "quotes"', (done) => {
  42. // https://github.com/handsontable/handsontable/issues/205
  43. handsontable({});
  44. selectCell(0, 0);
  45. triggerPaste('1\nThis is a "test" and a test\n2');
  46. setTimeout(() => {
  47. expect(getDataAtCell(0, 0)).toEqual('1');
  48. expect(getDataAtCell(1, 0)).toEqual('This is a "test" and a test');
  49. expect(getDataAtCell(2, 0)).toEqual('2');
  50. done();
  51. }, 200);
  52. });
  53. it('should correctly paste string when dataSchema is used', (done) => {
  54. // https://github.com/handsontable/handsontable/issues/237
  55. handsontable({
  56. colHeaders: true,
  57. dataSchema: {
  58. col1: null,
  59. col2: null,
  60. col3: null
  61. }
  62. });
  63. selectCell(0, 0);
  64. triggerPaste('1\tTest\t2');
  65. setTimeout(() => {
  66. expect(getDataAtCell(0, 0)).toEqual('1');
  67. expect(getDataAtCell(0, 1)).toEqual('Test');
  68. expect(getDataAtCell(0, 2)).toEqual('2');
  69. done();
  70. }, 200);
  71. });
  72. it('should paste not more rows than maxRows', (done) => {
  73. handsontable({
  74. minSpareRows: 1,
  75. minRows: 5,
  76. maxRows: 10,
  77. });
  78. selectCell(4, 0);
  79. triggerPaste('1\n2\n3\n4\n5\n6\n7\n8\n9\n10');
  80. setTimeout(() => {
  81. expect(countRows()).toEqual(10);
  82. expect(getDataAtCell(9, 0)).toEqual('6');
  83. done();
  84. }, 200);
  85. });
  86. it('should paste not more cols than maxCols', (done) => {
  87. handsontable({
  88. minSpareCols: 1,
  89. minCols: 5,
  90. maxCols: 10,
  91. });
  92. selectCell(0, 4);
  93. triggerPaste('1\t2\t3\t4\t5\t6\t7\t8\t9\t10');
  94. setTimeout(() => {
  95. expect(countCols()).toEqual(10);
  96. expect(getDataAtCell(0, 9)).toEqual('6');
  97. done();
  98. }, 200);
  99. });
  100. it('should paste not more rows & cols than maxRows & maxCols', (done) => {
  101. handsontable({
  102. minSpareRows: 1,
  103. minSpareCols: 1,
  104. minRows: 5,
  105. minCols: 5,
  106. maxRows: 6,
  107. maxCols: 6,
  108. });
  109. selectCell(4, 4);
  110. triggerPaste('1\t2\t3\n4\t5\t6\n7\t8\t9');
  111. setTimeout(() => {
  112. expect(countRows()).toEqual(6);
  113. expect(countCols()).toEqual(6);
  114. expect(getDataAtCell(5, 5)).toEqual('5');
  115. done();
  116. }, 200);
  117. });
  118. // https://github.com/handsontable/handsontable/issues/250
  119. it('should create new rows when pasting into grid with object data source', (done) => {
  120. handsontable({
  121. data: arrayOfNestedObjects(),
  122. colHeaders: true,
  123. columns: [
  124. {data: 'id'},
  125. {data: 'name.last'},
  126. {data: 'name.first'}
  127. ],
  128. minSpareRows: 1,
  129. });
  130. selectCell(3, 0);
  131. triggerPaste('a\tb\tc\nd\te\tf\ng\th\ti');
  132. setTimeout(() => {
  133. expect(countRows()).toEqual(7);
  134. expect(getDataAtCell(5, 2)).toEqual('i');
  135. done();
  136. }, 200);
  137. });
  138. // https://handsontable.com/demo/datasources.html
  139. it('should work with functional data source', () => {
  140. handsontable({
  141. data: [
  142. model({id: 1, name: 'Ted Right', address: ''}),
  143. model({id: 2, name: 'Frank Honest', address: ''}),
  144. model({id: 3, name: 'Joan Well', address: ''})
  145. ],
  146. dataSchema: model,
  147. startRows: 5,
  148. startCols: 3,
  149. colHeaders: ['ID', 'Name', 'Address'],
  150. columns: [
  151. {data: property('id')},
  152. {data: property('name')},
  153. {data: property('address')}
  154. ],
  155. minSpareRows: 1
  156. });
  157. function model(opts) {
  158. var _pub = {},
  159. _priv = $.extend({
  160. id: undefined,
  161. name: undefined,
  162. address: undefined
  163. }, opts);
  164. _pub.attr = function(attr, val) {
  165. if (typeof val === 'undefined') {
  166. return _priv[attr];
  167. }
  168. _priv[attr] = val;
  169. return _pub;
  170. };
  171. return _pub;
  172. }
  173. function property(attr) {
  174. return function(row, value) {
  175. return row.attr(attr, value);
  176. };
  177. }
  178. expect(getDataAtCell(1, 1)).toEqual('Frank Honest');
  179. setDataAtCell(1, 1, 'Something Else');
  180. expect(getDataAtCell(1, 1)).toEqual('Something Else');
  181. });
  182. it('should accept changes array as 1st param and source as 2nd param', () => {
  183. var callCount = 0,
  184. lastSource = '';
  185. handsontable({
  186. afterChange(changes, source) {
  187. callCount++;
  188. lastSource = source;
  189. }
  190. });
  191. setDataAtCell([[0, 0, 'new value']], 'customSource');
  192. expect(getDataAtCell(0, 0)).toEqual('new value');
  193. expect(lastSource).toEqual('customSource');
  194. });
  195. it('should trigger `afterSetDataAtCell` hook with applied changes', () => {
  196. var _changes;
  197. var _source;
  198. handsontable({
  199. afterSetDataAtCell(changes, source) {
  200. _changes = changes;
  201. _source = source;
  202. }
  203. });
  204. setDataAtCell(0, 0, 'foo bar', 'customSource');
  205. expect(_changes).toEqual([[0, 0, null, 'foo bar']]);
  206. expect(_source).toBe('customSource');
  207. expect(getDataAtCell(0, 0)).toEqual('foo bar');
  208. });
  209. it('should modify value on the fly using `afterSetDataAtCell` hook', () => {
  210. handsontable({
  211. data: [['a', 'b', 'c'], [1, 2, 3]],
  212. afterSetDataAtCell(changes, source) {
  213. if (changes[0][3] === 'foo bar') {
  214. changes[0][3] = 'bar';
  215. }
  216. if (changes[0][3] === 22) {
  217. changes[0][3] = 33;
  218. }
  219. }
  220. });
  221. setDataAtCell(0, 0, 'foo bar', 'customSource');
  222. setDataAtCell(1, 2, 22, 'customSource');
  223. expect(getDataAtCell(0, 0)).toBe('bar');
  224. expect(getDataAtCell(1, 2)).toBe(33);
  225. expect(getData()).toEqual([['bar', 'b', 'c'], [1, 2, 33]]);
  226. });
  227. it('should trigger `afterSetDataAtRowProp` hook with applied changes', () => {
  228. var _changes;
  229. var _source;
  230. handsontable({
  231. columns: [{data: 'name'}, {data: 'id'}],
  232. afterSetDataAtRowProp(changes, source) {
  233. _changes = changes;
  234. _source = source;
  235. }
  236. });
  237. setDataAtRowProp(0, 'name', 'foo bar', 'customSource');
  238. expect(_changes).toEqual([[0, 'name', void 0, 'foo bar']]);
  239. expect(_source).toBe('customSource');
  240. expect(getDataAtCell(0, 0)).toBe('foo bar');
  241. });
  242. it('should modify value on the fly using `afterSetDataAtRowProp` hook', () => {
  243. handsontable({
  244. data: [{name: 'a', id: 1}, {name: 'b', id: 2}, {name: 'c', id: 3}],
  245. columns: [{data: 'name'}, {data: 'id'}],
  246. afterSetDataAtRowProp(changes, source) {
  247. if (changes[0][3] === 'foo bar') {
  248. changes[0][3] = 'bar';
  249. }
  250. if (changes[0][3] === 22) {
  251. changes[0][3] = 33;
  252. }
  253. }
  254. });
  255. setDataAtRowProp(0, 'name', 'foo bar', 'customSource');
  256. setDataAtRowProp(1, 'id', 22, 'customSource');
  257. expect(getDataAtRowProp(0, 'name')).toEqual('bar');
  258. expect(getDataAtRowProp(1, 'id')).toBe(33);
  259. expect(getData()).toEqual([['bar', 1], ['b', 33], ['c', 3]]);
  260. });
  261. });