9f2ef8e10e9138eff66ccfbb3c94823cbd58b93d56839576b785365b52e612f84cc90a1e3d0ed36443a95be1fd9e86cd9bedf15f6314a78fa3497a15aad64f 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. describe('Core.getSourceDataAtCell', () => {
  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. 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. var
  61. _pub = {},
  62. _priv = {
  63. id: undefined,
  64. name: undefined,
  65. address: undefined
  66. };
  67. for (var i in opts) {
  68. if (Object.prototype.hasOwnProperty.call(opts, i)) {
  69. _priv[i] = opts[i];
  70. }
  71. }
  72. _pub.attr = function(attr, val) {
  73. if (typeof val === 'undefined') {
  74. return _priv[attr];
  75. }
  76. _priv[attr] = val;
  77. return _pub;
  78. };
  79. return _pub;
  80. }
  81. function property(attr) {
  82. return function(row, value) {
  83. return row.attr(attr, value);
  84. };
  85. }
  86. expect(getSourceDataAtCell(1, 1)).toEqual('Frank Honest');
  87. });
  88. describe('`modifyRowData` hook', () => {
  89. it('should be possible to change data for row on the fly ', () => {
  90. handsontable({
  91. data: [
  92. ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
  93. ['2008', 10, 11, 12, 13],
  94. ['2009', 20, 11, 14, 13],
  95. ['2010', 30, 15, 12, 13]
  96. ],
  97. modifyRowData(row) {
  98. var newDataset = [];
  99. if (row === 1) {
  100. newDataset.push('2016', 0, 0, 0, 0);
  101. }
  102. return newDataset.length ? newDataset : void 0;
  103. }
  104. });
  105. expect(getSourceDataAtCell(1, 0)).toEqual('2016');
  106. });
  107. });
  108. });