Fixed ESLint errors and tests for entries reducer

This commit is contained in:
Andrey Okonetchnikov 2016-10-12 16:04:58 +02:00
parent 2f37b3df12
commit 8d51f9be3e
2 changed files with 35 additions and 27 deletions

View File

@ -6,27 +6,27 @@ import reducer from '../entries';
describe('entries', () => {
it('should mark entries as fetching', () => {
const state = OrderedMap({
'posts': Map({ name: 'posts' })
posts: Map({ name: 'posts' }),
});
expect(
reducer(state, entriesLoading(Map({ name: 'posts' })))
).toEqual(
OrderedMap(fromJS({
'posts': { name: 'posts' },
'pages': {
'posts': { isFetching: true }
}
posts: { name: 'posts' },
pages: {
posts: { isFetching: true },
},
}))
);
});
it('should handle loaded entries', () => {
const state = OrderedMap({
'posts': Map({ name: 'posts' })
posts: Map({ name: 'posts' }),
});
const entries = [{ slug: 'a', path: '' }, { slug: 'b', title: 'B' }];
expect(
reducer(state, entriesLoaded(Map({ name: 'posts' }), entries))
reducer(state, entriesLoaded(Map({ name: 'posts' }), entries, 0))
).toEqual(
OrderedMap(fromJS(
{
@ -37,9 +37,10 @@ describe('entries', () => {
},
pages: {
posts: {
ids: ['a', 'b']
}
}
page: 0,
ids: ['a', 'b'],
},
},
}
))
);

View File

@ -1,18 +1,26 @@
import { Map, List, fromJS } from 'immutable';
import {
ENTRY_REQUEST, ENTRY_SUCCESS, ENTRIES_REQUEST, ENTRIES_SUCCESS, SEARCH_ENTRIES_REQUEST, SEARCH_ENTRIES_SUCCESS
ENTRY_REQUEST,
ENTRY_SUCCESS,
ENTRIES_REQUEST,
ENTRIES_SUCCESS,
SEARCH_ENTRIES_REQUEST,
SEARCH_ENTRIES_SUCCESS,
} from '../actions/entries';
let collection, loadedEntries, page, searchTerm;
let collection;
let loadedEntries;
let page;
let searchTerm;
const entries = (state = Map({ entities: Map(), pages: Map() }), action) => {
switch (action.type) {
case ENTRY_REQUEST:
return state.setIn(['entities', `${action.payload.collection}.${action.payload.slug}`, 'isFetching'], true);
return state.setIn(['entities', `${ action.payload.collection }.${ action.payload.slug }`, 'isFetching'], true);
case ENTRY_SUCCESS:
return state.setIn(
['entities', `${action.payload.collection}.${action.payload.entry.slug}`],
['entities', `${ action.payload.collection }.${ action.payload.entry.slug }`],
fromJS(action.payload.entry)
);
@ -24,15 +32,15 @@ const entries = (state = Map({ entities: Map(), pages: Map() }), action) => {
loadedEntries = action.payload.entries;
page = action.payload.page;
return state.withMutations((map) => {
loadedEntries.forEach((entry) => (
map.setIn(['entities', `${collection}.${entry.slug}`], fromJS(entry).set('isFetching', false))
loadedEntries.forEach(entry => (
map.setIn(['entities', `${ collection }.${ entry.slug }`], fromJS(entry).set('isFetching', false))
));
const ids = List(loadedEntries.map((entry) => entry.slug));
const ids = List(loadedEntries.map(entry => entry.slug));
map.setIn(['pages', collection], Map({
page: page,
ids: page === 0 ? ids : map.getIn(['pages', collection, 'ids'], List()).concat(ids)
page,
ids: page === 0 ? ids : map.getIn(['pages', collection, 'ids'], List()).concat(ids),
}));
});
@ -42,23 +50,22 @@ const entries = (state = Map({ entities: Map(), pages: Map() }), action) => {
map.setIn(['search', 'isFetching'], true);
map.setIn(['search', 'term'], action.payload.searchTerm);
});
} else {
return state;
}
return state;
case SEARCH_ENTRIES_SUCCESS:
loadedEntries = action.payload.entries;
page = action.payload.page;
searchTerm = action.payload.searchTerm;
return state.withMutations((map) => {
loadedEntries.forEach((entry) => (
map.setIn(['entities', `${entry.collection}.${entry.slug}`], fromJS(entry).set('isFetching', false))
loadedEntries.forEach(entry => (
map.setIn(['entities', `${ entry.collection }.${ entry.slug }`], fromJS(entry).set('isFetching', false))
));
const ids = List(loadedEntries.map(entry => ({ collection: entry.collection, slug: entry.slug })));
map.set('search', Map({
page: page,
page,
term: searchTerm,
ids: page === 0 ? ids : map.getIn(['search', 'ids'], List()).concat(ids)
ids: page === 0 ? ids : map.getIn(['search', 'ids'], List()).concat(ids),
}));
});
@ -68,12 +75,12 @@ const entries = (state = Map({ entities: Map(), pages: Map() }), action) => {
};
export const selectEntry = (state, collection, slug) => (
state.getIn(['entities', `${collection}.${slug}`])
state.getIn(['entities', `${ collection }.${ slug }`])
);
export const selectEntries = (state, collection) => {
const slugs = state.getIn(['pages', collection, 'ids']);
return slugs && slugs.map((slug) => selectEntry(state, collection, slug));
return slugs && slugs.map(slug => selectEntry(state, collection, slug));
};
export const selectSearchedEntries = (state) => {