Skip to content

Commit

Permalink
Bugfix: Prevent double-prefixing when using combineActions (#334)
Browse files Browse the repository at this point in the history
* Bugfix: Prevent double-prefixing when using `combineActions`

* remove focus from test
  • Loading branch information
SuaYoo authored and timche committed Feb 18, 2019
1 parent be8f1ca commit 3bba717
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/utils/flattenWhenNode.js
Expand Up @@ -19,7 +19,11 @@ export default predicate =>
}

function connectPrefix(type) {
if (partialFlatActionType || !prefix) {
if (
partialFlatActionType ||
!prefix ||
(prefix && new RegExp(`^${prefix}${namespace}`).test(type))
) {
return type;
}

Expand Down
20 changes: 20 additions & 0 deletions test/combineActions.test.js
Expand Up @@ -32,6 +32,26 @@ test('returns a stringifiable object', () => {
);
});

test('handles prefixed action types', () => {
const options = { prefix: 'my-custom-prefix' };
const { action1, action2 } = createActions('ACTION_1', 'ACTION_2', options);

expect(
combineActions(
'my-custom-prefix/ACTION_1',
'my-custom-prefix/ACTION_2'
).toString()
).toBe('my-custom-prefix/ACTION_1||my-custom-prefix/ACTION_2');
expect(combineActions(action1, action2).toString()).toBe(
'my-custom-prefix/ACTION_1||my-custom-prefix/ACTION_2'
);
expect(
combineActions(action1, action2, 'my-custom-prefix/ACTION_3').toString()
).toBe(
'my-custom-prefix/ACTION_1||my-custom-prefix/ACTION_2||my-custom-prefix/ACTION_3'
);
});

test('should throw error if actions is empty', () => {
expect(() => combineActions()).toThrow(
'Expected action types to be strings, symbols, or action creators'
Expand Down
33 changes: 33 additions & 0 deletions test/handleActions.test.js
Expand Up @@ -556,3 +556,36 @@ test('works with combineActions nested', () => {
loading: true
});
});

test('works with a prefix and namespace', () => {
const { increment, decrement } = createActions(
{
INCREMENT: [amount => ({ amount }), amount => ({ key: 'value', amount })],
DECREMENT: amount => ({ amount: -amount })
},
{ prefix: 'my-custom-prefix', namespace: '--' }
);

// NOTE: We should be using combineReducers in production, but this is just a test.
const reducer = handleActions(
{
[combineActions(increment, decrement)]: (
{ counter },
{ payload: { amount } }
) => ({
counter: counter + amount
})
},
{ counter: 0 },
{ prefix: 'my-custom-prefix', namespace: '--' }
);

expect(String(increment)).toBe('my-custom-prefix--INCREMENT');

expect(reducer({ counter: 3 }, increment(2))).toEqual({
counter: 5
});
expect(reducer({ counter: 10 }, decrement(3))).toEqual({
counter: 7
});
});

0 comments on commit 3bba717

Please sign in to comment.