Skip to content

Commit

Permalink
Make tests more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 committed Feb 12, 2019
1 parent 03faf9e commit 3a55b81
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/actions/createBillingAgreement.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default (cookies, dispatch) => event => {
event.preventDefault()
return axios({
method: 'POST',
timeout: 50000,
timeout: 30000,
url: '/paypal/new.json',
data: {
plan: 1
Expand All @@ -16,9 +16,9 @@ export default (cookies, dispatch) => event => {
})
.then(response => window.location.assign(response.data.redirect_url))
.catch(error => {
dispatch(dispatch({
dispatch({
type: CREATE_BILLING_AGREEMENT_FAILURE,
message: error.message
}))
})
})
}
7 changes: 2 additions & 5 deletions src/actions/executeBillingAgreement.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import axios from 'axios'
import { EXECUTE_BILLING_AGREEMENT_FAILURE } from '../types'

export default (cookies, params, dispatch) => {
console.log('cookies', cookies)
console.log('params', params)
console.log('dispatch', dispatch)
return axios({
method: 'GET',
timeout: 50000,
url: '/paypal/create.json',
timeout: 20000,
url: '/paypal/create',
params: {
plan: params.plan,
token: params.token
Expand Down
7 changes: 4 additions & 3 deletions src/tests/actions/createBillingAgreement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import thunk from 'redux-thunk'
import configureMockStore from 'redux-mock-store'
import { CREATE_BILLING_AGREEMENT_FAILURE } from '../../types'

describe('createBillingAgreement helper', () => {
describe('createBillingAgreement action', () => {
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
let store
Expand Down Expand Up @@ -50,7 +50,8 @@ describe('createBillingAgreement helper', () => {
expect(window.location.assign).toHaveBeenCalledWith(billingAgreementResponse.data.redirect_url)
})

it('dispatches if an error is returned', () => {
it('dispatches if an error is returned', async () => {
expect.assertions(1)
const error = new Error('Error: Request failed with status code 500')
const errorMessage = {
type: CREATE_BILLING_AGREEMENT_FAILURE,
Expand All @@ -62,7 +63,7 @@ describe('createBillingAgreement helper', () => {
})

store.dispatch(errorMessage)
createBillingAgreement(cookies, dispatch)(event).then(() => {
await createBillingAgreement(cookies, dispatch)(event).then(() => {
expect(store.getActions()).toEqual([errorMessage])
})
})
Expand Down
41 changes: 30 additions & 11 deletions src/tests/actions/executeBillingAgreement.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,63 @@ import thunk from 'redux-thunk'
import configureMockStore from 'redux-mock-store'
import { EXECUTE_BILLING_AGREEMENT_FAILURE } from '../../types'

describe('executeBillingAgreement helper', () => {
describe('executeBillingAgreement action', () => {
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
let store
const cookies = { get: jest.fn() }
const params = { plan: 'premium', token: 'valid_token' }
const params = { plan: 'premium', token: '' }
const dispatch = jest.fn()
beforeEach(() => {
beforeEach(done => {
moxios.install()
store = mockStore({})
done()
})

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

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

moxios.wait(() => {
const request = moxios.requests.mostRecent()
request.resolve(executedBillingAgreementResponse)
})

await executeBillingAgreement(cookies, params, dispatch).then(() => {
expect(executedBillingAgreementResponse).toEqual({
data: { message: 'Success' }
})
})
})

it('dispatches if an error is returned', () => {
it('dispatches if an error is returned', async () => {
expect.assertions(1)
const error = new Error('Error: Request failed with status code 500')
const errorMessage = {
type: EXECUTE_BILLING_AGREEMENT_FAILURE,
message: error
message: error.message
}

moxios.stubRequest('/paypal/create.json', {
status: 500,
response: { errorMessage }
response: error.message
})

moxios.wait(() => {
const request = moxios.requests.mostRecent()
request.reject(errorMessage)
})

store.dispatch(errorMessage)
executeBillingAgreement(cookies, params, dispatch).then(() => {
expect(store.getActions()).toEqual(errorMessage)
await executeBillingAgreement(cookies, params, dispatch).then(() => {
expect(store.getActions()).toEqual([errorMessage])
})
})
})
34 changes: 27 additions & 7 deletions src/tests/containers/ProjectsList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('ProjectsList', () => {
projects={paginatedProjectsFixture}
fetchProjects={() => {}}
setLastLocation={() => {}}
location={{ pathname: '/projects' }}
/>
)
wrapper.setState(
Expand Down Expand Up @@ -92,15 +93,25 @@ describe('ProjectsList', () => {
it("shouldn't render a Project component without projects", () => {
const wrapper = mount(
<StaticRouter context={context}>
<ProjectsList projects={{ 1: [] }} fetchProjects={() => {}} setLastLocation={() => {}} />
<ProjectsList
projects={{ 1: [] }}
fetchProjects={() => {}}
setLastLocation={() => {}}
location={{ pathname: '/projects' }}
/>
</StaticRouter>
)
expect(wrapper.find('Project')).toHaveLength(0)
})

it('should test componentWillReceiveProps', () => {
const wrapper = shallow(
<ProjectsList projects={[]} fetchProjects={() => {}} setLastLocation={() => {}} />
<ProjectsList
projects={[]}
fetchProjects={() => {}}
setLastLocation={() => {}}
location={{ pathname: '/projects' }}
/>
)
wrapper.setProps({ projects: [{ id: 1, languages: [] }] })
expect(wrapper.instance().state.projects).toEqual({
Expand All @@ -110,12 +121,15 @@ describe('ProjectsList', () => {

it('should call normalizeFilteredProjects', () => {
const wrapper = shallow(
<ProjectsList projects={[]} fetchProjects={() => {}} setLastLocation={() => {}} />
<ProjectsList
projects={[]}
fetchProjects={() => {}}
setLastLocation={() => {}}
location={{ pathname: '/projects' }}
/>
)
wrapper.setProps({ projects: projectsFixture })
expect(wrapper.instance().state.projects).toEqual(
paginatedProjectsFixture
)
expect(wrapper.instance().state.projects).toEqual(paginatedProjectsFixture)
})

it('should filter projects', () => {
Expand All @@ -134,7 +148,13 @@ describe('ProjectsList', () => {

it('adds error to the state if fetchProjects fails', async () => {
const wrapper = shallow(
<ProjectsList projects={[]} error={[]} fetchProjects={() => {}} setLastLocation={() => {}} location={{ pathname: '/projects' }} />
<ProjectsList
projects={[]}
error={[]}
fetchProjects={() => {}}
setLastLocation={() => {}}
location={{ pathname: '/projects' }}
/>
)
await wrapper.instance().componentDidMount()
expect(wrapper.state().error).toEqual(true)
Expand Down

0 comments on commit 3a55b81

Please sign in to comment.