Skip to content

Instantly share code, notes, and snippets.

View ferrannp's full-sized avatar
🏋️‍♂️
At the gym

Ferran Negre ferrannp

🏋️‍♂️
At the gym
View GitHub Profile
const Dropdown = (props) => (
<div className='dropdown'>
<button onClick={props.onToggle}>
Selected option: {props.data[props.optionSelected]}
</button>
<ul className={props.isOpen ? 'active':null}>
{
props.data.map((item, i) => {
return (
<li key={i} className={i === props.optionSelected ? 'selected':null}
it('does check if we already fetched that id and only calls fetch if necessary', () => {
const store = mockStore({id: 1234, isFetching: false }});
window.fetch = jest.fn().mockImplementation(() => Promise.resolve());
store.dispatch(fetchData(1234)); // Same id
expect(window.fetch).not.toBeCalled();
store.dispatch(fetchData(1234 + 1)); // Different id
expect(window.fetch).toBeCalled();
});
pit('calls request and failure actions if the fetch response was not successful', () => {
window.fetch = jest.fn().mockImplementation(() => Promise.resolve(mockResponse(
400, 'Test Error', '{"status":400, "statusText": Test Error!}')));
return store.dispatch(fetchData(1234))
.then(() => {
const expectedActions = store.getActions();
expect(expectedActions.length).toBe(2);
expect(expectedActions[0]).toEqual({type: types.FETCH_DATA_REQUEST});
pit('calls request and success actions if the fetch response was successful', () => {
window.fetch = jest.fn().mockImplementation(() =>
Promise.resolve(mockResponse(200, null, '{"id":"1234"}')));
return store.dispatch(fetchData(1234))
.then(() => {
const expectedActions = store.getActions();
expect(expectedActions.length).toBe(2);
expect(expectedActions[0]).toEqual({type: types.FETCH_DATA_REQUEST});
import 'whatwg-fetch';
...
export function fetchData(id) {
return (dispatch, getState) => {
if(getState().id === id)) {
return; // No need to fetch
}
dispatch(requestData(id));