diff --git a/src/reduxHotModule.js b/src/reduxHotModule.js index 2dcedd9..4a7aa50 100644 --- a/src/reduxHotModule.js +++ b/src/reduxHotModule.js @@ -12,8 +12,8 @@ class ReduxHotModule { addAction(this.actionsRepo, name, { isParam: true, defaultValue }) } - addEventAction(name) { - addAction(this.actionsRepo, name, { isEvent: true }) + addEventAction(name, defaultValue = null) { + addAction(this.actionsRepo, name, { isEvent: true, defaultValue }) } addResetAction(name = 'reset') { @@ -34,6 +34,7 @@ class ReduxHotModule { for (let i = 0; i < length; i += 1) { const { name, meta } = items[i] + const { defaultValue } = meta const typeNameConst = toConst(name) const type = `${typePrefix}${typeNameConst}` const typeName = `${moduleConst}_${typeNameConst}` @@ -42,12 +43,11 @@ class ReduxHotModule { types[typeName] = type if (meta.isParam) { - const { defaultValue } = meta actions[actionName] = (value = defaultValue) => ({ type, payload: { [name]: value } }) defaultState[name] = defaultValue paramTypes[type] = meta } else if (meta.isEvent) { - actions[actionName] = () => ({ type }) + actions[actionName] = (value = defaultValue) => ({ type, payload: value }) } else { actions[actionName] = () => ({ type }) resetTypes[type] = meta diff --git a/tests/reduxHotModule/actions/eventActions.test.js b/tests/reduxHotModule/actions/eventActions.test.js index 7455b0d..23f6577 100644 --- a/tests/reduxHotModule/actions/eventActions.test.js +++ b/tests/reduxHotModule/actions/eventActions.test.js @@ -24,7 +24,7 @@ describe('test event actions', () => { expect(actionNames).toContain('loadEventAction') }) - test('should return redux action without payload', () => { + test('should return redux action with default value', () => { const ml = new ReduxHotModule('moduleName') ml.addEventAction('loadedEvent') @@ -39,11 +39,62 @@ describe('test event actions', () => { const reduxAction = loadedEventAction() const expected = { - type: '@@moduleName/LOADED_EVENT' + type: '@@moduleName/LOADED_EVENT', + payload: null } expect(typeof reduxAction).toBe('object') expect(reduxAction).toStrictEqual(expected) - expect(loadedEventAction({})).toStrictEqual(expected) + }) + + test('should return redux action with provided default value', () => { + const ml = new ReduxHotModule('moduleName') + + const defaultValue = {} + + ml.addEventAction('loadedEvent', defaultValue) + + const { actions } = ml.create() + + expect(Object.keys(actions)).toHaveLength(1) + + const { loadedEventAction } = actions + + expect(typeof loadedEventAction).toBe('function') + + const reduxAction = loadedEventAction() + const expected = { + type: '@@moduleName/LOADED_EVENT', + payload: defaultValue + } + + expect(typeof reduxAction).toBe('object') + expect(reduxAction).toStrictEqual(expected) + expect(reduxAction.payload).toBe(defaultValue) + }) + + test('should return redux action with provided payload', () => { + const ml = new ReduxHotModule('moduleName') + + ml.addEventAction('loadedEvent') + + const { actions } = ml.create() + + expect(Object.keys(actions)).toHaveLength(1) + + const { loadedEventAction } = actions + + expect(typeof loadedEventAction).toBe('function') + + const value = {} + const reduxAction = loadedEventAction(value) + const expected = { + type: '@@moduleName/LOADED_EVENT', + payload: value + } + + expect(typeof reduxAction).toBe('object') + expect(reduxAction).toStrictEqual(expected) + expect(reduxAction.payload).toBe(value) }) })