Replies: 4 comments
-
I believe you need to do a little more work to send in the event object with enzyme. See https://enzymejs.github.io/enzyme/docs/api/ReactWrapper/simulate.html |
Beta Was this translation helpful? Give feedback.
-
This section of the docs should also help. https://react-spectrum.adobe.com/react-spectrum/testing.html#triggering-events |
Beta Was this translation helpful? Give feedback.
-
Is there any way of using enzyme instead of the recommended libraries in the docs? |
Beta Was this translation helpful? Give feedback.
-
After following the advice from @snowystinger , I have finally managed to make it work. export const triggerPress = (element: ReactWrapper): void => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(element.prop('onClick') as (e: any) => boolean)({
button: 0,
detail: 0,
nativeEvent: { detail: 0 },
currentTarget: element.getDOMNode(),
target: element.getDOMNode(),
// eslint-disable-next-line @typescript-eslint/no-empty-function
stopPropagation: () => {},
});
}; |
Beta Was this translation helpful? Give feedback.
-
❔ Question
Any recommendations for how to simulate press events in Enzyme?
💻 Code Sample
The only way I was able to invoke the callback passed to
onPress
was to do so directly:But I would rather trigger a click event on the button element so I know it's all wired correctly to my Button component.
I made a small CodeSandbox that has this wired up, just click on the "Tests" tab above the browser frame to see the test output.
https://codesandbox.io/s/unruffled-feather-evuqv?file=/src/Button.test.js
Beta Was this translation helpful? Give feedback.
All reactions