Skip to content

Commit

Permalink
fix(export): expanded Row Detail shouldn't be exported
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Jun 23, 2021
1 parent d933829 commit b6299e4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,36 @@ describe('ExcelExportService', () => {
]
});
});

it(`should skip lines that have an empty Slick DataView structure like "getItem" that is null and is part of the item object`, async () => {
mockCollection = [
{ id: 0, user: { firstName: 'John', lastName: 'Z' }, position: 'SALES_REP', order: 10 },
{ id: 1, getItem: null, getItems: null, __parent: { id: 0, user: { firstName: 'John', lastName: 'Z' }, position: 'SALES_REP', order: 10 } }
];
jest.spyOn(dataViewStub, 'getLength').mockReturnValue(mockCollection.length);
jest.spyOn(dataViewStub, 'getItem').mockReturnValue(null).mockReturnValueOnce(mockCollection[0]).mockReturnValueOnce(mockCollection[1]);
const spyOnAfter = jest.spyOn(service.onGridAfterExportToExcel, 'next');
const spyUrlCreate = jest.spyOn(URL, 'createObjectURL');
const spyDownload = jest.spyOn(service, 'startDownloadFile');

const optionExpectation = { filename: 'export.xlsx', format: 'xlsx' };

service.init(gridStub, dataViewStub);
await service.exportToExcel(mockExportExcelOptions);

expect(spyOnAfter).toHaveBeenCalledWith(optionExpectation);
expect(spyUrlCreate).toHaveBeenCalledWith(mockExcelBlob);
expect(spyDownload).toHaveBeenCalledWith({
...optionExpectation, blob: new Blob(), data: [
[
{ metadata: { style: 1, }, value: 'First Name', },
{ metadata: { style: 1, }, value: 'Last Name', },
{ metadata: { style: 1, }, value: 'Position', },
],
['John', 'Z', 'SALES_REP'],
]
});
});
});

describe('with Translation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,31 @@ describe('ExportService', () => {
done();
});
});


it(`should skip lines that have an empty Slick DataView structure like "getItem" that is null and is part of the item object`, async () => {
mockCollection = [
{ id: 0, user: { firstName: 'John', lastName: 'Z' }, position: 'SALES_REP', order: 10 },
{ id: 1, getItem: null, getItems: null, __parent: { id: 0, user: { firstName: 'John', lastName: 'Z' }, position: 'SALES_REP', order: 10 } }
];
jest.spyOn(dataViewStub, 'getLength').mockReturnValue(mockCollection.length);
jest.spyOn(dataViewStub, 'getItem').mockReturnValue(null).mockReturnValueOnce(mockCollection[0]).mockReturnValueOnce(mockCollection[1]);
const spyOnAfter = jest.spyOn(service.onGridAfterExportToFile, 'next');
const spyUrlCreate = jest.spyOn(URL, 'createObjectURL');
const spyDownload = jest.spyOn(service, 'startDownloadFile');

const optionExpectation = { filename: 'export.csv', format: 'csv', useUtf8WithBom: false };
const contentExpectation =
`"First Name","Last Name","Position"
"John","Z","SALES_REP"`;

service.init(gridStub, dataViewStub);
await service.exportToFile(mockExportCsvOptions);

expect(spyOnAfter).toHaveBeenCalledWith(optionExpectation);
expect(spyUrlCreate).toHaveBeenCalledWith(mockCsvBlob);
expect(spyDownload).toHaveBeenCalledWith({ ...optionExpectation, content: removeMultipleSpaces(contentExpectation) });
});
});

describe('with Translation', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,10 @@ export class ExcelExportService {
// loop through all the grid rows of data
for (let rowNumber = 0; rowNumber < lineCount; rowNumber++) {
const itemObj = this._dataView.getItem(rowNumber);
if (itemObj) {

// make sure we have a filled object AND that the item doesn't include the "getItem" method
// this happen could happen with an opened Row Detail as it seems to include an empty Slick DataView (we'll just skip those lines)
if (itemObj && !itemObj.hasOwnProperty('getItem')) {
// Normal row (not grouped by anything) would have an ID which was predefined in the Grid Columns definition
if (itemObj[this.datasetIdName] !== null && itemObj[this.datasetIdName] !== undefined) {
// get regular row item data
Expand Down
5 changes: 4 additions & 1 deletion src/app/modules/angular-slickgrid/services/export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ export class ExportService {
// loop through all the grid rows of data
for (let rowNumber = 0; rowNumber < lineCount; rowNumber++) {
const itemObj = this._dataView.getItem(rowNumber);
if (itemObj) {

// make sure we have a filled object AND that the item doesn't include the "getItem" method
// this happen could happen with an opened Row Detail as it seems to include an empty Slick DataView (we'll just skip those lines)
if (itemObj && !itemObj.hasOwnProperty('getItem')) {
// Normal row (not grouped by anything) would have an ID which was predefined in the Grid Columns definition
if (itemObj[this.datasetIdName] !== null && itemObj[this.datasetIdName] !== undefined) {
// get regular row item data
Expand Down

0 comments on commit b6299e4

Please sign in to comment.