Last active
April 22, 2019 03:57
-
-
Save axelnormand/a80347feaab3416b0f55ac3f29849cae to your computer and use it in GitHub Desktop.
sagas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import { put, call, takeLatest } from 'redux-saga/effects'; | |
import { login as loginApi } from '../api/login'; | |
import { | |
loginActionTypes, | |
loginLoading, | |
loginComplete, | |
loginInvalid | |
} from '../reducers/login/loginActions'; | |
import type { UserLogin } from '../reducers/login/loginActions'; | |
export function* login(action: UserLogin): Generator<*, *, *> { | |
const { username, password } = action.payload; | |
yield put(loginLoading()); | |
try { | |
const loginSuccess: boolean = yield call(loginApi, username, password); | |
if (loginSuccess) { | |
yield put(loginComplete()); | |
} else { | |
yield put(loginInvalid()); | |
} | |
} catch (e) {} | |
} | |
function* watchLogin(): Generator<*, *, *> { | |
yield takeLatest(userActionTypes.login, login); | |
} | |
export default [watchLogin]; // import array into root saga to run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import { put, call } from 'redux-saga/effects'; | |
import { cloneableGenerator } from 'redux-saga/utils'; | |
import { | |
login as loginActionCreator, | |
loginInvalid, | |
loginLoading, | |
loginComplete, | |
} from '../reducers/login/loginActions'; | |
import { login as loginApi } from '../api/login'; | |
import { login } from './login'; | |
const username = 'testUser'; | |
const password = 'testPassword'; | |
const loginAction = loginActionCreator({ username, password }); | |
describe('login flow', () => { | |
const generator = cloneableGenerator(login)(loginAction); | |
expect(generator.next().value).toEqual(put(loginLoading())); | |
expect(generator.next().value).toEqual(call(loginApi, username, password)); | |
test('credentials success', () => { | |
const clone = generator.clone(); | |
expect(clone.next(true).value).toEqual(put(loginComplete())); | |
expect(clone.next().done).toEqual(true); | |
}); | |
test('or credentials invalid', () => { | |
const clone = generator.clone(); | |
expect(clone.next(false).value).toEqual(put(loginInvalid())); | |
expect(clone.next().done).toEqual(true); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment