Description
Summary
During normal rendering in localhost
mode, SplitTreatments
first passes isReady: false
to its children and returns the control treatment, but shortly after returns isReady: true
and returns the treatment defined in config.features
as expected. But during testing using jest
and testing-library/react
, isReady
is always false
.
I've set up a simple repository to reproduce the issue here: https://github.com/brentcklein/split-tests
Environment
yarn: 1.21.1
node: v14.4.0
react: 16.13.1
react-scripts: 3.4.1
jest: 24.9.0
testing-library/react: 9.5.0
splitio-react: 1.1.0
Example code
// App.jsx
import React from 'react';
import './App.css';
import { SplitFactory } from "@splitsoftware/splitio-react";
import ExampleSplitComponent from "./ExampleSplitComponent";
function App() {
const splitConfig = {
core: {
authorizationKey: "localhost",
key: "default",
},
features: {
[`prod-feature-1`]: "on",
[`prod-feature-2`]: "off",
},
};
return (
<SplitFactory config={splitConfig}>
<ExampleSplitComponent splits={["prod-feature-1", "prod-feature-2"]} />
</SplitFactory>
);
}
export default App;
// ExampleSplitComponent.jsx
import React from "react";
import { SplitTreatments } from "@splitsoftware/splitio-react";
const ExampleSplitComponent = ({ splits }) => {
return splits.map(split => {
return (
<SplitTreatments names={[split]} key={split}>
{({ treatments, isReady }) => {
return isReady && treatments[split].treatment === "on"
? <div>Split {split} is on</div>
: <div>Split {split} is off</div>;
}}
</SplitTreatments>
);
});
}
export default ExampleSplitComponent;
// ExampleSplitComponent.test.jsx
import React from "react";
import { SplitFactory } from "@splitsoftware/splitio-react";
import ExampleSplitComponent from "./ExampleSplitComponent";
import { render, waitForElement, getByText } from "@testing-library/react";
it("renders the proper output based on the Split treatment", async () => {
const splitConfig = {
core: {
authorizationKey: "localhost",
key: "default",
},
features: {
[`test-feature-on`]: "on",
[`test-feature-off`]: "off",
},
};
const { container } = render(
<SplitFactory config={splitConfig}>
<ExampleSplitComponent splits={["test-feature-on", "test-feature-off"]} />
</SplitFactory>
);
const [ featureOn, featureOff ] = await waitForElement(
() => [
getByText(container, "Split test-feature-on is on"),
getByText(container, "Split test-feature-off is off"),
],
{ container }
);
expect(featureOn).toBeTruthy();
expect(featureOff).toBeTruthy();
});
The splits render correctly when run with yarn start
, but running the unit test results in Unable to find an element with the text: Split test-feature-on is on.
with the container output being
<div>
<div>
Split test-feature-on is off
</div>
<div>
Split test-feature-off is off
</div>
</div>