Core_setDataAtCell.spec.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. describe('Core_setDataAtCell', () => {
  2. const 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. const 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. const 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. const 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', async() => {
  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. await sleep(200);
  81. expect(countRows()).toEqual(10);
  82. expect(getDataAtCell(9, 0)).toEqual('6');
  83. });
  84. it('should paste not more cols than maxCols', (done) => {
  85. handsontable({
  86. minSpareCols: 1,
  87. minCols: 5,
  88. maxCols: 10,
  89. });
  90. selectCell(0, 4);
  91. triggerPaste('1\t2\t3\t4\t5\t6\t7\t8\t9\t10');
  92. setTimeout(() => {
  93. expect(countCols()).toEqual(10);
  94. expect(getDataAtCell(0, 9)).toEqual('6');
  95. done();
  96. }, 200);
  97. });
  98. it('should paste not more rows & cols than maxRows & maxCols', (done) => {
  99. handsontable({
  100. minSpareRows: 1,
  101. minSpareCols: 1,
  102. minRows: 5,
  103. minCols: 5,
  104. maxRows: 6,
  105. maxCols: 6,
  106. });
  107. selectCell(4, 4);
  108. triggerPaste('1\t2\t3\n4\t5\t6\n7\t8\t9');
  109. setTimeout(() => {
  110. expect(countRows()).toEqual(6);
  111. expect(countCols()).toEqual(6);
  112. expect(getDataAtCell(5, 5)).toEqual('5');
  113. done();
  114. }, 200);
  115. });
  116. // https://github.com/handsontable/handsontable/issues/250
  117. it('should create new rows when pasting into grid with object data source', (done) => {
  118. handsontable({
  119. data: arrayOfNestedObjects(),
  120. colHeaders: true,
  121. columns: [
  122. { data: 'id' },
  123. { data: 'name.last' },
  124. { data: 'name.first' }
  125. ],
  126. minSpareRows: 1,
  127. });
  128. selectCell(3, 0);
  129. triggerPaste('a\tb\tc\nd\te\tf\ng\th\ti');
  130. setTimeout(() => {
  131. expect(countRows()).toEqual(7);
  132. expect(getDataAtCell(5, 2)).toEqual('i');
  133. done();
  134. }, 200);
  135. });
  136. // https://handsontable.com/demo/datasources.html
  137. it('should work with functional data source', () => {
  138. handsontable({
  139. data: [
  140. model({ id: 1, name: 'Ted Right', address: '' }),
  141. model({ id: 2, name: 'Frank Honest', address: '' }),
  142. model({ id: 3, name: 'Joan Well', address: '' })
  143. ],
  144. dataSchema: model,
  145. startRows: 5,
  146. startCols: 3,
  147. colHeaders: ['ID', 'Name', 'Address'],
  148. columns: [
  149. { data: property('id') },
  150. { data: property('name') },
  151. { data: property('address') }
  152. ],
  153. minSpareRows: 1
  154. });
  155. function model(opts) {
  156. const _pub = {};
  157. const _priv = $.extend({
  158. id: undefined,
  159. name: undefined,
  160. address: undefined
  161. }, opts);
  162. _pub.attr = function(attr, val) {
  163. if (typeof val === 'undefined') {
  164. return _priv[attr];
  165. }
  166. _priv[attr] = val;
  167. return _pub;
  168. };
  169. return _pub;
  170. }
  171. function property(attr) {
  172. return function(row, value) {
  173. return row.attr(attr, value);
  174. };
  175. }
  176. expect(getDataAtCell(1, 1)).toEqual('Frank Honest');
  177. setDataAtCell(1, 1, 'Something Else');
  178. expect(getDataAtCell(1, 1)).toEqual('Something Else');
  179. });
  180. it('should accept changes array as 1st param and source as 2nd param', () => {
  181. let lastSource = '';
  182. handsontable({
  183. afterChange(changes, source) {
  184. lastSource = source;
  185. }
  186. });
  187. setDataAtCell([[0, 0, 'new value']], 'customSource');
  188. expect(getDataAtCell(0, 0)).toEqual('new value');
  189. expect(lastSource).toEqual('customSource');
  190. });
  191. it('should trigger `afterSetDataAtCell` hook with applied changes', () => {
  192. let _changes;
  193. let _source;
  194. handsontable({
  195. afterSetDataAtCell(changes, source) {
  196. _changes = changes;
  197. _source = source;
  198. }
  199. });
  200. setDataAtCell(0, 0, 'foo bar', 'customSource');
  201. expect(_changes).toEqual([[0, 0, null, 'foo bar']]);
  202. expect(_source).toBe('customSource');
  203. expect(getDataAtCell(0, 0)).toEqual('foo bar');
  204. });
  205. it('should modify value on the fly using `afterSetDataAtCell` hook', () => {
  206. handsontable({
  207. data: [['a', 'b', 'c'], [1, 2, 3]],
  208. afterSetDataAtCell(changes) {
  209. if (changes[0][3] === 'foo bar') {
  210. changes[0][3] = 'bar';
  211. }
  212. if (changes[0][3] === 22) {
  213. changes[0][3] = 33;
  214. }
  215. }
  216. });
  217. setDataAtCell(0, 0, 'foo bar', 'customSource');
  218. setDataAtCell(1, 2, 22, 'customSource');
  219. expect(getDataAtCell(0, 0)).toBe('bar');
  220. expect(getDataAtCell(1, 2)).toBe(33);
  221. expect(getData()).toEqual([['bar', 'b', 'c'], [1, 2, 33]]);
  222. });
  223. it('should trigger `afterSetDataAtRowProp` hook with applied changes', () => {
  224. let _changes;
  225. let _source;
  226. handsontable({
  227. columns: [{ data: 'name' }, { data: 'id' }],
  228. afterSetDataAtRowProp(changes, source) {
  229. _changes = changes;
  230. _source = source;
  231. }
  232. });
  233. setDataAtRowProp(0, 'name', 'foo bar', 'customSource');
  234. expect(_changes).toEqual([[0, 'name', void 0, 'foo bar']]);
  235. expect(_source).toBe('customSource');
  236. expect(getDataAtCell(0, 0)).toBe('foo bar');
  237. });
  238. it('should modify value on the fly using `afterSetDataAtRowProp` hook', () => {
  239. handsontable({
  240. data: [{ name: 'a', id: 1 }, { name: 'b', id: 2 }, { name: 'c', id: 3 }],
  241. columns: [{ data: 'name' }, { data: 'id' }],
  242. afterSetDataAtRowProp(changes) {
  243. if (changes[0][3] === 'foo bar') {
  244. changes[0][3] = 'bar';
  245. }
  246. if (changes[0][3] === 22) {
  247. changes[0][3] = 33;
  248. }
  249. }
  250. });
  251. setDataAtRowProp(0, 'name', 'foo bar', 'customSource');
  252. setDataAtRowProp(1, 'id', 22, 'customSource');
  253. expect(getDataAtRowProp(0, 'name')).toEqual('bar');
  254. expect(getDataAtRowProp(1, 'id')).toBe(33);
  255. expect(getData()).toEqual([['bar', 1], ['b', 33], ['c', 3]]);
  256. });
  257. });