Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Fix initial enhancer state for fn components #1041

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/__tests__/enhancer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ describe('Enhancer', () => {
expect(ref.current.state).to.deep.equal({_radiumStyleState: {}});
});

it('sets up initial state on a function component', () => {
// using plugin API to get state as state hooks keep things pretty private
// so we cannot just dive into the component instance like with the class example
const dummyPlugin = sinon.spy();
const Composed = props => <div {...props} />;
const Enhanced = Enhancer(Composed, {
plugins: [dummyPlugin]
});

renderFcIntoDocument(<Enhanced style={{color: 'red'}} />);

const pluginApi = dummyPlugin.getCall(0).args[0];

expect(pluginApi.getComponentField('state')).to.deep.equal({
_radiumStyleState: {}
});
});

it('merges with existing state', () => {
class Composed extends Component {
constructor(props) {
Expand Down
2 changes: 1 addition & 1 deletion src/enhancer.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ function createEnhancedFunctionComponent(
const {radiumConfig, ...otherProps} = props;
const radiumConfigContext = useContext(RadiumConfigContext);
const styleKeeperContext = useContext(StyleKeeperContext);
const [state, setState] = useState({});
const [state, setState] = useState({_radiumStyleState: {}});

const enhancerApi = useRef<EnhancerApi>({
state,
Expand Down