Authoring Plugins
Creating a plugin for enzyme-context
is relatively straightforward because an enzyme-context
plugin is just a function!
Anatomy of a Plugin
(node, options) => PluginReturns
Arguments
node
(React.ReactElement
): The react element passed tomount
/shallow
options
(Object
): The options passed tomount
/shallow
(inclduing any custom options that your plugin wants to handle.)
Returns
An Object with the following attributes
node
(React.ReactElement
): the react element to mountcontroller
(any
): the object this plugin wants to attach to the Enzyme wrapper. For example:options
(Object
): options to feed intomount()
/enzyme()
. This is how plugins provide context. For example:updater
((wrapper: ReactWrapper | ShallowWrapper) => () => void
[optional]): This function will be called immediately after the enzyme wrapper is created. It can be used to setup listeners that update the wrapper after it has been created. For example, if our plugin was supplying context that contained a list of all thewindow.postMessage
events we've received, we could do something like this:
Best Practice: Export a Factory
Sometimes it is useful for your plugin to accept global (as opposed to passed to mount
/shallow
) configuration. This can be accomplished simply by exporting a factory for your plugin from your module:
Before:
After
I would even recommend that you export a factory for your plugin even if it doesn't accept any configuration. Then, if you need to accept configuration in the future, you can do so without introducing a breaking change to your API.
Authoring Utilities
Enzyme Context has published a library called enzyme-context-utils with some helpful utilities for authoring enzyme-context plugins.
Last updated