Skip to content

Commit

Permalink
[Tests] useReducer
Browse files Browse the repository at this point in the history
  • Loading branch information
Gangwani, Pawan authored and ljharb committed Mar 17, 2019
1 parent 34a7359 commit 39676a0
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ describeWithDOM('mount', () => {
'useEffect',
'useLayoutEffect',
'useMemo',
'useReducer',
'useState',
'custom',
);
Expand Down
1 change: 1 addition & 0 deletions packages/enzyme-test-suite/test/ShallowWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -1231,6 +1231,7 @@ describe('shallow', () => {
'useEffect',
'useLayoutEffect',
'useMemo',
'useReducer',
'useState',
'custom',
);
Expand Down
78 changes: 78 additions & 0 deletions packages/enzyme-test-suite/test/shared/hooks/useReducer.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import React from 'react';
import { expect } from 'chai';

import { describeIf } from '../../_helpers';

import { useReducer } from '../../_helpers/react-compat';

export default function describeUseReducer({
hasHooks,
Wrap,
}) {
describeIf(hasHooks, 'hooks: useReducer', () => {
describe('with custom dispatch', () => {
const initialState = [];

function Child({ dispatch, text }) {
function fire() {
dispatch({
type: 'ADD_TEXT',
payload: text,
});
}

return <button type="button" onClick={fire}>Add {text}</button>;
}

function reducer(state, action) {
switch (action.type) {
case 'ADD_TEXT':
return [...state, action.payload];
default:
throw new Error();
}
}

function FooBarTextList() {
const [state, dispatch] = useReducer(reducer, initialState);

return (
<div className="FooBarTextList">
<Child text="foo" dispatch={dispatch} />
<Child text="bar" dispatch={dispatch} />
{state.map(text => (
<p key={text}>{text}</p>
))}
</div>
);
}

it('render with initial state from useReducer', () => {
const wrapper = Wrap(<FooBarTextList />);
expect(wrapper.find('p')).to.have.lengthOf(0);
});

it('Test with Add Foo & Bar tex', () => {
const wrapper = Wrap(<FooBarTextList />);
expect(wrapper.find('p')).to.have.lengthOf(0);
wrapper.find('Child').at(0).props().dispatch({
type: 'ADD_TEXT',
payload: 'foo',
});
wrapper.update();

expect(wrapper.find('p')).to.have.lengthOf(1);
expect(wrapper.find('p').at(0).text()).to.equal('foo');

wrapper.find('Child').at(1).props().dispatch({
type: 'ADD_TEXT',
payload: 'bar',
});
wrapper.update();
expect(wrapper.find('p')).to.have.length(2);
expect(wrapper.find('p').at(0).text()).to.equal('foo');
expect(wrapper.find('p').at(1).text()).to.equal('bar');
});
});
});
}

0 comments on commit 39676a0

Please sign in to comment.