| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- describe('Core_populateFromArray', () => {
- var id = 'testContainer';
- beforeEach(function() {
- this.$container = $(`<div id="${id}"></div>`).appendTo('body');
- });
- afterEach(function() {
- if (this.$container) {
- destroy();
- this.$container.remove();
- }
- });
- var arrayOfArrays = function() {
- return [
- ['', 'Kia', 'Nissan', 'Toyota', 'Honda', 'Mix'],
- ['2008', 10, 11, 12, 13, {a: 1, b: 2}],
- ['2009', 20, 11, 14, 13, {a: 1, b: 2}],
- ['2010', 30, 15, 12, 13, {a: 1, b: 2}]
- ];
- };
- it('should call onChange callback', () => {
- var output = null;
- handsontable({
- data: arrayOfArrays(),
- afterChange(changes) {
- output = changes;
- }
- });
- populateFromArray(0, 0, [['test', 'test'], ['test', 'test']], 1, 1);
- expect(output).toEqual([[0, 0, '', 'test'], [0, 1, 'Kia', 'test'], [1, 0, '2008', 'test'], [1, 1, 10, 'test']]);
- });
- it('should populate single value for whole selection', () => {
- var output = null;
- handsontable({
- data: arrayOfArrays(),
- afterChange(changes) {
- output = changes;
- }
- });
- populateFromArray(0, 0, [['test']], 3, 0);
- expect(output).toEqual([[0, 0, '', 'test'], [1, 0, '2008', 'test'], [2, 0, '2009', 'test'], [3, 0, '2010', 'test']]);
- });
- it('should populate value for whole selection only if populated data isn\'t an array', () => {
- var output = null;
- handsontable({
- data: arrayOfArrays(),
- afterChange(changes) {
- output = changes;
- }
- });
- populateFromArray(0, 0, [['test'], [[1, 2, 3]]], 3, 0);
- expect(output).toEqual([[0, 0, '', 'test'], [2, 0, '2009', 'test']]);
- });
- it('should populate value for whole selection only if populated data isn\'t an object', () => {
- var output = null;
- handsontable({
- data: arrayOfArrays(),
- afterChange(changes) {
- output = changes;
- }
- });
- populateFromArray(0, 0, [['test'], [{test: 1}]], 3, 0);
- expect(output).toEqual([[0, 0, '', 'test'], [2, 0, '2009', 'test']]);
- });
- it('shouldn\'t populate value if original value doesn\'t have the same data structure', () => {
- var output = null;
- handsontable({
- data: arrayOfArrays(),
- afterChange(changes) {
- output = changes;
- }
- });
- populateFromArray(1, 3, [['test']], 1, 5);
- expect(output).toEqual([[1, 3, 12, 'test'], [1, 4, 13, 'test']]);
- });
- it('should shift values down', () => {
- var output = null;
- handsontable({
- data: arrayOfArrays(),
- afterChange(changes) {
- output = changes;
- },
- minSpareRows: 1
- });
- populateFromArray(0, 0, [['test', 'test2'], ['test3', 'test4']], 2, 2, null, 'shift_down');
- expect(getData()).toEqual([
- ['test', 'test2', 'test', 'Toyota', 'Honda', 'Mix'],
- ['test3', 'test4', 'test3', 12, 13, { a: 1, b: 2 }],
- ['test', 'test2', 'test', 14, 13, { a: 1, b: 2 }],
- ['', 'Kia', 'Nissan', 12, 13, { a: 1, b: 2 }],
- ['2008', 10, 11, null, null, null],
- ['2009', 20, 11, null, null, null],
- ['2010', 30, 15, null, null, null],
- [null, null, null, null, null, null]
- ]);
- });
- it('should shift values right', () => {
- var output = null;
- handsontable({
- data: arrayOfArrays(),
- afterChange(changes) {
- output = changes;
- },
- minSpareCols: 1
- });
- populateFromArray(0, 0, [['test', 'test2'], ['test3', 'test4']], 2, 2, null, 'shift_right');
- expect(getData()).toEqual([
- ['test', 'test2', 'test', '', 'Kia', 'Nissan', 'Toyota', 'Honda', 'Mix', null],
- ['test3', 'test4', 'test3', '2008', 10, {a: 1, b: 2}, 12, 13, null, null],
- ['test', 'test2', 'test', '2009', 20, {a: 1, b: 2}, 14, 13, null, null],
- ['2010', 30, 15, 12, 13, {a: 1, b: 2}, null, null, null, null]
- ]);
- });
- it('should run beforeAutofillInsidePopulate hook for each inserted value', () => {
- var called = 0;
- var hot = handsontable({
- data: arrayOfArrays()
- });
- hot.addHook('beforeAutofillInsidePopulate', (index) => {
- called++;
- });
- populateFromArray(0, 0, [['test', 'test2'], ['test3', 'test4']], 1, 1, 'Autofill.fill', 'overwrite');
- expect(called).toEqual(4);
- });
- it('should run beforeAutofillInsidePopulate hook and could change cell data before insert if returned object with value property', () => {
- var hot = handsontable({
- data: arrayOfArrays()
- });
- hot.addHook('beforeAutofillInsidePopulate', (index) => ({
- value: 'my_test'
- }));
- populateFromArray(0, 0, [['test', 'test2'], ['test3', 'test4']], 1, 1, 'Autofill.fill', 'overwrite');
- expect(getDataAtCell(0, 0)).toEqual('my_test');
- });
- it('should populate 1 row from 2 selected rows', () => {
- var hot = handsontable({
- data: arrayOfArrays()
- });
- populateFromArray(2, 0, [['A1'], ['A2']], 2, 0, 'autofill', null, 'down', [[0]]);
- expect(getDataAtCell(2, 0)).toEqual('A1');
- expect(getDataAtCell(3, 0)).toEqual('2010');
- });
- it('should populate 1 column from 2 selected columns`', () => {
- var hot = handsontable({
- data: arrayOfArrays()
- });
- populateFromArray(0, 2, [['A1', 'A2']], 0, 2, 'autofill', null, 'right', [[0]]);
- expect(getDataAtCell(0, 2)).toEqual('A1');
- expect(getDataAtCell(0, 3)).toEqual('Toyota');
- });
- });
|