diff --git a/src/composeProps.ts b/src/composeProps.ts index f225dd1e..82707808 100644 --- a/src/composeProps.ts +++ b/src/composeProps.ts @@ -13,7 +13,7 @@ function composeProps>( if (typeof func === 'function') { composedProps[key] = (...args) => { func(...args); - return originProps[key](...args); + return originProps[key]?.(...args); }; } }); diff --git a/tests/composeProps.test.js b/tests/composeProps.test.js index c6b0d389..8fb76dee 100644 --- a/tests/composeProps.test.js +++ b/tests/composeProps.test.js @@ -5,19 +5,23 @@ describe('composeProps', () => { const aChange = jest.fn(); const aBlur = jest.fn(); const bChange = jest.fn(); + const bDemo = jest.fn(); const sourceProps = { value: '11', onChange: aChange, onBlur: aBlur }; - const patchProps = { onChange: bChange, placeholder: 'x' }; + const patchProps = { onChange: bChange, onDemo: bDemo, placeholder: 'x' }; const props = composeProps(sourceProps, patchProps, true); props.onChange(); props.onBlur(); + props.onDemo(); expect(aChange).toHaveBeenCalled(); expect(aBlur).toHaveBeenCalled(); expect(bChange).toHaveBeenCalled(); + expect(bDemo).toHaveBeenCalled(); expect(Object.keys(props)).toEqual([ 'value', 'onChange', 'onBlur', + 'onDemo', 'placeholder', ]); }); @@ -25,15 +29,23 @@ describe('composeProps', () => { const aChange = jest.fn(); const aBlur = jest.fn(); const bChange = jest.fn(); + const bDemo = jest.fn(); const sourceProps = { value: '11', onChange: aChange, onBlur: aBlur }; - const patchProps = { onChange: bChange, placeholder: 'x' }; + const patchProps = { onChange: bChange, onDemo: bDemo, placeholder: 'x' }; const props = composeProps(sourceProps, patchProps); props.onChange(); props.onBlur(); + props.onDemo(); expect(aChange).toHaveBeenCalled(); expect(aBlur).toHaveBeenCalled(); expect(bChange).toHaveBeenCalled(); - expect(Object.keys(props)).toEqual(['value', 'onChange', 'onBlur']); + expect(bDemo).toHaveBeenCalled(); + expect(Object.keys(props)).toEqual([ + 'value', + 'onChange', + 'onBlur', + 'onDemo', + ]); }); });