Search: refactor search reducer, add tests
This commit is contained in:
parent
5227f4688a
commit
6b54e88b62
2 changed files with 97 additions and 38 deletions
|
@ -1,4 +1,8 @@
|
||||||
import reducer from '../search';
|
import reducer from '../search';
|
||||||
|
import {
|
||||||
|
SEARCH_CHANGE,
|
||||||
|
SEARCH_CLEAR,
|
||||||
|
} from 'soapbox/actions/search';
|
||||||
import { Map as ImmutableMap } from 'immutable';
|
import { Map as ImmutableMap } from 'immutable';
|
||||||
|
|
||||||
describe('search reducer', () => {
|
describe('search reducer', () => {
|
||||||
|
@ -12,4 +16,38 @@ describe('search reducer', () => {
|
||||||
filter: 'accounts',
|
filter: 'accounts',
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('SEARCH_CHANGE', () => {
|
||||||
|
it('sets the value', () => {
|
||||||
|
const state = ImmutableMap({ value: 'hell' });
|
||||||
|
const action = { type: SEARCH_CHANGE, value: 'hello' };
|
||||||
|
expect(reducer(state, action).get('value')).toEqual('hello');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('SEARCH_CLEAR', () => {
|
||||||
|
it('resets the state', () => {
|
||||||
|
const state = ImmutableMap({
|
||||||
|
value: 'hello world',
|
||||||
|
submitted: true,
|
||||||
|
submittedValue: 'hello world',
|
||||||
|
hidden: false,
|
||||||
|
results: ImmutableMap(),
|
||||||
|
filter: 'statuses',
|
||||||
|
});
|
||||||
|
|
||||||
|
const action = { type: SEARCH_CLEAR };
|
||||||
|
|
||||||
|
const expected = ImmutableMap({
|
||||||
|
value: '',
|
||||||
|
submitted: false,
|
||||||
|
submittedValue: '',
|
||||||
|
hidden: false,
|
||||||
|
results: ImmutableMap(),
|
||||||
|
filter: 'accounts',
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(reducer(state, action)).toEqual(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -13,7 +13,7 @@ import {
|
||||||
COMPOSE_REPLY,
|
COMPOSE_REPLY,
|
||||||
COMPOSE_DIRECT,
|
COMPOSE_DIRECT,
|
||||||
} from '../actions/compose';
|
} from '../actions/compose';
|
||||||
import { Map as ImmutableMap, List as ImmutableList, fromJS } from 'immutable';
|
import { Map as ImmutableMap, OrderedSet as ImmutableOrderedSet, fromJS } from 'immutable';
|
||||||
|
|
||||||
const initialState = ImmutableMap({
|
const initialState = ImmutableMap({
|
||||||
value: '',
|
value: '',
|
||||||
|
@ -24,20 +24,65 @@ const initialState = ImmutableMap({
|
||||||
filter: 'accounts',
|
filter: 'accounts',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const toIds = items => {
|
||||||
|
return ImmutableOrderedSet(items.map(item => item.id));
|
||||||
|
};
|
||||||
|
|
||||||
|
const getResultsFilter = results => {
|
||||||
|
if (results.accounts.length > 0) {
|
||||||
|
return 'accounts';
|
||||||
|
} else if (results.statuses.length > 0) {
|
||||||
|
return 'statuses';
|
||||||
|
} else if (results.hashtags.length > 0) {
|
||||||
|
return 'hashtags';
|
||||||
|
} else {
|
||||||
|
return 'accounts';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const importResults = (state, results) => {
|
||||||
|
const filter = getResultsFilter(results);
|
||||||
|
|
||||||
|
return state.withMutations(state => {
|
||||||
|
state.set('results', ImmutableMap({
|
||||||
|
accounts: toIds(results.accounts),
|
||||||
|
statuses: toIds(results.accounts),
|
||||||
|
hashtags: fromJS(results.hashtags), // it's a list of maps
|
||||||
|
accountsHasMore: results.accounts.length >= 20,
|
||||||
|
statusesHasMore: results.statuses.length >= 20,
|
||||||
|
hashtagsHasMore: results.hashtags.length >= 20,
|
||||||
|
accountsLoaded: true,
|
||||||
|
statusesLoaded: true,
|
||||||
|
hashtagsLoaded: true,
|
||||||
|
}));
|
||||||
|
|
||||||
|
state.set('submitted', true);
|
||||||
|
state.set('filter', filter);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const paginateResults = (state, searchType, results) => {
|
||||||
|
return state.withMutations(state => {
|
||||||
|
state.setIn(['results', `${searchType}HasMore`], results[searchType].length >= 20);
|
||||||
|
state.setIn(['results', `${searchType}Loaded`], true);
|
||||||
|
state.updateIn(['results', searchType], items => items.concat(results[searchType].map(item => item.id)));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmitted = (state, value) => {
|
||||||
|
return state.withMutations(state => {
|
||||||
|
state.set('results', ImmutableMap());
|
||||||
|
state.set('submitted', true);
|
||||||
|
state.set('submittedValue', value);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
export default function search(state = initialState, action) {
|
export default function search(state = initialState, action) {
|
||||||
switch(action.type) {
|
switch(action.type) {
|
||||||
case SEARCH_CHANGE:
|
case SEARCH_CHANGE:
|
||||||
return state.withMutations(map => {
|
return state.set('value', action.value);
|
||||||
map.set('value', action.value);
|
|
||||||
});
|
|
||||||
case SEARCH_CLEAR:
|
case SEARCH_CLEAR:
|
||||||
return state.withMutations(map => {
|
return initialState;
|
||||||
map.set('value', '');
|
|
||||||
map.set('results', ImmutableMap());
|
|
||||||
map.set('submitted', false);
|
|
||||||
map.set('hidden', false);
|
|
||||||
map.set('filter', 'accounts');
|
|
||||||
});
|
|
||||||
case SEARCH_SHOW:
|
case SEARCH_SHOW:
|
||||||
return state.set('hidden', false);
|
return state.set('hidden', false);
|
||||||
case COMPOSE_REPLY:
|
case COMPOSE_REPLY:
|
||||||
|
@ -45,39 +90,15 @@ export default function search(state = initialState, action) {
|
||||||
case COMPOSE_DIRECT:
|
case COMPOSE_DIRECT:
|
||||||
return state.set('hidden', true);
|
return state.set('hidden', true);
|
||||||
case SEARCH_FETCH_REQUEST:
|
case SEARCH_FETCH_REQUEST:
|
||||||
return state.withMutations(map => {
|
return handleSubmitted(state, action.value);
|
||||||
map.set('results', ImmutableMap());
|
|
||||||
map.set('submitted', true);
|
|
||||||
map.set('submittedValue', action.value);
|
|
||||||
});
|
|
||||||
case SEARCH_FETCH_SUCCESS:
|
case SEARCH_FETCH_SUCCESS:
|
||||||
return state.set('results', ImmutableMap({
|
return importResults(state, action.results);
|
||||||
accounts: ImmutableList(action.results.accounts.map(item => item.id)),
|
|
||||||
statuses: ImmutableList(action.results.statuses.map(item => item.id)),
|
|
||||||
hashtags: fromJS(action.results.hashtags),
|
|
||||||
accountsHasMore: action.results.accounts.length >= 20,
|
|
||||||
statusesHasMore: action.results.statuses.length >= 20,
|
|
||||||
hashtagsHasMore: action.results.hashtags.length >= 20,
|
|
||||||
accountsLoaded: true,
|
|
||||||
statusesLoaded: true,
|
|
||||||
hashtagsLoaded: true,
|
|
||||||
})).set('submitted', true).set('filter', action.results.accounts.length > 0
|
|
||||||
? 'accounts'
|
|
||||||
: action.results.statuses.length > 0
|
|
||||||
? 'statuses'
|
|
||||||
: action.results.hashtags.length > 0
|
|
||||||
? 'hashtags'
|
|
||||||
: 'accounts');
|
|
||||||
case SEARCH_FILTER_SET:
|
case SEARCH_FILTER_SET:
|
||||||
return state.set('filter', action.value);
|
return state.set('filter', action.value);
|
||||||
case SEARCH_EXPAND_REQUEST:
|
case SEARCH_EXPAND_REQUEST:
|
||||||
return state.setIn(['results', `${action.searchType}Loaded`], false);
|
return state.setIn(['results', `${action.searchType}Loaded`], false);
|
||||||
case SEARCH_EXPAND_SUCCESS:
|
case SEARCH_EXPAND_SUCCESS:
|
||||||
return state.withMutations((state) => {
|
return paginateResults(state, action.searchType, action.results);
|
||||||
state.setIn(['results', `${action.searchType}HasMore`], action.results[action.searchType].length >= 20);
|
|
||||||
state.setIn(['results', `${action.searchType}Loaded`], true);
|
|
||||||
state.updateIn(['results', action.searchType], list => list.concat(action.results[action.searchType].map(item => item.id)));
|
|
||||||
});
|
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue