Skip to content

Commit 3bba717

Browse files
SuaYootimche
authored andcommitted
Bugfix: Prevent double-prefixing when using combineActions (#334)
* Bugfix: Prevent double-prefixing when using `combineActions` * remove focus from test
1 parent be8f1ca commit 3bba717

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/utils/flattenWhenNode.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ export default predicate =>
1919
}
2020

2121
function connectPrefix(type) {
22-
if (partialFlatActionType || !prefix) {
22+
if (
23+
partialFlatActionType ||
24+
!prefix ||
25+
(prefix && new RegExp(`^${prefix}${namespace}`).test(type))
26+
) {
2327
return type;
2428
}
2529

test/combineActions.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ test('returns a stringifiable object', () => {
3232
);
3333
});
3434

35+
test('handles prefixed action types', () => {
36+
const options = { prefix: 'my-custom-prefix' };
37+
const { action1, action2 } = createActions('ACTION_1', 'ACTION_2', options);
38+
39+
expect(
40+
combineActions(
41+
'my-custom-prefix/ACTION_1',
42+
'my-custom-prefix/ACTION_2'
43+
).toString()
44+
).toBe('my-custom-prefix/ACTION_1||my-custom-prefix/ACTION_2');
45+
expect(combineActions(action1, action2).toString()).toBe(
46+
'my-custom-prefix/ACTION_1||my-custom-prefix/ACTION_2'
47+
);
48+
expect(
49+
combineActions(action1, action2, 'my-custom-prefix/ACTION_3').toString()
50+
).toBe(
51+
'my-custom-prefix/ACTION_1||my-custom-prefix/ACTION_2||my-custom-prefix/ACTION_3'
52+
);
53+
});
54+
3555
test('should throw error if actions is empty', () => {
3656
expect(() => combineActions()).toThrow(
3757
'Expected action types to be strings, symbols, or action creators'

test/handleActions.test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,36 @@ test('works with combineActions nested', () => {
556556
loading: true
557557
});
558558
});
559+
560+
test('works with a prefix and namespace', () => {
561+
const { increment, decrement } = createActions(
562+
{
563+
INCREMENT: [amount => ({ amount }), amount => ({ key: 'value', amount })],
564+
DECREMENT: amount => ({ amount: -amount })
565+
},
566+
{ prefix: 'my-custom-prefix', namespace: '--' }
567+
);
568+
569+
// NOTE: We should be using combineReducers in production, but this is just a test.
570+
const reducer = handleActions(
571+
{
572+
[combineActions(increment, decrement)]: (
573+
{ counter },
574+
{ payload: { amount } }
575+
) => ({
576+
counter: counter + amount
577+
})
578+
},
579+
{ counter: 0 },
580+
{ prefix: 'my-custom-prefix', namespace: '--' }
581+
);
582+
583+
expect(String(increment)).toBe('my-custom-prefix--INCREMENT');
584+
585+
expect(reducer({ counter: 3 }, increment(2))).toEqual({
586+
counter: 5
587+
});
588+
expect(reducer({ counter: 10 }, decrement(3))).toEqual({
589+
counter: 7
590+
});
591+
});

0 commit comments

Comments
 (0)