From c141abfb14a51e60c51ac1e211e2b87c87b17af4 Mon Sep 17 00:00:00 2001 From: Matt Mazzola Date: Tue, 23 Aug 2016 15:53:32 -0700 Subject: [PATCH] Add report.print() to invoke window.print() on embedded iframe. --- src/report.ts | 14 +++++++ test/test.spec.ts | 68 +++++++++++++++++++++++++++++++++ test/utility/mockApp.ts | 5 ++- test/utility/mockReportEmbed.ts | 5 +++ 4 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/report.ts b/src/report.ts index 0697334b..5d98431a 100644 --- a/src/report.ts +++ b/src/report.ts @@ -160,6 +160,20 @@ export class Report extends embed.Embed implements IReportNode, IFilterable { return new Page(this, name, displayName); } + /** + * Print the active page of the report. + * (Invokes window.print() on embed iframe) + */ + print(): Promise { + return this.service.hpm.post('/report/print', null, { uid: this.config.uniqueId }, this.iframe.contentWindow) + .then(response => { + return response.body; + }) + .catch(response => { + throw response.body; + }); + } + /** * Remove all filters at report level * diff --git a/test/test.spec.ts b/test/test.spec.ts index faecf795..0c7790c6 100644 --- a/test/test.spec.ts +++ b/test/test.spec.ts @@ -1017,6 +1017,26 @@ describe('Protocol', function () { }); }); + describe('print', function () { + it('POST /report/print returns 202 if the request is valid', function (done) { + // Arrange + iframeLoaded + .then(() => { + spyApp.print.and.returnValue(Promise.resolve(null)); + // Act + hpm.post('/report/print', null) + .then(response => { + // Assert + expect(spyApp.print).toHaveBeenCalled(); + expect(response.statusCode).toEqual(202); + // Cleanup + spyApp.print.calls.reset(); + done(); + }); + }); + }); + }); + describe('filters (report level)', function () { it('GET /report/filters returns 200 with body as array of filters', function (done) { // Arrange @@ -2026,6 +2046,36 @@ describe('SDK-to-HPM', function () { }); }); + describe('print', function () { + it('report.print() sends POST /report/print', function () { + // Arrange + spyHpm.post.and.returnValue(Promise.resolve({ + body: {} + })); + + // Act + report.print(); + + // Assert + expect(spyHpm.post).toHaveBeenCalledWith('/report/print', null, { uid: uniqueId }, iframe.contentWindow); + }); + + it('report.print() returns promise that resolves if the request is accepted', function (done) { + // Arrange + spyHpm.post.and.returnValue(Promise.resolve({ + body: {} + })); + + // Act + report.print() + .then(() => { + // Assert + expect(spyHpm.post).toHaveBeenCalledWith('/report/print', null, { uid: uniqueId }, iframe.contentWindow); + done(); + }); + }); + }); + describe('settings', function () { it('report.updateSettings(settings) sends PATCH /report/settings with settings object', function () { // Arrange @@ -3064,6 +3114,24 @@ describe('SDK-to-MockApp', function () { }); }); + describe('print', function () { + it('report.print() returns promise that resolves with null if the report print command was accepted', function (done) { + // Arrange + iframeLoaded + .then(() => { + spyApp.print.and.returnValue(Promise.resolve(null)); + // Act + report.print() + .then(response => { + // Assert + expect(spyApp.print).toHaveBeenCalled(); + expect(response).toEqual(undefined); + done(); + }); + }); + }); + }); + describe('settings', function () { it('report.updateSettings(setting) returns promise that rejects with validation error if object is invalid', function (done) { // Arrange diff --git a/test/utility/mockApp.ts b/test/utility/mockApp.ts index e67b7d3c..a577d105 100644 --- a/test/utility/mockApp.ts +++ b/test/utility/mockApp.ts @@ -19,6 +19,7 @@ export interface IApp { setFilters(filters: (models.IBasicFilter | models.IAdvancedFilter)[]): Promise; validateFilter(filter: models.IFilter): Promise; // Other + print(): Promise; exportData(): Promise; } @@ -41,6 +42,7 @@ export const mockAppSpyObj = { setFilters: jasmine.createSpy("setFilters").and.returnValue(Promise.resolve(null)), validateFilter: jasmine.createSpy("validateFilter").and.callFake(models.validateFilter), // Other + print: jasmine.createSpy("print").and.returnValue(Promise.resolve(null)), exportData: jasmine.createSpy("exportData").and.returnValue(Promise.resolve(null)), reset() { @@ -56,7 +58,8 @@ export const mockAppSpyObj = { mockAppSpyObj.getFilters.calls.reset(); mockAppSpyObj.setFilters.calls.reset(); mockAppSpyObj.validateFilter.calls.reset(); - + mockAppSpyObj.print.calls.reset(); + mockAppSpyObj.exportData.calls.reset(); } }; diff --git a/test/utility/mockReportEmbed.ts b/test/utility/mockReportEmbed.ts index a24f0aa8..d6e40274 100644 --- a/test/utility/mockReportEmbed.ts +++ b/test/utility/mockReportEmbed.ts @@ -296,5 +296,10 @@ export function setupMockApp(iframeContentWindow: Window, parentWindow: Window, }); }); + router.post('/report/print', (req, res) => { + app.print(); + res.send(202); + }); + return hpm; } \ No newline at end of file