Last active
February 9, 2019 17:01
-
-
Save dominiceden/f3e5e8d505253189ec014384ed9616f4 to your computer and use it in GitHub Desktop.
Testing Shopify Polaris form components with Enzyme and Jest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I've spent a few hours working out how to simulate the selection of a select option in my React app using the Polaris. Polaris provides a | |
// <Select /> component to use that wraps the <select> DOM element, so simulating a change is a bit tricky and involves reading the Polaris | |
// source to work out what's going on. Here's how I got it working for me. | |
// Polaris wants an event object with currentTarget defined. Not sure why they use currentTarget instead of target... | |
mountedWrapper.find('select[name="location_id"]').prop('onChange')({ | |
currentTarget: { | |
value: newLocationId, | |
}, | |
}); | |
// Now assert that the state of the parent container that the form is controlled by has changed. | |
expect(container.state().booking.location_id).toEqual( | |
newLocationId | |
); | |
// Further reading: | |
// https://github.com/airbnb/enzyme/issues/1617 | |
// https://github.com/airbnb/enzyme/issues/400 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment