Merged PR 381264: [React Wrapper]: Clean the Event Handler Map during Unmounting

**Issue:**  https://github.com/microsoft/powerbi-client-react/issues/79

**Problem:** With Strict Mode in React 18, React will simulate mounting, unmounting and remounting the component in development mode. [React 18 Strict Mode behaviours](https://react.dev/blog/2022/03/29/react-v18#new-strict-mode-behaviors)

During the first mount Event Handlers will be set and we store the eventHandlers in a variable `prevEventHandlerMapString` to avoid resetting the eventHandlers again if the same map is passed.

After the first mount, it will unmount, and we are not cleaning the `prevEventHandlerMapString` here.
When the component gets remounted again, since there is no change in eventHandlers it will return instead of setting the eventHandlers.

**Resolution:**

Clean the `prevEventHandlerMapString` during component gets unmounted to initial empty string.

Related work items: #1069474, #1072700, #1073594
This commit is contained in:
Kotana Sai 2023-04-17 09:31:35 +00:00
Родитель c9fa39c112
Коммит 9a5bc2e8bd
2 изменённых файлов: 39 добавлений и 1 удалений

Просмотреть файл

@ -186,6 +186,9 @@ export class PowerBIEmbed extends React.Component<EmbedProps> {
if (this.containerRef.current) {
this.powerbi.reset(this.containerRef.current);
}
// Set the previous event handler map string to empty
this.prevEventHandlerMapString = '';
};
render(): JSX.Element {

Просмотреть файл

@ -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