Skip to content

Commit 23e0432

Browse files
author
Matt Mazzola
committed
Update to construct complete list of allowedEvents and check all of them instead of only those in child class. Fixes bug for allowing load event handler.
1 parent 37fee9c commit 23e0432

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

src/embed.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,18 @@ export interface IInternalEventHandler<T> {
4242
}
4343

4444
export abstract class Embed {
45-
public static accessTokenAttribute = 'powerbi-access-token';
46-
public static embedUrlAttribute = 'powerbi-embed-url';
47-
public static nameAttribute = 'powerbi-name';
48-
public static typeAttribute = 'powerbi-type';
49-
public static type: string;
45+
static allowedEvents = ["loaded"];
46+
static accessTokenAttribute = 'powerbi-access-token';
47+
static embedUrlAttribute = 'powerbi-embed-url';
48+
static nameAttribute = 'powerbi-name';
49+
static typeAttribute = 'powerbi-type';
50+
static type: string;
5051

5152
private static defaultSettings: models.ISettings = {
5253
filterPaneEnabled: true
5354
};
5455

56+
allowedEvents = [];
5557
eventHandlers: IInternalEventHandler<any>[];
5658
hpm: hpm.HttpPostMessage;
5759
service: service.Service;
@@ -64,6 +66,7 @@ export abstract class Embed {
6466
* The service has list of all embeds on the host page, and each embed has reference to the service that created it.
6567
*/
6668
constructor(service: service.Service, hpmFactory: service.IHpmFactory, element: HTMLElement, config: IEmbedConfiguration) {
69+
Array.prototype.push.apply(this.allowedEvents, Embed.allowedEvents);
6770
this.eventHandlers = [];
6871
this.service = service;
6972
this.element = element;
@@ -154,6 +157,10 @@ export abstract class Embed {
154157
* ```
155158
*/
156159
on<T>(eventName: string, handler: service.IEventHandler<T>): void {
160+
if(this.allowedEvents.indexOf(eventName) === -1) {
161+
throw new Error(`eventName is must be one of ${this.allowedEvents}. You passed: ${eventName}`);
162+
}
163+
157164
this.eventHandlers.push({
158165
test: (event: service.IEvent<T>) => event.name === eventName,
159166
handle: handler

src/report.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ export class Report extends embed.Embed {
99
static reportIdAttribute = 'powerbi-report-id';
1010
static type = "Report";
1111

12+
constructor(service: service.Service, hpmFactory: service.IHpmFactory, element: HTMLElement, config: embed.IEmbedConfiguration) {
13+
super(service, hpmFactory, element, config);
14+
Array.prototype.push.apply(this.allowedEvents, Report.allowedEvents);
15+
}
16+
1217
/**
1318
* This adds backwards compatibility for older config which used the reportId query param to specify report id.
1419
* E.g. http://embedded.powerbi.com/appTokenReportEmbed?reportId=854846ed-2106-4dc2-bc58-eb77533bf2f1
@@ -113,14 +118,6 @@ export class Report extends embed.Embed {
113118
});
114119
}
115120

116-
on<T>(eventName: string, handler: service.IEventHandler<T>): void {
117-
if(Report.allowedEvents.indexOf(eventName) === -1) {
118-
throw new Error(`eventName is must be one of ${Report.allowedEvents}. You passed: ${eventName}`);
119-
}
120-
121-
super.on<T>(eventName, handler);
122-
}
123-
124121
/**
125122
* Set the active page
126123
*/

test/test.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4314,5 +4314,27 @@ describe('SDK-to-MockApp', function () {
43144314
expect(testData.handler).not.toHaveBeenCalled();
43154315
});
43164316
});
4317+
4318+
it(`ensure load event is allowed`, function () {
4319+
// Arrange
4320+
const testData = {
4321+
uniqueId: 'uniqueId',
4322+
reportId: 'fakeReportId',
4323+
eventName: 'loaded',
4324+
handler: jasmine.createSpy('handler'),
4325+
simulatedBody: {
4326+
initiator: 'sdk'
4327+
}
4328+
};
4329+
4330+
report.on(testData.eventName, testData.handler);
4331+
4332+
// Act
4333+
iframeHpm.post(`/reports/${report2.config.uniqueId}/events/${testData.eventName}`, testData.simulatedBody)
4334+
.then(response => {
4335+
// Assert
4336+
expect(testData.handler).toHaveBeenCalledWith(testData.simulatedBody);
4337+
});
4338+
});
43174339
});
43184340
});

0 commit comments

Comments
 (0)