Even though types make your code safer, it doesn't remove the need for testing! If you want to test your ReasonReact components using your JavaScript testing stack, that's perfectly alright, but if you prefer to write them in Reason, here are some testing frameworks and bindings to make your life easier:
Pick the test framework you like the best (both use Jest under the hood):
We provide test utilities in the ReactTestUtils module.
Here's what a test might look like:
open TestFramework;
open ReactTestUtils;
describe("My basic test", ({test, beforeEach, afterEach}) => {
// Here, we prepare an empty ref that will eventually be
// the root node for our test
let container = ref(None);
// Before each test, creates a new div root
beforeEach(prepareContainer(container));
// After each test, removes the div
afterEach(cleanupContainer(container));
test("can render DOM elements", ({expect}) => {
// The following function gives us the div
let container = getContainer(container);
// Most of the ReactTestUtils API is there
act(() => {
ReactDOMRe.render(<div> "Hello world!"->React.string </div>, container)
});
expect.bool(
container
// We also provide some basic DOM querying utilities
// to ease your tests
->DOM.findBySelectorAndTextContent("div", "Hello world!")
->Option.isSome,
).
toBeTrue();
});
});Directly from the React test utilities.
actactAsync(let's your return a promise)isElementisElementOfTypeisDOMComponentisCompositeComponentisCompositeComponentWithType
And utilities for setup and teardown:
prepareContainercleanupContainergetContainer
Simulate.clickSimulate.clickWithEventSimulate.changeSimulate.changeWithEventSimulate.changeWithValueSimulate.changeWithCheckedSimulate.focusSimulate.blurSimulate.canPlaySimulate.timeUpdateSimulate.ended
If you feel like some are missing, you can easily adapt the bindings: ReactTestUtils and make a PR!
valuefindBySelectorfindByAllSelectorfindBySelectorAndTextContentfindBySelectorAndPartialTextContent
