Skip to content

Commit 2b93c12

Browse files
committed
fix(WebComponentWrapper): Fixed event handler removal after prop update
Unmount old attached event handler in case a new one is passed
1 parent 78dc3af commit 2b93c12

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { withWebComponent } from './withWebComponent';
2+
import UI5Button from '@ui5/webcomponents/dist/Button';
3+
import { mountThemedComponent } from '@shared/tests/utils';
4+
import { expect } from 'chai';
5+
import React, { FC, cloneElement } from 'react';
6+
import { spy } from 'sinon';
7+
8+
describe('withWebComponent', () => {
9+
it('Unmount Event Handlers correctly after prop update', () => {
10+
let Button: FC<any> = withWebComponent(UI5Button);
11+
const callback = spy();
12+
const wrapper = mountThemedComponent(<Button onPress={(...args) => callback(...args)} />);
13+
const component = wrapper
14+
.find('ui5-button')
15+
.first()
16+
.instance();
17+
// @ts-ignore
18+
component.onclick({});
19+
expect(callback.callCount).to.equal(1, 'onPress handler has not been called');
20+
wrapper.setProps({
21+
children: cloneElement(wrapper.prop('children'), { onPress: (...args) => callback(...args) })
22+
});
23+
wrapper.update();
24+
// @ts-ignore
25+
component.onclick({});
26+
expect(callback.callCount).to.equal(2, 'onPress handler has not been called after update');
27+
});
28+
});

packages/fiori3/src/internal/withWebComponent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export function withWebComponent<T>(WebComponent): FC<T> {
9696
const eventHandler = this.props[eventIdentifier] || this.props[alternativeKey];
9797
if (typeof eventHandler === 'function' && this.eventRegistry[alternativeKey] !== eventHandler) {
9898
if (this.eventRegistry[alternativeKey]) {
99-
this.wcRef.removeEventListener(eventIdentifier, this.eventRegistry[alternativeKey]);
99+
this.wcRef.removeEventListener(eventIdentifier, this.eventRegistryWrapped[alternativeKey]);
100100
}
101101
this.eventRegistryWrapped[alternativeKey] = this.createEventWrapperFor(eventIdentifier, eventHandler);
102102
this.wcRef.addEventListener(eventIdentifier, this.eventRegistryWrapped[alternativeKey]);

0 commit comments

Comments
 (0)