Skip to content

Instantly share code, notes, and snippets.

View sandwichsudo's full-sized avatar

Gilly Ames sandwichsudo

View GitHub Profile
@sandwichsudo
sandwichsudo / RepoSearchActions.js
Last active July 27, 2017 10:21
Concise testable actions
import searchService from '../../../services/search/searchService';
import { actionTypes } from '../RepoSearchConstants';
export const fetchRepos = () => async (dispatch) => {
let error = '';
dispatch({
type: actionTypes.REQUEST_START,
});
try {
const { items } = await searchService.repoSearch();
@sandwichsudo
sandwichsudo / mock_xhr.js
Last active July 27, 2017 14:20
Added XHR
// src/xhr/__mocks__/xhr.js
let getResponse;
const __setMockGetResponse = (response) => {
getResponse = response;
};
const _get = jest.fn(() => getResponse);
@sandwichsudo
sandwichsudo / RepoSearchReducer.js
Last active July 27, 2017 14:29
Reducer case to update the error when request completes.
// src/containers/RepoSearchPage/reducer/RepoSearchReducer.js
import { actionTypes } from '../RepoSearchConstants';
const initialState = {
results: [],
error: ''
};
export default (state = initialState, action) => {
@sandwichsudo
sandwichsudo / RepoSearchReducer.spec.js
Last active July 27, 2017 14:30
Add test to update error in state.
// src/containers/RepoSearchPage/reducer/RepoSearchReducer.spec.js
import RepoSearchReducer from './RepoSearchReducer';
import { actionTypes } from '../RepoSearchConstants';
const initialState = {
results: [],
error: '',
};
@sandwichsudo
sandwichsudo / RepoSearchActions.js
Last active July 27, 2017 14:30
New action structure
// src/containers/RepoSearchPage/actions/RepoSearchActions.js
import searchService from '../../../services/search/searchService';
import { actionTypes } from '../RepoSearchConstants';
export const fetchRepos = () => async (dispatch) => {
let error = '';
try {
const { items } = await searchService.repoSearch();
dispatch({
@sandwichsudo
sandwichsudo / RepoSearchActions.spec.js
Last active July 27, 2017 14:31
Add test for action with error flow
// src/containers/RepoSearchPage/actions/RepoSearchActions.spec.js
import { fetchRepos } from './RepoSearchActions';
import searchService from '../../../services/search/searchService';
import { actionTypes } from '../RepoSearchConstants';
jest.mock('../../../services/search/searchService');
describe('repo search actions', () => {
describe('fetchRepos', () => {
@sandwichsudo
sandwichsudo / RepoSearchPage.js
Last active July 27, 2017 14:32
Add error to the state
// src/containers/RepoSearchPage/page/RepoSearchPage.js
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import RepoResultsList from '../components/RepoResultsList/RepoResultsList';
import * as RepoSearchActions from '../actions/RepoSearchActions';
export class RepoSearchPage extends Component {
@sandwichsudo
sandwichsudo / RepoSearchPage.js
Last active July 27, 2017 14:32
Call api when component mounts
// src/containers/RepoSearchPage/page/RepoSearchPage.js
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import RepoResultsList from '../components/RepoResultsList/RepoResultsList';
import * as RepoSearchActions from '../actions/RepoSearchActions';
export class RepoSearchPage extends Component {
@sandwichsudo
sandwichsudo / RepoSearchPage.js
Last active July 27, 2017 14:33
Test for calling API when component mounts
// src/containers/RepoSearchPage/page/RepoSearchPage.js
// export the class to test in isolation
export class RepoSearchPage extends Component {
constructor(props) {
super(props);
}
...
@sandwichsudo
sandwichsudo / RepoSearchReducer.js
Last active July 27, 2017 14:34
Add case to update results in the reducer.
// src/containers/RepoSearchPage/reducer/RepoSearchReducer.js
import { actionTypes } from '../RepoSearchConstants';
const initialState = {
results: [{
name: 'foo',
stargazers_count: 100,
id: '1',
},{