Skip to content

Commit

Permalink
Move epic stuff to observable index, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansukale committed Jul 8, 2018
1 parent 377ae83 commit f529d6f
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 22 deletions.
5 changes: 2 additions & 3 deletions src/redux-observable/fromResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import fromResource from '../fromResource';
import generateEpic from './generateEpic';

export default function(...args) {
const tasks = fromResource.apply(null, [generateEpic, ...args]);

const tasks = fromResource(generateEpic, ...args);
const epics = Object.keys(tasks).map(taskName => tasks[taskName].epic);

tasks.epic = combineEpics(...epics);

return tasks;
}
4 changes: 2 additions & 2 deletions src/redux-observable/generateEpic.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { from, of } from 'rxjs';
import { ofType } from 'redux-observable';
import { map, filter, switchMap, catchError } from 'rxjs/operators';
import { map, switchMap, catchError } from 'rxjs/operators';
import createUrl from 'batarang/createUrl';

import ajaxObservable from './ajaxObservable';
Expand Down Expand Up @@ -90,6 +90,6 @@ export default function generateEpic(
}

deps.ajax = deps.ajax || ajaxObservable(deps.getHeaders);

return generator({ url, actions, onSuccess, onFailure, beforeSubmit }, deps);
}
17 changes: 16 additions & 1 deletion src/redux-observable/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { combineEpics } from 'redux-observable';

import processResources from '../processResources';
import generateEpic from './generateEpic';
import fromResource from './fromResource';

export default function incrudable(resources, config) {
return processResources(generateEpic, resources, config);
const sources = processResources(generateEpic, resources, config);

const epics = Object.keys(sources).reduce((acc, sourceName) => {
const tasks = sources[sourceName];
const epics = Object.keys(tasks).map(taskName => tasks[taskName].epic);
tasks.epic = combineEpics(...epics);

return acc.concat(tasks.epic);
}, []);
console.log('epics', epics);

sources.epic = combineEpics(...epics);

return sources;
}

incrudable.fromResource = fromResource;
10 changes: 1 addition & 9 deletions src/redux-thunk/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { combineEpics } from 'redux-observable';

import processResources from '../processResources';
import generateThunk from './generateThunk';
import fromResource from './fromResource';


export default function incrudable(resources, config) {
const sources = processResources(generateThunk, resources, config);

const epics = Object.keys(sources).map(resourceName => sources[resourceName].epic);
sources.epic = combineEpics(...epics);

return sources;
return processResources(generateThunk, resources, config);
}

incrudable.fromResource = fromResource;
2 changes: 1 addition & 1 deletion tests/redux-observable/fromResource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('fromResource: redux-observable', () => {
});
});

// it('creates an root epic for all the operations', done => {
// It('creates an root epic for all the operations', done => {
// const tasks = fromResource(resource, config);
// const request = { params: { id: 10 } };
// const action$ = of(tasks.create(request));
Expand Down
4 changes: 2 additions & 2 deletions tests/redux-observable/generateEpic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import generateEpic, {

function getTask(options, ajax) {
const type = options.actions.wait.toString();
const waitStub = sinon.stub(options.actions, 'wait').callThrough().toString = () => type;
sinon.stub(options.actions, 'wait').callThrough().toString = () => type;
sinon.stub(options.actions, 'success').callThrough();
sinon.stub(options.actions, 'failure').callThrough();

Expand Down Expand Up @@ -288,7 +288,7 @@ describe('generateEpic', () => {
);

const type = actions.wait.toString();
const waitStub = sinon.stub(actions, 'wait').callThrough().toString = () => type;
sinon.stub(actions, 'wait').callThrough().toString = () => type;
sinon.stub(actions, 'success').callThrough();
sinon.stub(actions, 'failure').callThrough();

Expand Down
13 changes: 9 additions & 4 deletions tests/redux-observable/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ const resources = {
describe('incrudable', () => {
it('create the actions and tasks for resources', () => {
const tasks = incrudable(resources);
expect(tasks.epic).to.be.a('function');

Object.keys(resources).forEach(resource => {
expect(tasks[resource].epic).to.be.a('function');

Object.keys(resources[resource].operations).forEach(operation => {
expect(tasks[resource][operation]).to.be.a('function');
expect(tasks[resource][operation].success).to.be.a('function');
expect(tasks[resource][operation].failure).to.be.a('function');
expect(tasks[resource][operation].wait).to.be.a('function');
const opTask = tasks[resource][operation];
expect(opTask).to.be.a('function');
expect(opTask.epic).to.be.a('function');
expect(opTask.success).to.be.a('function');
expect(opTask.failure).to.be.a('function');
expect(opTask.wait).to.be.a('function');
});
});
});
Expand Down

0 comments on commit f529d6f

Please sign in to comment.