Skip to content

Commit

Permalink
addEventAction() with payload
Browse files Browse the repository at this point in the history
  • Loading branch information
gavrya committed Apr 17, 2020
1 parent 35e983e commit 1626054
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/reduxHotModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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}`
Expand All @@ -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
Expand Down
57 changes: 54 additions & 3 deletions tests/reduxHotModule/actions/eventActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
})
})

0 comments on commit 1626054

Please sign in to comment.