getSourceDataAtCell.spec.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. describe('Core.getSourceDataAtCell', () => {
  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. it('should return null when is call without arguments', () => {
  13. handsontable({
  14. data: [[1, 2, 3], ['a', 'b', 'c']],
  15. });
  16. expect(getSourceDataAtCell()).toBeNull();
  17. });
  18. it('should return cell value when provided data was an array of arrays', () => {
  19. handsontable({
  20. data: [[1, 2, 3], ['a', 'b', 'c']],
  21. });
  22. expect(getSourceDataAtCell(1, 1)).toEqual('b');
  23. });
  24. it('should return cell value when provided data was an array of objects', () => {
  25. handsontable({
  26. data: [{ a: 1, b: 2, c: 3 }, { a: 'a', b: 'b', c: 'c' }],
  27. copyable: true
  28. });
  29. expect(getSourceDataAtCell(1, 'b')).toEqual('b');
  30. });
  31. it('should return cell value when provided data was an array of objects (nested structure)', () => {
  32. handsontable({
  33. data: [{ a: 1, b: { a: 21, b: 22 }, c: 3 }, { a: 'a', b: { a: 'ba', b: 'bb' }, c: 'c' }],
  34. columns: [
  35. { data: 'a' },
  36. { data: 'b.a' },
  37. { data: 'b.b' },
  38. { data: 'c' },
  39. ]
  40. });
  41. expect(getSourceDataAtCell(1, 'b.b')).toEqual('bb');
  42. });
  43. it('should return cell value when data is provided by dataSchema', () => {
  44. handsontable({
  45. data: [
  46. model({ id: 1, name: 'Ted Right', address: '' }),
  47. model({ id: 2, name: 'Frank Honest', address: '' }),
  48. model({ id: 3, name: 'Joan Well', address: '' }),
  49. model({ id: 4, name: 'Gail Polite', address: '' }),
  50. model({ id: 5, name: 'Michael Fair', address: '' })
  51. ],
  52. dataSchema: model,
  53. columns: [
  54. { data: property('id') },
  55. { data: property('name') },
  56. { data: property('address') }
  57. ]
  58. });
  59. function model(opts) {
  60. const _pub = {};
  61. const _priv = {
  62. id: undefined,
  63. name: undefined,
  64. address: undefined
  65. };
  66. Handsontable.helper.objectEach(opts, (value, key) => {
  67. _priv[key] = value;
  68. });
  69. _pub.attr = function(attr, val) {
  70. if (typeof val === 'undefined') {
  71. return _priv[attr];
  72. }
  73. _priv[attr] = val;
  74. return _pub;
  75. };
  76. return _pub;
  77. }
  78. function property(attr) {
  79. return function(row, value) {
  80. return row.attr(attr, value);
  81. };
  82. }
  83. expect(getSourceDataAtCell(1, 1)).toEqual('Frank Honest');
  84. });
  85. describe('`modifyRowData` hook', () => {
  86. it('should be possible to change data for row on the fly ', () => {
  87. handsontable({
  88. data: [
  89. ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
  90. ['2008', 10, 11, 12, 13],
  91. ['2009', 20, 11, 14, 13],
  92. ['2010', 30, 15, 12, 13]
  93. ],
  94. modifyRowData(row) {
  95. const newDataset = [];
  96. if (row === 1) {
  97. newDataset.push('2016', 0, 0, 0, 0);
  98. }
  99. return newDataset.length ? newDataset : void 0;
  100. }
  101. });
  102. expect(getSourceDataAtCell(1, 0)).toEqual('2016');
  103. });
  104. });
  105. });