Skip to content

Commit 5f699ca

Browse files
author
Matt Mazzola
authored
Add exception to prevent attempting to embed with different type than previous embed type. Add tests. (microsoft#42)
1 parent d1ba628 commit 5f699ca

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/service.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,15 @@ export class Service implements IService {
247247
throw new Error(`Attempted to embed using config ${JSON.stringify(config)} on element ${element.outerHTML} which already has embedded comopnent associated, but could not find the existing comopnent in the list of active components. This could indicate the embeds list is out of sync with the DOM, or the component is referencing the incorrect HTML element.`);
248248
}
249249

250+
/**
251+
* TODO: Dynamic embed type switching could be supported but there is work needed to prepare the service state and DOM cleanup.
252+
* remove all event handlers from the DOM, then reset the element to initial state which removes iframe, and removes from list of embeds
253+
* then we can call the embedNew function which would allow setting the proper embedUrl and construction of object based on the new type.
254+
*/
255+
if (typeof config.type === "string" && config.type !== component.config.type) {
256+
throw new Error(`Embedding on an existing element with a different type than the previous embed object is not supported. Attempted to embed using config ${JSON.stringify(config)} on element ${element.outerHTML}, but the existing element contains an embed of type: ${this.config.type} which does not match the new type: ${config.type}`);
257+
}
258+
250259
component.load(<embed.IInternalEmbedConfiguration>config);
251260

252261
return component;

test/test.spec.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,36 @@ describe('service', function () {
140140
expect(attemptEmbed).toThrowError(Error);
141141
});
142142

143+
it('if attempting to embed on existing element with different type than previous embed, throw error', function () {
144+
// Arrange
145+
const component = $('<div></div>')
146+
.appendTo('#powerbi-fixture');
147+
148+
const reportEmbedConfig: embed.IEmbedConfiguration = {
149+
type: "report",
150+
id: "fakeReportId",
151+
accessToken: "fakeAccessToken",
152+
embedUrl: "fakeEmbedUrl"
153+
};
154+
155+
const dashboardEmbedConfig: embed.IEmbedConfiguration = {
156+
type: "dashboard",
157+
id: "fakeDashboardId",
158+
accessToken: "fakeAccessToken",
159+
embedUrl: "fakeEmbedUrl"
160+
};
161+
162+
powerbi.embed(component[0], reportEmbedConfig);
163+
164+
// Act
165+
const attemptEmbed = () => {
166+
powerbi.embed(component[0], dashboardEmbedConfig);
167+
};
168+
169+
// Assert
170+
expect(attemptEmbed).toThrowError(Error);
171+
});
172+
143173
it('if attempting to embed without specifying an embed url, throw error', function () {
144174
// Arrange
145175
const component = $('<div></div>')

0 commit comments

Comments
 (0)