Skip to content

Instantly share code, notes, and snippets.

@JasonOffutt
Last active October 12, 2017 23:04
Show Gist options
  • Save JasonOffutt/0a2349beb15902be0a3af1ed0221f128 to your computer and use it in GitHub Desktop.
Save JasonOffutt/0a2349beb15902be0a3af1ed0221f128 to your computer and use it in GitHub Desktop.
// components/search/factory.js
// Grab singleton instance of DI container
import { container } from './depdendencies';
import actionFactory from '../../actions';
export const createActions = () => {
const http = container.instance('HTTP');
return actionFactory(http);
};
// actions/index.js
import search from './search';
export default (http) => ({
search: search(http)
});
// actions/search.js
const requestResults = () => ({ type: 'SEARCH_REQUEST' });
const receiveResults = (response) => ({ type: 'SEARCH_RESPONSE', response });
const requestError = (response) => ({ type: 'SEARCH_ERROR', response });
const createQuery (http) => {
return (dispatch) => {
return (query) => {
const url = `/api/search?q=${query}`;
dispatch(requestResults());
return http(url).get()
.then((response) => dispatch(receiveResults(response))
.catch((response) => dispatch(requestError(response));
};
};
};
export default (http) => ({
query: createQuery(http)
});
// test/spec/actions/search.js
import actionFactory from '../../src/actions';
import httpStub from '../stubs/http';
const actions = actionFactory(httpStub);
describe('Search action tests', () => {
describe('query', () => {
it('should do something useful', () => {
// test things, etc...
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment