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

Commit

Permalink
Fix initial enhancer state for fn components
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanmarks committed Jan 22, 2020
1 parent e1fa4f7 commit eadf5df
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
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

0 comments on commit eadf5df

Please sign in to comment.