Skip to content

Bugfix: Prevent double-prefixing when using combineActions #334

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Feb 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/utils/flattenWhenNode.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
});
});