Skip to content

Instantly share code, notes, and snippets.

@ccorcos
Created August 29, 2016 23:34
Show Gist options
  • Save ccorcos/c336f4a2b911a8d786729662b9402e8f to your computer and use it in GitHub Desktop.
Save ccorcos/c336f4a2b911a8d786729662b9402e8f to your computer and use it in GitHub Desktop.
// actions/sync.js
export const makePaymentSent = () => ({
type: defs.MAKE_PAYMENT_SENT,
})
export const makePaymentFailed = (error) => ({
type: defs.MAKE_PAYMENT_FAILED,
error,
})
export const makePaymentSuccess = (loan) => ({
type: defs.MAKE_PAYMENT_SUCCESS,
loan,
})
// actions/async.js
import * as sync from './sync'
export function makePayment(loanId, amount, paymentMethodId) {
return async dispatch => {
dispatch(sync.makePaymentSent())
const response = await api.makePayment(loanId, amount, paymentMethodId)
if (response.status !== 200) {
dispatch(sync.makePaymentFailed(response.statusText))
} else {
const result = await response.json()
dispatch(sync.makePaymentSuccess(result.data))
}
}
}
// actions/index.js
export * from './async'
export * from './sync'
// actions/test.js
import expect from 'expect'
import mockApi from 'crm/src/api/mocks'
import * as actions from 'crm/src/actions'
describe('makePayment(loanId, amount, paymentMethodId)', () => {
it('onSuccess', async () => {
const dispatch = expect.createSpy()
const loanId = Symbol()
const amount = Symbol()
const paymentMethodId = Symbol()
const loan = Symbol()
const apiMakePayment = mockApi.makePayment.success(loan)
await actions.fetchUserLoans(loanId, amount, paymentMethodId)(dispatch)
expect(dispatch).toHaveBeenCalledWith(actions.makePaymentSent())
expect(apiMakePayment).toHaveBeenCalledWith(loanId, amount, paymentMethodId)
expect(dispatch).toHaveBeenCalledWith(actions.makePaymentSuccess(loan))
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment