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

View File

@ -1,18 +1,26 @@
import { Map, List, fromJS } from 'immutable'; import { Map, List, fromJS } from 'immutable';
import { 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'; } 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) => { const entries = (state = Map({ entities: Map(), pages: Map() }), action) => {
switch (action.type) { switch (action.type) {
case ENTRY_REQUEST: 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: case ENTRY_SUCCESS:
return state.setIn( return state.setIn(
['entities', `${action.payload.collection}.${action.payload.entry.slug}`], ['entities', `${ action.payload.collection }.${ action.payload.entry.slug }`],
fromJS(action.payload.entry) fromJS(action.payload.entry)
); );
@ -24,15 +32,15 @@ const entries = (state = Map({ entities: Map(), pages: Map() }), action) => {
loadedEntries = action.payload.entries; loadedEntries = action.payload.entries;
page = action.payload.page; page = action.payload.page;
return state.withMutations((map) => { return state.withMutations((map) => {
loadedEntries.forEach((entry) => ( loadedEntries.forEach(entry => (
map.setIn(['entities', `${collection}.${entry.slug}`], fromJS(entry).set('isFetching', false)) 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({ map.setIn(['pages', collection], Map({
page: page, page,
ids: page === 0 ? ids : map.getIn(['pages', collection, 'ids'], List()).concat(ids) 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', 'isFetching'], true);
map.setIn(['search', 'term'], action.payload.searchTerm); map.setIn(['search', 'term'], action.payload.searchTerm);
}); });
} else {
return state;
} }
return state;
case SEARCH_ENTRIES_SUCCESS: case SEARCH_ENTRIES_SUCCESS:
loadedEntries = action.payload.entries; loadedEntries = action.payload.entries;
page = action.payload.page; page = action.payload.page;
searchTerm = action.payload.searchTerm; searchTerm = action.payload.searchTerm;
return state.withMutations((map) => { return state.withMutations((map) => {
loadedEntries.forEach((entry) => ( loadedEntries.forEach(entry => (
map.setIn(['entities', `${entry.collection}.${entry.slug}`], fromJS(entry).set('isFetching', false)) map.setIn(['entities', `${ entry.collection }.${ entry.slug }`], fromJS(entry).set('isFetching', false))
)); ));
const ids = List(loadedEntries.map(entry => ({ collection: entry.collection, slug: entry.slug }))); const ids = List(loadedEntries.map(entry => ({ collection: entry.collection, slug: entry.slug })));
map.set('search', Map({ map.set('search', Map({
page: page, page,
term: searchTerm, 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) => ( export const selectEntry = (state, collection, slug) => (
state.getIn(['entities', `${collection}.${slug}`]) state.getIn(['entities', `${ collection }.${ slug }`])
); );
export const selectEntries = (state, collection) => { export const selectEntries = (state, collection) => {
const slugs = state.getIn(['pages', collection, 'ids']); 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) => { export const selectSearchedEntries = (state) => {