Moved applyDefaults function and related code to actions (where it belong).

- Moved applyDefaults + related code
- Added tests for /actions/config.js
- Fixed some ESLint errors
This commit is contained in:
Andrey Okonetchnikov
2016-11-11 12:17:01 +01:00
parent 858da43140
commit 5b5f1edb22
4 changed files with 127 additions and 47 deletions

View File

@ -13,12 +13,23 @@ describe('collections', () => {
it('should load the collections from the config', () => {
expect(
collections(undefined, configLoaded({ collections: [
{ name: 'posts', folder: '_posts', fields: [{ name: 'title', widget: 'string' }] },
] }))
collections(undefined, configLoaded({
collections: [
{
name: 'posts',
folder: '_posts',
fields: [{ name: 'title', widget: 'string' }],
},
],
}))
).toEqual(
OrderedMap({
posts: fromJS({ name: 'posts', folder: '_posts', fields: [{ name: 'title', widget: 'string' }] }),
posts: fromJS({
name: 'posts',
folder: '_posts',
fields: [{ name: 'title', widget: 'string' }],
type: 'folder_based_collection',
}),
})
);
});

View File

@ -1,28 +1,12 @@
import Immutable from 'immutable';
import _ from 'lodash';
import * as publishModes from '../constants/publishModes';
import { CONFIG_REQUEST, CONFIG_SUCCESS, CONFIG_FAILURE } from '../actions/config';
const defaults = {
publish_mode: publishModes.SIMPLE
};
const applyDefaults = (config) => {
// Make sure there is a public folder
_.set(defaults,
'public_folder',
config.media_folder.charAt(0) === '/' ? config.media_folder : '/' + config.media_folder);
return _.defaultsDeep(config, defaults);
};
const config = (state = null, action) => {
switch (action.type) {
case CONFIG_REQUEST:
return Immutable.Map({ isFetching: true });
case CONFIG_SUCCESS:
const config = applyDefaults(action.payload);
return Immutable.fromJS(config);
return Immutable.fromJS(action.payload);
case CONFIG_FAILURE:
return Immutable.Map({ error: action.payload.toString() });
default: