Skip to content

Commit

Permalink
articles saga test
Browse files Browse the repository at this point in the history
  • Loading branch information
Khang authored and justinlevi committed Mar 16, 2018
1 parent c3c08bc commit af7804f
Show file tree
Hide file tree
Showing 3 changed files with 8,255 additions and 2 deletions.
9 changes: 7 additions & 2 deletions front-end/src/redux/article/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
// deleteArticle,
// saveArticleUpdates,
fetchArticlesSuccess,
fetchArticlesFailure,
createArticleSuccess,
createArticleFailure,
deleteArticleSuccess,
Expand All @@ -23,8 +24,12 @@ function* fetchArticlesSaga() {
yield put(tokensExpiredCheck());
yield take(oauthActionTypes.TOKENS_EXPIRED_CHECK_VALID);

const result = yield call(articlesByUser);
yield put(fetchArticlesSuccess({ articles: formatFetchArticlesResult(result) }));
try{
const result = yield call(articlesByUser);
yield put(fetchArticlesSuccess({ articles: formatFetchArticlesResult(result) }));
}catch(error){
yield put(fetchArticlesFailure(`${error}`))
}
}

function* createArticleSaga(action) {
Expand Down
70 changes: 70 additions & 0 deletions front-end/src/redux/article/sagas.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import createSagaMiddleware from 'redux-saga';
import configureMockStore from 'redux-mock-store';
import * as sagas from './sagas';
import * as actions from './actions';
import * as oauthActions from '../auth/oauth/actions';

import { introspectionQuery, buildClientSchema, graphql } from 'graphql';
import { addMockFunctionsToSchema } from 'graphql-tools';
import { print } from 'graphql/language/printer';

import * as introspectionResult from '../../api/schema.json';

import {
CURRENT_USER_QUERY,
ARTICLES_BY_USER_QUERY,
} from '../../api/apolloProxy';

const sagaMiddleware = createSagaMiddleware();
const mockStore = configureMockStore([sagaMiddleware]);

describe('the sagas', () => {

const schema = buildClientSchema(introspectionResult);
addMockFunctionsToSchema({ schema });

it('should execute the fetchArticlesSaga action creator and fail the call to articlesByUser', (done) => {
const store = mockStore({});
sagaMiddleware.run(sagas.watchArticleActions); // has to be executed after the mockStore() call

const expectedActions = [
{ type: 'FETCH_ARTICLES' },
{ type: 'TOKENS_EXPIRED_CHECK' },
{ type: 'TOKENS_EXPIRED_CHECK_VALID' },
{
error: 'Error: Network error: Network request failed',
type: 'FETCH_ARTICLES_FAILURE'
}
];

store.subscribe(() => {
const storeActions = store.getActions();
let refreshCheck;

storeActions.forEach(function(item){
if(item.type === 'TOKENS_EXPIRED_CHECK'){
refreshCheck = 'valid';
}else{
refreshCheck = 'invalid';
}
})

if(refreshCheck === 'valid'){
store.dispatch({
type: oauthActions.TOKENS_EXPIRED_CHECK_VALID,
});
}

graphql(schema, print(ARTICLES_BY_USER_QUERY), null).then((result) => {
if(storeActions.length >= expectedActions.length){
expect(storeActions.sort()).toEqual(expectedActions.sort());
done();
}
});
});

store.dispatch({
type: actions.FETCH_ARTICLES,
});
});
});
Loading

0 comments on commit af7804f

Please sign in to comment.