mirror of
https://github.com/wassname/talk.git
synced 2026-07-05 01:26:22 +08:00
Adding and testing function to fetch assets in talk reducer.
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
export const SHOW_BANUSER_DIALOG = 'SHOW_BANUSER_DIALOG';
|
||||
export const HIDE_BANUSER_DIALOG = 'HIDE_BANUSER_DIALOG';
|
||||
export const USER_BAN_SUCESS = 'USER_BAN_SUCESS';
|
||||
export const USER_BAN_SUCCESS = 'USER_BAN_SUCCESS';
|
||||
|
||||
@@ -32,7 +32,7 @@ export default (state = initialState, action) => {
|
||||
case 'COMMENT_STREAM_FETCH_SUCCESS': return replaceComments(action, state);
|
||||
case actions.SHOW_BANUSER_DIALOG: return setBanUser(state, true, action);
|
||||
case actions.HIDE_BANUSER_DIALOG: return setBanUser(state, false, action);
|
||||
case actions.USER_BAN_SUCESS: return setBanUser(state, false, action);
|
||||
case actions.USER_BAN_SUCCESS: return setBanUser(state, false, action);
|
||||
default: return state;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -11,22 +11,20 @@ import coralApi from '../../../coral-framework/helpers/response';
|
||||
// Intercept redux actions and act over the ones we are interested
|
||||
export default store => next => action => {
|
||||
|
||||
next(action);
|
||||
|
||||
switch (action.type) {
|
||||
case 'COMMENTS_MODERATION_QUEUE_FETCH':
|
||||
fetchModerationQueueComments(store);
|
||||
break;
|
||||
return fetchModerationQueueComments(store);
|
||||
case 'COMMENT_UPDATE':
|
||||
updateComment(store, action.comment);
|
||||
break;
|
||||
return updateComment(store, action.comment);
|
||||
case 'COMMENT_CREATE':
|
||||
createComment(store, action.name, action.body);
|
||||
break;
|
||||
return createComment(store, action.name, action.body);
|
||||
case 'USER_BAN':
|
||||
userStatusUpdate(store, action.status, action.userId, action.commentId);
|
||||
break;
|
||||
return userStatusUpdate(store, action.status, action.userId, action.commentId);
|
||||
case 'ASSETS_FETCH':
|
||||
return fetchAssets(store, action);
|
||||
}
|
||||
|
||||
next(action);
|
||||
};
|
||||
|
||||
// Get comments to fill each of the three lists on the mod queue
|
||||
@@ -88,6 +86,20 @@ const createComment = (store, name, comment) => {
|
||||
// Ban a user
|
||||
const userStatusUpdate = (store, status, userId, commentId) => {
|
||||
return coralApi(`/users/${userId}/status`, {method: 'POST', body: {status: status, comment_id: commentId}})
|
||||
.then(res => store.dispatch({type: 'USER_BAN_SUCESS', res}))
|
||||
.then(res => store.dispatch({type: 'USER_BAN_SUCCESS', res}))
|
||||
.catch(error => store.dispatch({type: 'USER_BAN_FAILED', error}));
|
||||
};
|
||||
|
||||
// Fetch a page of assets
|
||||
// Get comments to fill each of the three lists on the mod queue
|
||||
const fetchAssets = (store, action) => {
|
||||
const {skip, limit, search} = action;
|
||||
return coralApi(`/assets?skip=${skip}&limit=${limit}&search=${search}`)
|
||||
.then(({result, count}) =>
|
||||
/* Post comments and users to redux store. Actions will be posted when they are needed. */
|
||||
store.dispatch({type: 'ASSETS_FETCH_SUCCESS',
|
||||
assets: result,
|
||||
count
|
||||
}))
|
||||
.catch(error => store.dispatch({type: 'ASSETS_FETCH_FAILED', error}));
|
||||
};
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
import 'react';
|
||||
import 'redux';
|
||||
import {expect} from 'chai';
|
||||
import fetchMock from 'fetch-mock';
|
||||
import adapter from '../../../../client/coral-admin/src/services/talk-adapter';
|
||||
import {Map} from 'immutable';
|
||||
|
||||
import configureStore from 'redux-mock-store';
|
||||
|
||||
const mockStore = configureStore();
|
||||
|
||||
describe('talk-adapter.js', () => {
|
||||
let store;
|
||||
|
||||
beforeEach(() => {
|
||||
store = mockStore(new Map({}));
|
||||
fetchMock.restore();
|
||||
});
|
||||
|
||||
describe('ASSETS_FETCH', () => {
|
||||
|
||||
const assets = [
|
||||
{
|
||||
url: 'http://test.com',
|
||||
id: '123',
|
||||
status: 'closed'
|
||||
},
|
||||
{
|
||||
url: 'http://test.org',
|
||||
id: '456',
|
||||
status: 'open'
|
||||
}
|
||||
];
|
||||
|
||||
it('should fetch a list of assets', () => {
|
||||
|
||||
const action = {
|
||||
type: 'ASSETS_FETCH',
|
||||
skip: 2,
|
||||
limit: 20,
|
||||
search: ''
|
||||
};
|
||||
|
||||
fetchMock.get('*', JSON.stringify({
|
||||
result: assets,
|
||||
count: 2
|
||||
}));
|
||||
|
||||
return adapter(store)(()=>{})(action)
|
||||
.then(() => {
|
||||
expect(store.getActions()[0]).to.have.property('type', 'ASSETS_FETCH_SUCCESS');
|
||||
expect(store.getActions()[0]).to.have.property('count', 2);
|
||||
expect(store.getActions()[0]).to.have.property('assets').
|
||||
and.to.deep.equal(assets);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an error appropriatly', () => {
|
||||
|
||||
const action = {
|
||||
type: 'ASSETS_FETCH',
|
||||
skip: 2,
|
||||
limit: 20,
|
||||
search: ''
|
||||
};
|
||||
|
||||
fetchMock.get('*', 404);
|
||||
|
||||
return adapter(store)(()=>{})(action)
|
||||
.then(() => {
|
||||
expect(store.getActions()[0]).to.have.property('type', 'ASSETS_FETCH_FAILED');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user