This file contains hidden or 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
| 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} |
This file contains hidden or 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
| 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(); | |
| }); |
This file contains hidden or 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
| 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}); |
This file contains hidden or 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
| 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}); |
This file contains hidden or 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 'whatwg-fetch'; | |
| ... | |
| export function fetchData(id) { | |
| return (dispatch, getState) => { | |
| if(getState().id === id)) { | |
| return; // No need to fetch | |
| } | |
| dispatch(requestData(id)); |
NewerOlder