Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions frontend/src/actions/projectActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const fetchProjectError = (error) => {
const fetchProject = (projectId, params) => {
return (dispatch) => {
dispatch(fetchProjectRequested())
axios
return axios
.get(api.API_URL + `/projects/fetch/${projectId}`, { params })
.then((project) => {
if (project.data) {
Expand All @@ -40,11 +40,11 @@ const fetchProject = (projectId, params) => {
})
.catch((e) => {
dispatch(addNotification('actions.task.fetch.other.error', { severity: 'error' }))
dispatch(fetchProjectError(e))
// eslint-disable-next-line no-console
console.log('not possible to fetch issue')
// eslint-disable-next-line no-console
console.log(e)
return dispatch(fetchProjectError(e))
})
}
}
Expand All @@ -68,7 +68,7 @@ const listProjectsError = (error) => {
const listProjects = () => {
return (dispatch) => {
dispatch(listProjectsRequested())
axios
return axios
.get(api.API_URL + '/projects/list')
.then((projects) => {
if (projects.data) {
Expand All @@ -79,11 +79,11 @@ const listProjects = () => {
})
.catch((e) => {
dispatch(addNotification('actions.task.fetch.other.error', { severity: 'error' }))
dispatch(listProjectsError(e))
// eslint-disable-next-line no-console
console.log('not possible to fetch issue')
// eslint-disable-next-line no-console
console.log(e)
return dispatch(listProjectsError(e))
})
}
}
Expand Down
71 changes: 71 additions & 0 deletions frontend/tests/actions/projectActions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import axios from 'axios'
import { applyMiddleware, combineReducers, createStore } from 'redux'
import { thunk } from 'redux-thunk'
import { fetchProject, listProjects } from '../../src/actions/projectActions'
import { project, projects } from '../../src/reducers/projectReducer'

jest.mock('axios')

describe.each([
[
'fetchProject',
() => fetchProject(7, { status: 'open' }),
'project',
{ id: 7 },
'FETCH_PROJECT'
],
['listProjects', () => listProjects(), 'projects', [{ id: 7 }], 'LIST_PROJECTS']
])('%s completion', (name, action, stateKey, responseData, prefix) => {
let store

beforeEach(() => {
jest.clearAllMocks()
store = createStore(
combineReducers({ project, projects, intl: () => ({ messages: {} }) }),
applyMiddleware(thunk)
)
jest.spyOn(console, 'log').mockImplementation(() => {})
})

afterEach(() => jest.restoreAllMocks())

it('waits for the HTTP response and the reducer update before resolving', async () => {
let resolveRequest
axios.get.mockReturnValue(
new Promise((resolve) => {
resolveRequest = resolve
})
)

const completion = store.dispatch(action())
expect(completion).toEqual(expect.any(Promise))
let settled = false
completion.then(() => {
settled = true
})
await Promise.resolve()
expect(settled).toBe(false)
expect(store.getState()[stateKey].completed).toBe(false)

resolveRequest({ data: responseData })
await expect(completion).resolves.toMatchObject({ type: `${prefix}_SUCCESS` })
expect(store.getState()[stateKey]).toMatchObject({ completed: true, data: responseData })
})

it('resolves to the error action after a network failure is reduced', async () => {
const error = new Error('Network Error')
axios.get.mockRejectedValue(error)
await expect(store.dispatch(action())).resolves.toMatchObject({
type: `${prefix}_ERROR`,
error
})
expect(store.getState()[stateKey]).toMatchObject({ completed: true, error })
})

it('resolves to the error action when the response has no project data', async () => {
axios.get.mockResolvedValue({ data: null })
await expect(store.dispatch(action())).resolves.toMatchObject({ type: `${prefix}_ERROR` })
expect(store.getState()[stateKey].completed).toBe(true)
expect(store.getState()[stateKey].error).toEqual({ message: 'actions.task.fetch.unavailable' })
})
})