diff --git a/React/powerbi-client-react/src/PowerBIEmbed.tsx b/React/powerbi-client-react/src/PowerBIEmbed.tsx index c60ad19..7caac29 100644 --- a/React/powerbi-client-react/src/PowerBIEmbed.tsx +++ b/React/powerbi-client-react/src/PowerBIEmbed.tsx @@ -186,6 +186,9 @@ export class PowerBIEmbed extends React.Component { if (this.containerRef.current) { this.powerbi.reset(this.containerRef.current); } + + // Set the previous event handler map string to empty + this.prevEventHandlerMapString = ''; }; render(): JSX.Element { diff --git a/React/powerbi-client-react/test/PowerBIEmbed.spec.tsx b/React/powerbi-client-react/test/PowerBIEmbed.spec.tsx index 642248c..3d823bf 100644 --- a/React/powerbi-client-react/test/PowerBIEmbed.spec.tsx +++ b/React/powerbi-client-react/test/PowerBIEmbed.spec.tsx @@ -7,10 +7,12 @@ import React from 'react'; import ReactDOM from 'react-dom'; import { act, isElement } from 'react-dom/test-utils'; import { Report, Dashboard, service, factories, IEmbedSettings, IReportEmbedConfiguration } from 'powerbi-client'; -import { PowerBIEmbed } from '../src/PowerBIEmbed'; import { mockPowerBIService, mockedMethods } from "./mockService"; import { IBasicFilter, FilterType, FiltersOperations } from 'powerbi-models'; +import { PowerBIEmbed } from '../src/PowerBIEmbed'; +import { stringifyMap } from '../src/utils'; + // Use this function to render powerbi entity with only config function renderReport(container: HTMLDivElement, config) { let testReport: Report = undefined; @@ -944,6 +946,39 @@ describe('tests of PowerBIEmbed', function () { }); describe('tests for setting event handlers', () => { + it('test event handlers are setting when remounting twice', () => { + // Arrange + const eventHandlers = new Map([ + ['loaded', function () { }], + ['rendered', function () { }], + ['error', function () { }] + ]); + + const powerbi = new service.Service( + factories.hpmFactory, + factories.wpmpFactory, + factories.routerFactory); + const embed = powerbi.bootstrap(container, { type: 'report' }); + + // Act + const powerbiembed = new PowerBIEmbed({ + embedConfig: { type: 'report' }, + eventHandlers: eventHandlers + }); + + // Ignoring next line as setEventHandlers is a private method + // @ts-ignore + powerbiembed.setEventHandlers(embed, eventHandlers); + powerbiembed.componentWillUnmount(); + expect((powerbiembed as any).prevEventHandlerMapString).toBe(''); + powerbiembed.componentDidMount(); + // @ts-ignore + powerbiembed.setEventHandlers(embed, eventHandlers); + + // Assert + expect((powerbiembed as any).prevEventHandlerMapString).toBe(stringifyMap(eventHandlers)); + }); + it('clears and sets the event handlers', () => { // Arrange