a59c251daee44a0f71bb8f616aa7567c728552e1ef993dfaf0b1d9561ca1ccc2becfb26cf8b522cd1b4d871707fe52e771e2e6fb92ebcdea335a7e87defa62 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. describe('Core_getDataAt*', () => {
  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 arrayOfArrays = function() {
  13. return [
  14. ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
  15. ['2008', 10, 11, 12, 13],
  16. ['2009', 20, 11, 14, 13],
  17. ['2010', 30, 15, 12, 13]
  18. ];
  19. };
  20. var arrayOfObjects = function() {
  21. return [
  22. {
  23. 'id.a.b.c': 1,
  24. id: 1,
  25. name: 'Nannie Patel',
  26. address: 'Jenkin ville',
  27. details: {
  28. city: 'Chicago'
  29. },
  30. },
  31. {
  32. 'id.a.b.c': 2,
  33. id: 2,
  34. name: 'Łucja Grożny and Środeńczak',
  35. address: 'Gardiner',
  36. details: {
  37. city: 'New York'
  38. },
  39. },
  40. ];
  41. };
  42. it('should return data at specified row', () => {
  43. handsontable({
  44. data: arrayOfArrays()
  45. });
  46. expect(getDataAtRow(0)).toEqual(['', 'Kia', 'Nissan', 'Toyota', 'Honda']);
  47. });
  48. it('should return data at specified col', () => {
  49. handsontable({
  50. data: arrayOfArrays()
  51. });
  52. expect(getDataAtCol(1)).toEqual(['Kia', 10, 20, 30]);
  53. });
  54. describe('Core_getDataAtRowProp', () => {
  55. it('should return data at specified column', () => {
  56. handsontable({
  57. data: arrayOfObjects()
  58. });
  59. expect(getDataAtRowProp(1, 'id.a.b.c')).toBe(2);
  60. expect(getDataAtRowProp(1, 'id')).toBe(2);
  61. expect(getDataAtRowProp(1, 'id')).toBe(2);
  62. expect(getDataAtRowProp(1, 'details.city')).toBe('New York');
  63. });
  64. });
  65. describe('`modifyData` hook', () => {
  66. it('should be fired with specified arguments on every `set`, `get` operation (array of arrays)', () => {
  67. var spy = jasmine.createSpy();
  68. handsontable({
  69. data: arrayOfArrays(),
  70. autoColumnSize: false,
  71. modifyData: spy,
  72. });
  73. expect(spy.calls.count()).toBe(20); // call for all cells
  74. expect(spy.calls.argsFor(1)[0]).toBe(0);
  75. expect(spy.calls.argsFor(1)[1]).toBe(1);
  76. expect(spy.calls.argsFor(1)[2].value).toBe('Kia');
  77. expect(spy.calls.argsFor(1)[3]).toBe('get');
  78. spy.calls.reset();
  79. setDataAtCell(2, 3, 'foo');
  80. expect(spy.calls.count()).toBe(21); // call for all cells + 1 from setDataAtCell
  81. expect(spy.calls.argsFor(0)[0]).toBe(2);
  82. expect(spy.calls.argsFor(0)[1]).toBe(3);
  83. expect(spy.calls.argsFor(0)[2].value).toBe('foo');
  84. expect(spy.calls.argsFor(0)[3]).toBe('set');
  85. });
  86. it('should be fired with specified arguments on every `set`, `get` operation (array of objects)', () => {
  87. var spy = jasmine.createSpy();
  88. handsontable({
  89. data: arrayOfObjects(),
  90. autoColumnSize: false,
  91. modifyData: spy,
  92. });
  93. expect(spy.calls.count()).toBe(10); // call for all cells
  94. expect(spy.calls.argsFor(2)[0]).toBe(0);
  95. expect(spy.calls.argsFor(2)[1]).toBe(2);
  96. expect(spy.calls.argsFor(2)[2].value).toBe('Nannie Patel');
  97. expect(spy.calls.argsFor(2)[3]).toBe('get');
  98. spy.calls.reset();
  99. setDataAtRowProp(2, 'name', 'foo');
  100. expect(spy.calls.count()).toBe(16);
  101. expect(spy.calls.argsFor(0)[0]).toBe(2);
  102. expect(spy.calls.argsFor(0)[1]).toBe(2);
  103. expect(spy.calls.argsFor(0)[2].value).toBe('foo');
  104. expect(spy.calls.argsFor(0)[3]).toBe('set');
  105. });
  106. it('should overwrite value while loading data', () => {
  107. handsontable({
  108. data: arrayOfArrays(),
  109. modifyData(row, column, valueHolder, ioMode) {
  110. if (ioMode === 'get' && row === 1 && column === 2) {
  111. valueHolder.value = 'foo';
  112. }
  113. },
  114. });
  115. expect(getDataAtCell(1, 2)).toBe('foo');
  116. expect(getSourceDataAtCell(1, 2)).toBe(11);
  117. });
  118. it('should overwrite value while saving data', () => {
  119. handsontable({
  120. data: arrayOfArrays(),
  121. modifyData(row, column, valueHolder, ioMode) {
  122. if (ioMode === 'set' && row === 1 && column === 2) {
  123. valueHolder.value = 'foo';
  124. }
  125. },
  126. });
  127. setDataAtCell(1, 2, 'bar');
  128. expect(getDataAtCell(1, 2)).toBe('foo');
  129. expect(getSourceDataAtCell(1, 2)).toBe('foo');
  130. });
  131. });
  132. });