mirror of
https://github.com/wassname/talk.git
synced 2026-09-10 12:43:11 +08:00
Adding asset reducer.
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import {Map, List, fromJS} from 'immutable';
|
||||
|
||||
const initialState = Map({
|
||||
byId: Map(),
|
||||
ids: List()
|
||||
});
|
||||
|
||||
export default (state = initialState, action) => {
|
||||
switch (action.type) {
|
||||
case 'ASSETS_FETCH_SUCCESS': return replaceAssets(action, state);
|
||||
default: return state;
|
||||
}
|
||||
};
|
||||
|
||||
const replaceAssets = (action, state) => {
|
||||
const assets = fromJS(action.assets.reduce((prev, curr) => { prev[curr.id] = curr; return prev; }, {}));
|
||||
return state.set('byId', assets)
|
||||
.set('count', action.count)
|
||||
.set('ids', List(assets.keys()));
|
||||
};
|
||||
@@ -0,0 +1,39 @@
|
||||
import {Map} from 'immutable';
|
||||
import {expect} from 'chai';
|
||||
import assetsReducer from '../../../../client/coral-admin/src/reducers/assets';
|
||||
|
||||
describe ('assetsReducer', () => {
|
||||
describe('ASSETS_FETCH_SUCCESS', () => {
|
||||
it('should replace the existing assets', () => {
|
||||
const action = {
|
||||
type: 'ASSETS_FETCH_SUCCESS',
|
||||
count: 200,
|
||||
assets: [
|
||||
{
|
||||
id: '123',
|
||||
url: 'http://test.com',
|
||||
closedAt: 'tomorrow'
|
||||
},
|
||||
{
|
||||
id: '456',
|
||||
url: 'http://test2.com',
|
||||
closedAt: 'thursday'
|
||||
},
|
||||
]
|
||||
};
|
||||
const store = new Map({});
|
||||
const result = assetsReducer(store, action);
|
||||
console.log(result.getIn(['byId', '123']).toJS());
|
||||
expect(result.getIn(['byId', '123']).toJS()).to.deep.equal({
|
||||
url: 'http://test.com',
|
||||
closedAt: 'tomorrow',
|
||||
id: '123'
|
||||
});
|
||||
expect(result.getIn(['ids']).toJS()).to.deep.equal([
|
||||
'123',
|
||||
'456'
|
||||
]);
|
||||
expect(result.getIn(['count'])).to.equal(200);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user