Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:AgileVentures/WebsiteOne-FE into…
Browse files Browse the repository at this point in the history
… 74-add-spinner-to-createBillingAgreement
  • Loading branch information
mattwr18 committed Feb 15, 2019
2 parents 17a99eb + 6195d5d commit 38a2814
Show file tree
Hide file tree
Showing 7 changed files with 11,557 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"dependencies": {
"axios": "^0.18.0",
"babel-plugin-dotenv": "^0.1.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"global": "^4.3.2",
"izitoast": "^1.4.0",
Expand Down
17 changes: 17 additions & 0 deletions src/helpers/createBillingAgreement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import axios from 'axios'

export default (cookies, id) => event => {
event.preventDefault()
return axios({
method: 'POST',
timeout: 50000,
url: '/paypal/new.json',
data: {
plan: id
},
headers: {
Authorization: cookies.get('_WebsiteOne_session')
}
})
.then(response => window.location.assign(response.data.redirect_url))
}
16 changes: 16 additions & 0 deletions src/helpers/executeBillingAgreement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import axios from 'axios'

export default (props, params) => {
return axios({
method: 'GET',
timeout: 50000,
url: '/paypal/create.json',
params: {
plan: params.plan,
token: params.token
},
headers: {
Authorization: props.cookies.get('_WebsiteOne_session')
}
})
}
43 changes: 43 additions & 0 deletions src/tests/helpers/createBillingAgreement.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import moxios from 'moxios'
import billingAgreementResponse from '../../fixtures/billingAgreementResponse'
import createBillingAgreement from '../../helpers/createBillingAgreement'

describe('createBillingAgreement helper', () => {
const cookies = { get: jest.fn() }
const event = { preventDefault: jest.fn() }
const { assign } = window.location
beforeEach(() => {
moxios.install()
Object.defineProperty(window.location, 'assign', {
configurable: true
})
window.location.assign = jest.fn()
})

afterEach(() => {
moxios.uninstall()
window.location.assign = assign
})

it('posts plan info to Rails backend and returns with redirect url', () => {
moxios.stubRequest('/paypal/new.json', {
status: 200,
response: billingAgreementResponse
})
createBillingAgreement(cookies)(event)
expect(billingAgreementResponse.data.redirect_url).toEqual(
'https://www.sandbox.paypal.com/cgi-bin/webscr'
)
})

it('calls window.location.assign to redirect to PayPal site', () => {
moxios.stubRequest('/paypal/new.json', {
status: 200,
response: billingAgreementResponse
})
createBillingAgreement(cookies)(event).then(
window.location.assign(billingAgreementResponse.data.redirect_url)
)
expect(window.location.assign).toHaveBeenCalled()
})
})
24 changes: 24 additions & 0 deletions src/tests/helpers/executeBillingAgreement.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import moxios from 'moxios'
import { executedBillingAgreementResponse } from '../../fixtures/billingAgreementResponse'
import executeBillingAgreement from '../../helpers/executeBillingAgreement'

describe('executeBillingAgreement helper', () => {
const props = { cookies: { get: jest.fn() } }
const params = { plan: 'premium', token: 'valid_token' }
beforeEach(() => {
moxios.install()
})

afterEach(() => {
moxios.uninstall()
})

it('posts plan info to Rails backend and returns with redirect url', () => {
moxios.stubRequest('/paypal/create.json', {
status: 200,
response: executedBillingAgreementResponse
})
executeBillingAgreement(props, params)
expect(executedBillingAgreementResponse).toEqual({ data: { message: 'Success' } })
})
})

0 comments on commit 38a2814

Please sign in to comment.