Skip to content

Commit

Permalink
refactor(typescript): tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akameco committed Mar 11, 2019
1 parent 885b580 commit cbe4232
Show file tree
Hide file tree
Showing 15 changed files with 104 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,48 @@
// @flow
import { call, put } from 'redux-saga/effects'
import * as api from 'services/api'
import { getToken } from 'containers/LoginModal/saga'
import * as sagas from '../sagas'
import * as actions from '../actions'

test('post', () => {
const gen = sagas.post('endpoint', { dummy: 1 })

const gen = sagas.post('endpoint', {
dummy: 1,
})
let next = gen.next()
expect(next.value).toStrictEqual(call(getToken))

const token = 'fake token'
next = gen.next(token)
expect(next.value).toStrictEqual(
call(api.postRequest, 'endpoint', { dummy: 1 }, token)
call(
api.postRequest,
'endpoint',
{
dummy: 1,
},
token
)
)
})

test('get', () => {
const gen = sagas.get('endpoint', true)

let next = gen.next()
expect(next.value).toStrictEqual(call(getToken))

const token = 'fake token'
next = gen.next(token)
expect(next.value).toStrictEqual(call(api.getRequest, 'endpoint', {}, token))
expect(next.value).toStrictEqual(call(api.getRequest, 'endpoint', {}, token)) // $FlowFixMe

// $FlowFixMe
next = gen.next({ entities: {}, result: {} })
next = gen.next({
entities: {},
result: {},
})
expect(next.value).toStrictEqual(
// $FlowFixMe
put(actions.apiRequestSuccess({ entities: {}, result: {} }))
put(
actions.apiRequestSuccess({
entities: {},
result: {},
})
)
)
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow
import { takeEvery, put } from 'redux-saga/effects'
import * as sagas from '../saga'
import * as constants from '../constants'
Expand All @@ -13,23 +12,25 @@ test('root', () => {
})

test('bookmark', () => {
const action = { id: 1, restrict: 'public' }
const action = {
id: 1,
restrict: 'public',
}
const gen = sagas.bookmark(action)

let next = gen.next()
expect(next.value).toMatchSnapshot()

next = gen.next()
expect(next.value).toStrictEqual(put(actions.addBookmarkSuccess(1, 'public')))
})

test('bookmark failed', () => {
const action = { id: 1, restrict: 'public' }
const action = {
id: 1,
restrict: 'public',
}
const gen = sagas.bookmark(action)

let next = gen.next()
expect(next.value).toMatchSnapshot()

next = gen.throw('error')
expect(next.value).toStrictEqual(put(actions.addBookmarkFailer(1, 'error')))
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow
import * as sagas from '../saga'

test('root', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow
import { takeEvery } from 'redux-saga/effects'
import { ADD_BOOKMARK_SUCCESS } from 'containers/BookmarkButton/constants'
import * as sagas from '../saga'
Expand All @@ -10,46 +9,59 @@ test('root', () => {
expect(next.value).toStrictEqual(
takeEvery(constants.ADD_COLUMN, sagas.addColumn)
)

next = gen.next()
expect(next.value).toStrictEqual(
takeEvery([constants.FETCH, constants.FETCH_NEXT], sagas.fetchBookmark)
)

next = gen.next()
expect(next.value).toStrictEqual(
takeEvery(ADD_BOOKMARK_SUCCESS, sagas.fetchNew)
)
})

test('fetch next', () => {
const gen = sagas.fetchBookmark({ id: 'public', type: constants.FETCH })
const gen = sagas.fetchBookmark({
id: 'public',
type: constants.FETCH,
})
const next = gen.next()
expect(next.value).toMatchSnapshot()
expect(gen.next({ nextUrl: 'fake' }).value).toMatchSnapshot()
expect(
gen.next({
nextUrl: 'fake',
}).value
).toMatchSnapshot()
})

test('fetch first', () => {
const gen = sagas.fetchBookmark({ id: 'public', type: constants.FETCH })
const gen = sagas.fetchBookmark({
id: 'public',
type: constants.FETCH,
})
let next = gen.next()

expect(next.value).toMatchSnapshot()

next = gen.next({ id: '1' })
next = gen.next({
id: '1',
})
expect(next.value).toMatchSnapshot()

next = gen.next({ ids: [1, 2, 3] })
next = gen.next({
ids: [1, 2, 3],
})
expect(next.value).toMatchSnapshot()
})

test('new', () => {
const gen = sagas.fetchNew({ restrict: 'public' })
let next = gen.next({ id: 'public' })
const gen = sagas.fetchNew({
restrict: 'public',
})
let next = gen.next({
id: 'public',
})
expect(next.value).toMatchSnapshot()

next = gen.next({ ids: [1, 2, 3] })
next = gen.next({
ids: [1, 2, 3],
})
expect(next.value).toMatchSnapshot()

next = gen.next('myid')
expect(next.value).toMatchSnapshot()
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// @flow
import { takeEvery } from 'redux-saga/effects'
import * as sagas from '../saga'
import * as constants from '../constants'

test('root', () => {
const gen = sagas.default()
const next = gen.next()

expect(next.value).toStrictEqual(
takeEvery(constants.ADD_COLUMN, sagas.addFollowColumn)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow
import { takeEvery, put } from 'redux-saga/effects'
import { OPEN_ILLUST_VIEWER } from 'containers/IllustPreview/constants'
import { OPEN_MANGA_PREVIEW } from 'containers/MangaPreview/constants'
Expand All @@ -8,21 +7,20 @@ import * as actions from '../actions'

test('root Saga', () => {
const gen = sagas.default()

let next = gen.next()
expect(next.value).toStrictEqual(
takeEvery(Actions.ADD_COLUMN_HISTORY, sagas.addHistoryColumn)
)

next = gen.next()
expect(next.value).toStrictEqual(
takeEvery([OPEN_ILLUST_VIEWER, OPEN_MANGA_PREVIEW], sagas.addHistory)
)
})

test('add history', () => {
const gen = sagas.addHistory({ id: 1 })

const gen = sagas.addHistory({
id: 1,
})
const next = gen.next()
expect(next.value).toStrictEqual(put(actions.addHistory(1)))
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow
import { takeEvery, take, call } from 'redux-saga/effects'
import * as sagas from '../saga'
import * as constants from '../constants'
Expand All @@ -17,8 +16,16 @@ test('root', () => {
test('watchNewIllust', () => {
const gen = sagas.watchNewIllust()
expect(gen.next().value).toStrictEqual(take(constants.START_WATCH))
expect(gen.next({ id: 1 }).value).toMatchSnapshot()
expect(gen.next().value).toMatchSnapshot()
// $FlowFixMe
expect(gen.next().value).toStrictEqual(call(sagas.fetchNew, { id: 1 }))
expect(
gen.next({
id: 1,
}).value
).toMatchSnapshot()
expect(gen.next().value).toMatchSnapshot() // $FlowFixMe

expect(gen.next().value).toStrictEqual(
call(sagas.fetchNew, {
id: 1,
})
)
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow
import { takeEvery } from 'redux-saga/effects'
import * as sagas from '../saga'
import * as constants from '../constants'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow
import { takeEvery } from 'redux-saga/effects'
import * as sagas from '../saga'
import * as constants from '../constants'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow
import * as effects from 'redux-saga/effects'
import * as sagas from '../saga'
import * as actions from '../actions'
Expand All @@ -15,19 +14,17 @@ test('root', () => {
test('usersIn', () => {
const gen = sagas.usersIn()
const id = 'fate'

let next = gen.next()
expect(next.value).toStrictEqual(effects.take(constants.USERS_IN))

next = gen.next({ id: 'fate1000users入り', usersIn: 100 })
next = gen.next({
id: 'fate1000users入り',
usersIn: 100,
})
expect(next.value).toStrictEqual(effects.put(actions.setUsersIn(id, 100)))

next = gen.next()
expect(next.value).toStrictEqual(effects.put(actions.resetIds(id)))

next = gen.next()
expect(next.value).toStrictEqual(effects.put(actions.setNextUrl(id, null)))

next = gen.next()
expect(next.value).toStrictEqual(effects.put(actions.fetch(id)))
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @flow
import { takeEvery } from 'redux-saga/effects'
import * as sagas from '../saga'
import * as constants from '../constants'
Expand All @@ -9,12 +8,10 @@ test('root', () => {
expect(next.value).toStrictEqual(
takeEvery(constants.ADD_COLUMN, sagas.addColumn)
)

next = gen.next()
expect(next.value).toStrictEqual(
takeEvery(constants.FETCH, sagas.fetchUserIllust)
)

next = gen.next()
expect(next.value).toStrictEqual(
takeEvery(constants.FETCH_NEXT, sagas.fetchNextUserIllust)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,56 @@
// @flow
import { takeEvery, put } from 'redux-saga/effects'
import * as sagas from '../saga'
import * as constants from '../constants'
import * as actions from '../actions'

test('root', () => {
const gen = sagas.default()

let next = gen.next()
expect(next.value).toStrictEqual(
takeEvery(constants.FOLLOW_REQUEST, sagas.follow)
)

next = gen.next()
expect(next.value).toStrictEqual(
takeEvery(constants.UN_FOLLOW_REQUEST, sagas.unfollow)
)
})

test('follow success', () => {
const gen = sagas.follow({ id: 1 })

const gen = sagas.follow({
id: 1,
})
let next = gen.next()
expect(next.value).toMatchSnapshot()

next = gen.next()
expect(next.value).toStrictEqual(put(actions.followSuccess('public')))
})

test('follow failer', () => {
const gen = sagas.follow({ id: 1 })

const gen = sagas.follow({
id: 1,
})
let next = gen.next()
expect(next.value).toMatchSnapshot()

next = gen.throw('err')
expect(next.value).toStrictEqual(put(actions.followFailer('err')))
})

test('unfollow success', () => {
const gen = sagas.unfollow({ id: 1 })

const gen = sagas.unfollow({
id: 1,
})
let next = gen.next()
expect(next.value).toMatchSnapshot()

next = gen.next()
expect(next.value).toStrictEqual(put(actions.unFollowSuccess('public')))
})

test('unfollow failer', () => {
const gen = sagas.unfollow({ id: 1 })

const gen = sagas.unfollow({
id: 1,
})
let next = gen.next()
expect(next.value).toMatchSnapshot()

next = gen.throw('err')
expect(next.value).toStrictEqual(put(actions.unFollowFailer('err')))
})
Loading

0 comments on commit cbe4232

Please sign in to comment.