Skip to content

Commit 0d1b1f7

Browse files
Kerumentimche
authored andcommitted
Make combineActions works with nested handleActions (#294)
* make combineActions works with nested handleActions * Rename variables in test
1 parent 51f00d9 commit 0d1b1f7

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/utils/flattenWhenNode.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import isMap from 'lodash/isMap';
2-
import { DEFAULT_NAMESPACE } from '../constants';
2+
import { DEFAULT_NAMESPACE, ACTION_TYPE_DELIMITER } from '../constants';
33
import ownKeys from './ownKeys';
44

55
function get(key, x) {
@@ -14,9 +14,12 @@ export default predicate =>
1414
partialFlatActionType = ''
1515
) {
1616
function connectNamespace(type) {
17-
return partialFlatActionType
18-
? `${partialFlatActionType}${namespace}${type}`
19-
: type;
17+
if (!partialFlatActionType) return type;
18+
const types = type.toString().split(ACTION_TYPE_DELIMITER);
19+
const partials = partialFlatActionType.split(ACTION_TYPE_DELIMITER);
20+
return []
21+
.concat(...partials.map(p => types.map(t => `${p}${namespace}${t}`)))
22+
.join(ACTION_TYPE_DELIMITER);
2023
}
2124

2225
function connectPrefix(type) {

test/handleActions.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,30 @@ test('works with nested reducerMap and identity handlers', () => {
529529
message: 'hello'
530530
});
531531
});
532+
533+
test('works with combineActions nested', () => {
534+
const { apiCall1, apiCall2 } = createActions('API_CALL_1', 'API_CALL_2');
535+
const {
536+
apiCall1: { loading: apiCallLoading1 },
537+
apiCall2: { loading: apiCallLoading2 }
538+
} = createActions({
539+
API_CALL_1: { LOADING: undefined },
540+
API_CALL_2: { LOADING: undefined }
541+
});
542+
543+
const reducer = handleActions(
544+
{
545+
[combineActions(apiCall1, apiCall2)]: {
546+
LOADING: (state, { payload: loading }) => ({ loading })
547+
}
548+
},
549+
{ loading: false }
550+
);
551+
552+
expect(reducer({ loading: false }, apiCallLoading1(true))).toEqual({
553+
loading: true
554+
});
555+
expect(reducer({ loading: false }, apiCallLoading2(true))).toEqual({
556+
loading: true
557+
});
558+
});

0 commit comments

Comments
 (0)