Skip to content

Commit

Permalink
Add a failing test for potential useReducer bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Mar 16, 2019
1 parent 4162f60 commit daac56e
Showing 1 changed file with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,49 @@ describe('ReactHooks', () => {
expect(root).toMatchRenderedOutput('4');
});

it('uses the right reducer in an edge case', () => {
let _setCounter;

function Parent() {
const [counter, setCounter] = React.useState(1);
_setCounter = setCounter;
return (
<React.Fragment>
{counter}
<Child count={counter} />
</React.Fragment>
);
}

function Child({count}) {
const reducer = (state, action) => {
return count;
};
const [state, dispatch] = React.useReducer(reducer);
React.useEffect(
() => {
dispatch();
},
[count],
);
return state || 'init';
}

const root = ReactTestRenderer.create(null);
ReactTestRenderer.act(() => {
root.update(<Parent />);
});
expect(root).toMatchRenderedOutput('11');
ReactTestRenderer.act(() => {
_setCounter(c => c + 1);
});
expect(root).toMatchRenderedOutput('22');
ReactTestRenderer.act(() => {
_setCounter(c => c + 1);
});
expect(root).toMatchRenderedOutput('33');
});

it('warns for bad useImperativeHandle first arg', () => {
const {useImperativeHandle} = React;
function App() {
Expand Down

0 comments on commit daac56e

Please sign in to comment.