Skip to content

Commit 1f358a9

Browse files
committed
Add reset method to core service which checks for associated component and if found removes it from internal list, removes component, and removes iframe. (microsoft#14)
* Add reset method to core service which removes checks for associated component and if found removes it from internal list, removes component, and removes iframe. * Fix comment about removing iframe from element.
1 parent 9c7db05 commit 1f358a9

File tree

5 files changed

+90
-13
lines changed

5 files changed

+90
-13
lines changed

dist/powerbi.js

Lines changed: 16 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/powerbi.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/powerbi.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface IPowerBiConfiguration {
1212
onError?: (error: any) => any;
1313
}
1414

15-
export default class PowerBi {
15+
export class PowerBi {
1616
/**
1717
* List of components this service can embed.
1818
*/
@@ -157,10 +157,24 @@ export default class PowerBi {
157157
}
158158

159159
/**
160-
* Remove component from the list of embedded components.
160+
* Given an html element which has component embedded within it, remove the component from list of embeds, remove association with component, and remove the iframe.
161161
*/
162-
remove(component: Embed): void {
163-
Utils.remove(x => x === component, this.embeds);
162+
reset(element: HTMLElement) {
163+
const powerBiElement = <IPowerBiElement>element;
164+
165+
if (!powerBiElement.powerBiEmbed) {
166+
throw new Error(`You attempted to get an instance of powerbi component associated with element: ${element.outerHTML} but there was no associated instance.`);
167+
}
168+
169+
/** Remove component from internal list */
170+
Utils.remove(x => x === powerBiElement.powerBiEmbed, this.embeds);
171+
/** Delete property from html element */
172+
delete powerBiElement.powerBiEmbed;
173+
/** Remove iframe from element */
174+
const iframe = element.querySelector('iframe');
175+
if(iframe) {
176+
iframe.remove();
177+
}
164178
}
165179

166180
/**
@@ -194,4 +208,4 @@ export default class PowerBi {
194208
}
195209
}
196210

197-
211+
export default PowerBi;

test/core.spec.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import PowerBi from '../src/core';
1+
import * as pbi from '../src/core';
22

3-
declare var powerbi: PowerBi;
3+
declare var powerbi: pbi.PowerBi;
44

55
describe('powerbi', function () {
66
beforeAll(function () {
@@ -238,6 +238,56 @@ describe('powerbi', function () {
238238
});
239239
});
240240

241+
describe('reset', function () {
242+
it('throws an error if the element being reset does not have embedded component associated with it', function () {
243+
// Arrange
244+
const $element = $('<div></div>');
245+
246+
// Act
247+
const attemptToReset = () => {
248+
powerbi.reset($element.get(0));
249+
};
250+
251+
// Assert
252+
expect(attemptToReset).toThrowError();
253+
});
254+
255+
it('deletes the powerBiEmbed property on the element', function () {
256+
// Arrange
257+
const $element = $('<div></div>');
258+
powerbi.embed($element.get(0), {
259+
type: 'report',
260+
embedUrl: 'fakeUrl',
261+
accessToken: 'fakeToken'
262+
});
263+
264+
// Act
265+
expect((<pbi.IPowerBiElement>$element.get(0)).powerBiEmbed).toBeDefined();
266+
powerbi.reset($element.get(0));
267+
268+
// Assert
269+
expect((<pbi.IPowerBiElement>$element.get(0)).powerBiEmbed).toBeUndefined();
270+
});
271+
272+
it('clears the innerHTML of the element', function () {
273+
// Arrange
274+
const $element = $('<div></div>');
275+
powerbi.embed($element.get(0), {
276+
type: 'report',
277+
embedUrl: 'fakeUrl',
278+
accessToken: 'fakeToken'
279+
});
280+
281+
// Act
282+
var iframe = $element.find('iframe');
283+
expect(iframe.length).toEqual(1);
284+
powerbi.reset($element.get(0));
285+
286+
// Assert
287+
expect($element.html()).toEqual('');
288+
});
289+
});
290+
241291
// TODO: Either find a way to fix the test or remove it.
242292
// 1. onReceiveMessage is private so it's not supposed to be accessable for testing
243293
// 2. the window.addEventListener('message', this.onReceiveMessage.bind(this)) prevents the method from being spied on.

0 commit comments

Comments
 (0)