Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

🛠️ debounce-handler handle no prop case #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/debounce-handler/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ const debounceHandler = (handlerName, delay, leadingCall) => (Target) => {

const delayValue = typeof delay === 'function' ? delay(props) : delay

this.debouncedPropInvoke = debounce(
(...args) => this.props[handlerName](...args),
delayValue,
leadingCall
)
if (this.props[handlerName]) {
this.debouncedPropInvoke = debounce(
(...args) => this.props[handlerName](...args),
delayValue,
leadingCall
)
} else {
this.debouncedPropInvoke = () => {}
}

this.handler = (e, ...rest) => {
if (e && typeof e.persist === 'function') {
Expand Down
9 changes: 9 additions & 0 deletions packages/debounce-handler/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ describe('debounceHandler', () => {
expect(mockJustDebounce.mock.calls).toMatchSnapshot()
})

it('should not error if prop is not present', () => {
const EnhancedTarget = debounceHandler('testHandler', 75, true)(Target)
const wrapper = mount(
<EnhancedTarget />
)
const testHandler = wrapper.find(Target).prop('testHandler')
testHandler()
})

describe('display name', () => {
const origNodeEnv = process.env.NODE_ENV

Expand Down