Skip to content

Commit 389bc12

Browse files
authored
Add report.print() to invoke window.print() on embedded iframe. (microsoft#32)
1 parent a6c601d commit 389bc12

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

src/report.ts

+14
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,20 @@ export class Report extends embed.Embed implements IReportNode, IFilterable {
160160
return new Page(this, name, displayName);
161161
}
162162

163+
/**
164+
* Print the active page of the report.
165+
* (Invokes window.print() on embed iframe)
166+
*/
167+
print(): Promise<void> {
168+
return this.service.hpm.post<models.IError[]>('/report/print', null, { uid: this.config.uniqueId }, this.iframe.contentWindow)
169+
.then(response => {
170+
return response.body;
171+
})
172+
.catch(response => {
173+
throw response.body;
174+
});
175+
}
176+
163177
/**
164178
* Remove all filters at report level
165179
*

test/test.spec.ts

+68
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,26 @@ describe('Protocol', function () {
10171017
});
10181018
});
10191019

1020+
describe('print', function () {
1021+
it('POST /report/print returns 202 if the request is valid', function (done) {
1022+
// Arrange
1023+
iframeLoaded
1024+
.then(() => {
1025+
spyApp.print.and.returnValue(Promise.resolve(null));
1026+
// Act
1027+
hpm.post<void>('/report/print', null)
1028+
.then(response => {
1029+
// Assert
1030+
expect(spyApp.print).toHaveBeenCalled();
1031+
expect(response.statusCode).toEqual(202);
1032+
// Cleanup
1033+
spyApp.print.calls.reset();
1034+
done();
1035+
});
1036+
});
1037+
});
1038+
});
1039+
10201040
describe('filters (report level)', function () {
10211041
it('GET /report/filters returns 200 with body as array of filters', function (done) {
10221042
// Arrange
@@ -2026,6 +2046,36 @@ describe('SDK-to-HPM', function () {
20262046
});
20272047
});
20282048

2049+
describe('print', function () {
2050+
it('report.print() sends POST /report/print', function () {
2051+
// Arrange
2052+
spyHpm.post.and.returnValue(Promise.resolve({
2053+
body: {}
2054+
}));
2055+
2056+
// Act
2057+
report.print();
2058+
2059+
// Assert
2060+
expect(spyHpm.post).toHaveBeenCalledWith('/report/print', null, { uid: uniqueId }, iframe.contentWindow);
2061+
});
2062+
2063+
it('report.print() returns promise that resolves if the request is accepted', function (done) {
2064+
// Arrange
2065+
spyHpm.post.and.returnValue(Promise.resolve({
2066+
body: {}
2067+
}));
2068+
2069+
// Act
2070+
report.print()
2071+
.then(() => {
2072+
// Assert
2073+
expect(spyHpm.post).toHaveBeenCalledWith('/report/print', null, { uid: uniqueId }, iframe.contentWindow);
2074+
done();
2075+
});
2076+
});
2077+
});
2078+
20292079
describe('settings', function () {
20302080
it('report.updateSettings(settings) sends PATCH /report/settings with settings object', function () {
20312081
// Arrange
@@ -3064,6 +3114,24 @@ describe('SDK-to-MockApp', function () {
30643114
});
30653115
});
30663116

3117+
describe('print', function () {
3118+
it('report.print() returns promise that resolves with null if the report print command was accepted', function (done) {
3119+
// Arrange
3120+
iframeLoaded
3121+
.then(() => {
3122+
spyApp.print.and.returnValue(Promise.resolve(null));
3123+
// Act
3124+
report.print()
3125+
.then(response => {
3126+
// Assert
3127+
expect(spyApp.print).toHaveBeenCalled();
3128+
expect(response).toEqual(undefined);
3129+
done();
3130+
});
3131+
});
3132+
});
3133+
});
3134+
30673135
describe('settings', function () {
30683136
it('report.updateSettings(setting) returns promise that rejects with validation error if object is invalid', function (done) {
30693137
// Arrange

test/utility/mockApp.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export interface IApp {
1919
setFilters(filters: (models.IBasicFilter | models.IAdvancedFilter)[]): Promise<void>;
2020
validateFilter(filter: models.IFilter): Promise<models.IError[]>;
2121
// Other
22+
print(): Promise<void>;
2223
exportData(): Promise<void>;
2324
}
2425

@@ -41,6 +42,7 @@ export const mockAppSpyObj = {
4142
setFilters: jasmine.createSpy("setFilters").and.returnValue(Promise.resolve(null)),
4243
validateFilter: jasmine.createSpy("validateFilter").and.callFake(models.validateFilter),
4344
// Other
45+
print: jasmine.createSpy("print").and.returnValue(Promise.resolve(null)),
4446
exportData: jasmine.createSpy("exportData").and.returnValue(Promise.resolve(null)),
4547

4648
reset() {
@@ -56,7 +58,8 @@ export const mockAppSpyObj = {
5658
mockAppSpyObj.getFilters.calls.reset();
5759
mockAppSpyObj.setFilters.calls.reset();
5860
mockAppSpyObj.validateFilter.calls.reset();
59-
61+
mockAppSpyObj.print.calls.reset();
62+
mockAppSpyObj.exportData.calls.reset();
6063
}
6164
};
6265

test/utility/mockReportEmbed.ts

+5
Original file line numberDiff line numberDiff line change
@@ -296,5 +296,10 @@ export function setupMockApp(iframeContentWindow: Window, parentWindow: Window,
296296
});
297297
});
298298

299+
router.post('/report/print', (req, res) => {
300+
app.print();
301+
res.send(202);
302+
});
303+
299304
return hpm;
300305
}

0 commit comments

Comments
 (0)