This is an adapter to support using the Enzyme UI component testing library with Rax.
The adapter supports Rax 1.x.
Add the library to your development dependencies:
# If using npm
npm install --save-dev enzyme-adapter-rax
# If using yarn
yarn add --dev enzyme-adapter-rax
Then in the setup code for your tests, configure Enzyme to use the adapter provided by this package:
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-rax';
configure({ adapter: new Adapter() });
Once the adapter is configured, you can write Enzyme tests for your Rax UI components following the Enzyme docs. The full DOM rendering, shallow rendering and string rendering modes are supported.
For runnable example projects, see the examples/ directory. To run the examples locally, clone this repository, then run:
cd examples/<project name>
npm install
npm test
The general intent is that tests written using Enzyme + React can be easily made to work with Enzyme + Rax. However there are some differences in behavior between this adapter and Enzyme's React adapters to be aware of:
The simulate
API does not dispatch actual DOM events in the React adapters, it just calls
the corresponding handler. The Rax adapter does dispatch an actual event
using element.dispatchEvent(...)
.
When target tested component is triggered rerender, you need use jest.runAllTimers()
to resolve the async render.
Example:
const wrapper = mount(<Counter initialCount={5} />);
wrapper.find('button').simulate('click');
jest.runAllTimers();
expect(wrapper.text()).toEqual('Current value: 6');