Skip to content

Commit

Permalink
feat(sagas): Add transaction of fetching comments after fetch or crea…
Browse files Browse the repository at this point in the history
…te job successfully
  • Loading branch information
abalone0204 committed Jun 8, 2016
1 parent ecd5d3e commit 57e58f9
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 17 deletions.
32 changes: 27 additions & 5 deletions front-end/app/sagas/fetchJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import {
SUCCESS_FETCH_JOB
} from '../actions/fetchJob.js'

import {
REQUEST_CREATE_JOB
} from '../actions/createJob.js'

import {
REQUEST_FETCH_COMMENTS
} from '../actions/fetchComments.js'

import fetchJob from '../API/fetchJob.js'

export function* watchRequestFetchJob() {
Expand All @@ -21,15 +29,29 @@ export function* watchRequestFetchJob() {

export function* fetchJobFlow(action) {
try {

const job = yield call(fetchJob, action.query)
yield put({
type: SUCCESS_FETCH_JOB,
job
})
yield call(jobHandler, job, action)

} catch (error) {
yield put({
type: FAIL_TO_FETCH_JOB,
error
})
}
}
}

export function* jobHandler(job, action) {
if (job === null) {
yield put({
type: REQUEST_CREATE_JOB,
params: action.query
})
} else {
yield put({
type: SUCCESS_FETCH_JOB,
job
})
}
}

7 changes: 6 additions & 1 deletion front-end/app/sagas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import {watchRequestLeaveComment} from './leaveComment.js'
import {watchRequestFetchComments} from './fetchComments.js'
import {watchRequestFetchJob} from './fetchJob.js'
import {watchRequestCreateJob} from './createJob.js'
import {
transactionOfJobAndComments
} from './transactionFlows.js'

export default function* rootSaga() {
yield [
watchRequestLogin(),
watchRequestLeaveComment(),
watchRequestFetchComments(),
watchRequestFetchJob(),
watchRequestCreateJob()
watchRequestCreateJob(),
transactionOfJobAndComments()
]
}
4 changes: 0 additions & 4 deletions front-end/app/sagas/leaveComment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import {
import checkAccessToken from '../API/checkAccessToken.js'
import createComment from '../API/createComment.js'

import {

} from '../actions/fetchComments.js'

import {
REQUEST_LEAVE_COMMENT,
FAIL_TO_LEAVE_COMMENT,
Expand Down
28 changes: 28 additions & 0 deletions front-end/app/sagas/transactionFlows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
takeEvery
} from 'redux-saga'

import {
put,
call
} from 'redux-saga/effects'

import {
SUCCESS_CREATE_JOB
} from '../actions/createJob.js'

import {
SUCCESS_FETCH_JOB
} from '../actions/fetchJob.js'

import {
requestFetchComments
} from '../actions/fetchComments.js'

export function* transactionOfJobAndComments() {
yield call(takeEvery, [SUCCESS_CREATE_JOB, SUCCESS_FETCH_JOB], sendRequestToFetchComments)
}

export function* sendRequestToFetchComments(action) {
yield put(requestFetchComments(action.job.id))
}
57 changes: 50 additions & 7 deletions tests/front-end/sagas/fetchJob.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ import {
SUCCESS_FETCH_JOB
} from 'actions/fetchJob.js'

import {
REQUEST_CREATE_JOB
} from 'actions/createJob.js'

import {
watchRequestFetchJob,
fetchJobFlow
fetchJobFlow,
jobHandler
} from 'sagas/fetchJob.js'

import fetchJob from 'API/fetchJob.js'
Expand Down Expand Up @@ -52,26 +57,64 @@ describe('Sagas/ fetchJob', () => {
assert.deepEqual(expected, actual)
})

it('should put success fetch job effect', () => {
it('should call jobHandler', () => {
const job = {
id: '123123',
e04_job_no: '123$3#',
company_name: 'goo',
job_name: 'work'
}
const expected = put({
type: SUCCESS_FETCH_JOB,
job
})
const expected = call(jobHandler, job, action)
const actual = iterator.next(job).value
assert.deepEqual(expected, actual)
})

it('should handle error', () => {
const error = new Error('mock')
const expected = put({type: FAIL_TO_FETCH_JOB, error})
const expected = put({
type: FAIL_TO_FETCH_JOB,
error
})
const actual = iterator.throw(error).value
assert.deepEqual(expected, actual)
})
describe('jobHandler', () => {
describe('if job is null', () => {
const iterator = jobHandler(null, action)
it('should put request create job effect, if the job is null', () => {
const expected = put({
type: REQUEST_CREATE_JOB,
params: action.query
})
const actual = iterator.next().value
assert.deepEqual(expected, actual)
})
})

describe('if job is found', () => {
const job = {
id: '123123',
e04_job_no: '123$3#',
company_name: 'goo',
job_name: 'work'
}
const iterator = jobHandler(job, action)

it('should put success fetched job effect, if the job is found', () => {
const expected = put({
type: SUCCESS_FETCH_JOB,
job
})
const actual = iterator.next().value
assert.deepEqual(expected, actual)
})
})




})
})


})
43 changes: 43 additions & 0 deletions tests/front-end/sagas/transactionFlows.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'babel-polyfill'

import {
assert
} from 'chai'

import {
takeEvery
} from 'redux-saga'

import {
put,
call
} from 'redux-saga/effects'

import {
SUCCESS_CREATE_JOB
} from 'actions/createJob.js'

import {
SUCCESS_FETCH_JOB
} from 'actions/fetchJob.js'

import {
requestFetchComments
} from 'actions/fetchComments.js'

import {
transactionOfJobAndComments,
sendRequestToFetchComments
} from 'sagas/transactionFlows'

describe('Sagas/ Transaction flows', () => {
describe('transactionOfJobAndComments', () => {
const iterator = transactionOfJobAndComments()

it('should watch success effect of fetching and creating job, and call sendRequestToFetchComments', () => {
const expected = call(takeEvery, [SUCCESS_CREATE_JOB, SUCCESS_FETCH_JOB], sendRequestToFetchComments)
const actual = iterator.next().value
assert.deepEqual(expected, actual)
})
})
})

0 comments on commit 57e58f9

Please sign in to comment.