Skip to content

Commit

Permalink
fix: do not create empty object rows, use newRowCreator instead
Browse files Browse the repository at this point in the history
  • Loading branch information
zewa666 committed Sep 2, 2024
1 parent 77b89c6 commit a170560
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -642,14 +642,17 @@ describe('CellExternalCopyManager', () => {
});

it('should Copy, Paste and run Execute clip command', (done) => {
const mockNewRowCreator = jest.fn();
let nextAddNewRowId = 0;
const mockNewRowCreator = jest.fn((count: number) => {
for (let i = 0; i < count; i++) {
gridStub.getData<SlickDataView>().addItem({
id: nextAddNewRowId--
});
}
});
const mockOnPasteCells = jest.fn();
const renderSpy = jest.spyOn(gridStub, 'render');
const setDataSpy = jest.spyOn(gridStub, 'setData');
jest.spyOn(gridStub.getSelectionModel() as SelectionModel, 'getSelectedRanges').mockReturnValueOnce([new SlickRange(0, 1, 2, 2)]).mockReturnValueOnce(null as any);
let clipCommand;
const clipboardCommandHandler = (cmd) => {
clipCommand = cmd;
cmd.execute();
};
plugin.init(gridStub, { clearCopySelectionDelay: 1, clipboardPasteDelay: 1, includeHeaderWhenCopying: true, clipboardCommandHandler, newRowCreator: mockNewRowCreator, onPasteCells: mockOnPasteCells });
Expand All @@ -670,21 +673,12 @@ describe('CellExternalCopyManager', () => {
Object.defineProperty(keyDownCtrlPasteEvent, 'isImmediatePropagationStopped', { writable: true, configurable: true, value: jest.fn() });
gridStub.onKeyDown.notify({ cell: 0, row: 0, grid: gridStub }, keyDownCtrlPasteEvent, gridStub);
document.querySelector('textarea')!.value = `Doe\tserialized output`;

setTimeout(() => {
expect(getActiveCellSpy).toHaveBeenCalled();
expect(renderSpy).toHaveBeenCalled();
expect(setDataSpy).toHaveBeenCalledWith([{ firstName: 'John', lastName: 'Doe', age: 30 }, { firstName: 'Jane', lastName: 'Doe' }, {}, {}]);
expect(mockNewRowCreator).toHaveBeenCalled();

const getDataItemSpy = jest.spyOn(gridStub, 'getDataItem');
const setData2Spy = jest.spyOn(gridStub, 'setData');
const render2Spy = jest.spyOn(gridStub, 'render');
clipCommand.undo();
expect(getDataItemSpy).toHaveBeenCalled();
expect(setData2Spy).toHaveBeenCalledWith([{ firstName: 'John', lastName: 'Doe', age: 30 }, { firstName: 'Jane', lastName: 'Doe' }]);
expect(render2Spy).toHaveBeenCalled();
expect(mockOnPasteCells).toHaveBeenCalledWith(expect.toBeObject(), { ranges: [new SlickRange(3, 0, 3, 1)] });
done();
}, 2);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,7 @@ export class SlickCellExternalCopyManager {

// ignore new rows if we don't have a "newRowCreator"
if ((availableRows < destH) && typeof this._addonOptions.newRowCreator === 'function') {
const d = this._dataWrapper.getDataItems();
for (addRows = 1; addRows <= (destH - availableRows); addRows++) {
d.push({});
}
this._dataWrapper.setDataItems(d);
this._addonOptions.newRowCreator(destH - availableRows);
this._grid.render();
}

Expand Down

0 comments on commit a170560

Please sign in to comment.