Created
June 30, 2017 00:43
-
-
Save audiolion/59a5967405beb9c069841b59521cf38e to your computer and use it in GitHub Desktop.
trouble a brewin' in some reactive waters, redux!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { connect } from 'react-redux'; | |
export class TodosList extends Component { | |
static propTypes = { | |
todos: PropTypes.arrayOf(todoPropTypes).isRequired, | |
isLoading: PropTypes.bool.isRequired, | |
hasErrored: PropTypes.bool, | |
dispatch: PropTypes.func.isRequired, | |
}; | |
randomlyHideTodos = () => { | |
const toggledTodos = this.props.todos.map(todo => { | |
if (Math.random() > 0.5) { | |
todo.hidden = true; | |
} | |
return todo; | |
} | |
this.props.dispatch(updateTodos({ todos: toggledTodos })); | |
}; | |
render() { | |
return ( | |
<ul> | |
{this.props.todos.map(todo => | |
<Todo | |
key={todo.id} | |
{...todo} | |
/> | |
)} | |
</ul> | |
); | |
} | |
} | |
const mapStateToProps = state => { | |
return { | |
todos: state.todosReducer.todos, | |
isLoading: state.todosReducer.isLoading, | |
hasErrored: state.todosReducer.hasErrored, | |
}; | |
}; | |
export default connect(mapStateToProps)(TodosList); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { shallow, mount } from 'enzyme'; | |
import configureStore from 'redux-mock-store' | |
import { TodosList } from 'components/TodosList'; | |
import { mockTodosData } from '__mocks__/todos'; | |
describe('TodosList Component', () => { | |
let wrapper; | |
const middlewares = []; | |
beforeEach(() => { | |
wrapper = shallow( | |
<TodosList | |
todos={mockTodosData} | |
hasErrored={false} | |
isLoading={false} | |
dispatch={jest.fn()} | |
/> | |
); | |
}); | |
it('can randomly hide todos and dispatch result to redux', () => { | |
const visibleTodos = 10; // mock data has 10 hidden = false todos | |
wrapper.instance().randomlyHideTodos(); | |
const newVisibleTodos = wrapper.instance().props.todos.reduce((a, b) => { b.hidden ? a + 1 : a }, 0); | |
expect(newVisibleTodos).toBeLessthan(visibleTodos); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment