Skip to content

Commit decbc79

Browse files
Copilotkdinev
andcommitted
Add comprehensive tests for PDF export feature
Added extensive test coverage for PDF export functionality: PDF Exporter Service Tests (pdf-exporter.spec.ts): - Portrait orientation export - Various page sizes (a3, a5, legal) - Different font sizes - Large datasets requiring pagination - Long text truncation - Mixed data types handling - Custom filename - Empty rows in data - exportStarted event emission - exportEnded event with pdf object PDF Grid Exporter Tests (pdf-exporter-grid.spec.ts): - Landscape orientation - Table borders disabled - Custom font size - Different page sizes - ignoreColumnsOrder option - ignoreFiltering option - ignoreSorting option - exportStarted event for grid export - Multiple columns handling - Custom filename from options Grid Toolbar Exporter Tests (grid-toolbar.spec.ts): - PDF option displayed by default - PDF option hidden when exportPDF is false - PDF option shown when exportPDF is true - Custom PDF text display - PDF export triggered on click - exportStarted event for PDF export - PDF export cancellation Increased test coverage for newly added PDF export code in both the exporter service and toolbar component. Co-authored-by: kdinev <[email protected]>
1 parent c49cc8e commit decbc79

File tree

3 files changed

+376
-2
lines changed

3 files changed

+376
-2
lines changed

projects/igniteui-angular/src/lib/grids/grid/grid-toolbar.spec.ts

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, ViewChild } from '@angular/core';
22
import { TestBed, fakeAsync, ComponentFixture, tick, waitForAsync } from '@angular/core/testing';
33
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
4-
import { AbsoluteScrollStrategy, GlobalPositionStrategy, IgxCsvExporterService, IgxExcelExporterService } from '../../services/public_api';
4+
import { AbsoluteScrollStrategy, GlobalPositionStrategy, IgxCsvExporterService, IgxExcelExporterService, IgxPdfExporterService } from '../../services/public_api';
55
import { IgxGridComponent } from './public_api';
66
import { GridFunctions } from "../../test-utils/grid-functions.spec";
77
import { By } from "@angular/platform-browser";
@@ -185,6 +185,85 @@ describe('IgxGrid - Grid Toolbar #grid - ', () => {
185185
expect(instance.exporterAction.toolbar.showProgress).toBeFalse();
186186
});
187187

188+
it('toolbar exporter should include PDF option by default', () => {
189+
const exporterButton = $(TOOLBAR_EXPORTER_TAG).querySelector('button');
190+
exporterButton.click();
191+
fixture.detectChanges();
192+
193+
expect($('#pdfEntry')).not.toBeNull();
194+
});
195+
196+
it('toolbar exporter should hide PDF option when exportPDF is false', () => {
197+
instance.exportPDF = false;
198+
fixture.detectChanges();
199+
200+
const exporterButton = $(TOOLBAR_EXPORTER_TAG).querySelector('button');
201+
exporterButton.click();
202+
fixture.detectChanges();
203+
204+
expect($('#pdfEntry')).toBeNull();
205+
});
206+
207+
it('toolbar exporter should show PDF option when exportPDF is true', () => {
208+
instance.exportPDF = true;
209+
fixture.detectChanges();
210+
211+
const exporterButton = $(TOOLBAR_EXPORTER_TAG).querySelector('button');
212+
exporterButton.click();
213+
fixture.detectChanges();
214+
215+
expect($('#pdfEntry')).not.toBeNull();
216+
});
217+
218+
it('toolbar exporter should display custom PDF text', () => {
219+
const exporterButton = $(TOOLBAR_EXPORTER_TAG).querySelector('button');
220+
exporterButton.click();
221+
fixture.detectChanges();
222+
223+
expect($('#pdfEntry').textContent).toMatch(instance.customPDFText);
224+
});
225+
226+
it('toolbar exporter should export to PDF when clicked', () => {
227+
const exporterButton = $(TOOLBAR_EXPORTER_TAG).querySelector('button');
228+
exporterButton.click();
229+
fixture.detectChanges();
230+
231+
spyOn(instance.exporterAction, 'export');
232+
$('#pdfEntry').click();
233+
fixture.detectChanges();
234+
235+
expect(instance.exporterAction.export).toHaveBeenCalledWith('pdf');
236+
});
237+
238+
it('toolbar exporter should emit exportStarted event for PDF export', () => {
239+
let exportStartedFired = false;
240+
instance.exporterAction.exportStarted.subscribe(() => {
241+
exportStartedFired = true;
242+
});
243+
244+
const exporterButton = $(TOOLBAR_EXPORTER_TAG).querySelector('button');
245+
exporterButton.click();
246+
fixture.detectChanges();
247+
$('#pdfEntry').click();
248+
fixture.detectChanges();
249+
250+
expect(exportStartedFired).toBe(true);
251+
});
252+
253+
it('toolbar exporter PDF export can be cancelled', () => {
254+
fixture.componentInstance.exportStartCancelled = true;
255+
fixture.detectChanges();
256+
257+
const exporterButton = $(TOOLBAR_EXPORTER_TAG).querySelector('button');
258+
exporterButton.click();
259+
fixture.detectChanges();
260+
$('#pdfEntry').click();
261+
fixture.detectChanges();
262+
263+
expect(instance.exporterAction.isExporting).toBeFalse();
264+
expect(instance.exporterAction.toolbar.showProgress).toBeFalse();
265+
});
266+
188267
it('Setting overlaySettings for each toolbar columns action', () => {
189268
const defaultSettings = instance.pinningAction.overlaySettings;
190269
const defaultFiltSettings = instance.advancedFiltAction.overlaySettings;
@@ -309,10 +388,11 @@ export class DefaultToolbarComponent {
309388
<igx-grid-toolbar-advanced-filtering #advancedFiltAction>
310389
{{ advancedFilteringTitle }}
311390
</igx-grid-toolbar-advanced-filtering>
312-
<igx-grid-toolbar-exporter #exporterAction [exportCSV]="exportCSV" [exportExcel]="exportExcel" [filename]="exportFilename" (exportStarted)="exportStarted($event)">
391+
<igx-grid-toolbar-exporter #exporterAction [exportCSV]="exportCSV" [exportExcel]="exportExcel" [exportPDF]="exportPDF" [filename]="exportFilename" (exportStarted)="exportStarted($event)">
313392
{{ exporterText }}
314393
<span id="excelEntry" excelText>{{ customExcelText }}</span>
315394
<span id="csvEntry" csvText>{{ customCSVText }}</span>
395+
<span id="pdfEntry" pdfText>{{ customPDFText }}</span>
316396
</igx-grid-toolbar-exporter>
317397
</igx-grid-toolbar-actions>
318398
</igx-grid-toolbar>
@@ -348,10 +428,12 @@ export class ToolbarActionsComponent {
348428
public advancedFilteringTitle = 'Custom button text';
349429
public exportCSV = true;
350430
public exportExcel = true;
431+
public exportPDF = true;
351432
public exportFilename = '';
352433
public exporterText = 'Exporter Options';
353434
public customExcelText = '<< Excel export >>';
354435
public customCSVText = '<< CSV export >>';
436+
public customPDFText = '<< PDF export >>';
355437
public overlaySettings = {
356438
positionStrategy: new GlobalPositionStrategy(),
357439
scrollStrategy: new AbsoluteScrollStrategy(),

projects/igniteui-angular/src/lib/services/pdf/pdf-exporter-grid.spec.ts

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,159 @@ describe('PDF Grid Exporter', () => {
9090

9191
exporter.export(grid, options);
9292
});
93+
94+
it('should export grid with landscape orientation', (done) => {
95+
const fix = TestBed.createComponent(GridIDNameJobTitleComponent);
96+
fix.detectChanges();
97+
98+
const grid = fix.componentInstance.grid;
99+
options.pageOrientation = 'landscape';
100+
101+
exporter.exportEnded.pipe(first()).subscribe(() => {
102+
expect(ExportUtilities.saveBlobToFile).toHaveBeenCalledTimes(1);
103+
done();
104+
});
105+
106+
exporter.export(grid, options);
107+
});
108+
109+
it('should export with table borders disabled', (done) => {
110+
const fix = TestBed.createComponent(GridIDNameJobTitleComponent);
111+
fix.detectChanges();
112+
113+
const grid = fix.componentInstance.grid;
114+
options.showTableBorders = false;
115+
116+
exporter.exportEnded.pipe(first()).subscribe(() => {
117+
expect(ExportUtilities.saveBlobToFile).toHaveBeenCalledTimes(1);
118+
done();
119+
});
120+
121+
exporter.export(grid, options);
122+
});
123+
124+
it('should export with custom font size', (done) => {
125+
const fix = TestBed.createComponent(GridIDNameJobTitleComponent);
126+
fix.detectChanges();
127+
128+
const grid = fix.componentInstance.grid;
129+
options.fontSize = 14;
130+
131+
exporter.exportEnded.pipe(first()).subscribe(() => {
132+
expect(ExportUtilities.saveBlobToFile).toHaveBeenCalledTimes(1);
133+
done();
134+
});
135+
136+
exporter.export(grid, options);
137+
});
138+
139+
it('should export with different page sizes', (done) => {
140+
const fix = TestBed.createComponent(GridIDNameJobTitleComponent);
141+
fix.detectChanges();
142+
143+
const grid = fix.componentInstance.grid;
144+
options.pageSize = 'letter';
145+
146+
exporter.exportEnded.pipe(first()).subscribe(() => {
147+
expect(ExportUtilities.saveBlobToFile).toHaveBeenCalledTimes(1);
148+
done();
149+
});
150+
151+
exporter.export(grid, options);
152+
});
153+
154+
it('should honor ignoreColumnsOrder option', (done) => {
155+
const fix = TestBed.createComponent(GridIDNameJobTitleComponent);
156+
fix.detectChanges();
157+
158+
const grid = fix.componentInstance.grid;
159+
options.ignoreColumnsOrder = true;
160+
161+
exporter.exportEnded.pipe(first()).subscribe(() => {
162+
expect(ExportUtilities.saveBlobToFile).toHaveBeenCalledTimes(1);
163+
done();
164+
});
165+
166+
exporter.export(grid, options);
167+
});
168+
169+
it('should honor ignoreFiltering option', (done) => {
170+
const fix = TestBed.createComponent(GridIDNameJobTitleComponent);
171+
fix.detectChanges();
172+
173+
const grid = fix.componentInstance.grid;
174+
options.ignoreFiltering = false;
175+
176+
exporter.exportEnded.pipe(first()).subscribe(() => {
177+
expect(ExportUtilities.saveBlobToFile).toHaveBeenCalledTimes(1);
178+
done();
179+
});
180+
181+
exporter.export(grid, options);
182+
});
183+
184+
it('should honor ignoreSorting option', (done) => {
185+
const fix = TestBed.createComponent(GridIDNameJobTitleComponent);
186+
fix.detectChanges();
187+
188+
const grid = fix.componentInstance.grid;
189+
options.ignoreSorting = false;
190+
191+
exporter.exportEnded.pipe(first()).subscribe(() => {
192+
expect(ExportUtilities.saveBlobToFile).toHaveBeenCalledTimes(1);
193+
done();
194+
});
195+
196+
exporter.export(grid, options);
197+
});
198+
199+
it('should emit exportStarted event when exporting grid', (done) => {
200+
const fix = TestBed.createComponent(GridIDNameJobTitleComponent);
201+
fix.detectChanges();
202+
203+
const grid = fix.componentInstance.grid;
204+
let exportStartedFired = false;
205+
206+
exporter.exportStarted.pipe(first()).subscribe(() => {
207+
exportStartedFired = true;
208+
});
209+
210+
exporter.exportEnded.pipe(first()).subscribe(() => {
211+
expect(exportStartedFired).toBe(true);
212+
done();
213+
});
214+
215+
exporter.export(grid, options);
216+
});
217+
218+
it('should handle grid with multiple columns', (done) => {
219+
const fix = TestBed.createComponent(GridIDNameJobTitleComponent);
220+
fix.detectChanges();
221+
222+
const grid = fix.componentInstance.grid;
223+
224+
exporter.exportEnded.pipe(first()).subscribe(() => {
225+
expect(ExportUtilities.saveBlobToFile).toHaveBeenCalledTimes(1);
226+
done();
227+
});
228+
229+
exporter.export(grid, options);
230+
});
231+
232+
it('should export with custom filename from options', (done) => {
233+
const fix = TestBed.createComponent(GridIDNameJobTitleComponent);
234+
fix.detectChanges();
235+
236+
const grid = fix.componentInstance.grid;
237+
const customOptions = new IgxPdfExporterOptions('MyCustomGrid');
238+
239+
exporter.exportEnded.pipe(first()).subscribe(() => {
240+
expect(ExportUtilities.saveBlobToFile).toHaveBeenCalledTimes(1);
241+
const callArgs = (ExportUtilities.saveBlobToFile as jasmine.Spy).calls.mostRecent().args;
242+
expect(callArgs[1]).toBe('MyCustomGrid');
243+
done();
244+
});
245+
246+
exporter.export(grid, customOptions);
247+
});
93248
});

0 commit comments

Comments
 (0)