Skip to content

Commit

Permalink
Verify custom onSuccess for generateThunk
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansukale committed Jun 4, 2018
1 parent 43346ab commit 9601852
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/createActionMethodNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const DEFAULT_OPERATIONS = ['create', 'read', 'update', 'del', 'list'];
export function getMethodName(base, operation) {
// TODO: capitalize first char of base
switch (operation) {
case: 'list':
case 'list':
return `read${base}List`;
default:
return `${operation}${base}`;
Expand All @@ -12,4 +12,4 @@ export function getMethodName(base, operation) {

export default function createActionMethodNames(base, operations = DEFAULT_OPERATIONS) {
return operations.map(getMethodName.bind(base));
}
}
57 changes: 40 additions & 17 deletions tests/redux-thunk/generateThunk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ describe('generateThunk', function () {
})
});

it('generates thunk for `read` operation that dispatches all the actions', function () {
it('generates thunk for `update` operation that dispatches all the actions', function () {
const options = {
operation: 'read',
operation: 'update',
actions: createMockActions(),
url: '/users/:id'
};
Expand All @@ -70,15 +70,14 @@ describe('generateThunk', function () {
expect(options.actions.success.calledOnce).to.equal(true);
expect(dispatch.calledTwice).to.equal(true);

expect(config.ajax.getJSON.calledOnce);
expect(config.ajax.getJSON.firstCall.args[0]).to.equal('/users/10');
expect(config.ajax.putJSON.calledOnce);
expect(config.ajax.putJSON.firstCall.args[0]).to.equal('/users/10');
})
});


it('generates thunk for `update` operation that dispatches all the actions', function () {
it('generates thunk for `delete` operation that dispatches all the actions', function () {
const options = {
operation: 'update',
operation: 'del',
actions: createMockActions(),
url: '/users/:id'
};
Expand All @@ -92,33 +91,33 @@ describe('generateThunk', function () {
expect(options.actions.success.calledOnce).to.equal(true);
expect(dispatch.calledTwice).to.equal(true);

expect(config.ajax.putJSON.calledOnce);
expect(config.ajax.putJSON.firstCall.args[0]).to.equal('/users/10');
expect(config.ajax.delJSON.calledOnce);
expect(config.ajax.delJSON.firstCall.args[0]).to.equal('/users/10');
})
});

it('generates thunk for `delete` operation that dispatches all the actions', function () {
it('generates thunk for `list` operation that dispatches all the actions', function () {
const options = {
operation: 'del',
operation: 'list',
actions: createMockActions(),
url: '/users/:id'
url: '/users'
};
const config = {ajax: createMockAjax()};
const dispatch = sinon.spy();
const thunk = generateThunk(options, config)({params: {id: '10'}});
const thunk = generateThunk(options, config)({query: {sort_by: 'age'}});

return thunk(dispatch)
.then(() => {
expect(options.actions.wait.calledOnce).to.equal(true);
expect(options.actions.success.calledOnce).to.equal(true);
expect(dispatch.calledTwice).to.equal(true);

expect(config.ajax.delJSON.calledOnce);
expect(config.ajax.delJSON.firstCall.args[0]).to.equal('/users/10');
})
expect(config.ajax.getJSON.calledOnce);
expect(config.ajax.getJSON.firstCall.args[0]).to.equal('/users?sort_by=age');
});
});

it('generates thunk for `list` operation that dispatches all the actions', function () {
it('invokes custom onSuccess', function () {
const options = {
operation: 'list',
actions: createMockActions(),
Expand All @@ -138,4 +137,28 @@ describe('generateThunk', function () {
expect(config.ajax.getJSON.firstCall.args[0]).to.equal('/users?sort_by=age');
});
});

it('generates thunk for `read` operation that dispatches all the actions', function () {
const options = {
operation: 'read',
actions: createMockActions(),
url: '/users/:id',
onSuccess: sinon.spy()
};
const request = {params: {id: '10'}};
const config = {ajax: createMockAjax()};
const dispatch = sinon.spy();
const thunk = generateThunk(options, config)(request);

return thunk(dispatch)
.then(() => {
expect(options.onSuccess.calledOnce);

const args = options.onSuccess.firstCall.args;

expect(args[0]).to.have.all.keys('actions', 'dispatch', 'onError', 'done');
expect(args[1]).to.equal(request);
expect(args[2]).to.equal('getJSON');
})
});
});
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ module.exports = {
'process.env.NODE_ENV': JSON.stringify(NODE_ENV)
})
]
};
};

0 comments on commit 9601852

Please sign in to comment.