redux-duplicator
TypeScript icon, indicating that this package has built-in type declarations

0.0.12 • Public • Published

Concept

Genrate reducers and actionCreators from their templates.

See https://redux.js.org/recipes/structuring-reducers/reusing-reducer-logic .

This has not been launched. Develoment is undertaking now.

Usage

import duplicateRedux from '../src/redux-duplicator'
import { createAction, handleActions } from 'redux-actions' // Optional

const initialState = {
  id: ''
}

const _actionTypes = {
  SET_ID: `SET_ID`
}

const setId = createAction(_actionTypes.SET_ID, (payload: string) => payload)

const _actionCreators = {
  setId
}

const id = handleActions(
  {
    [_actionTypes.SET_ID]: (state, { payload }: ReturnType<typeof _actionCreators['setId']>) =>
      payload!
  },
  initialState.id
)

const nameSpace = 'TEST/'

const { reducer, actionTypes, actionCreators } = duplicateRedux(nameSpace, {
  reducer: id,
  actionTypes: _actionTypes,
  actionCreators: _actionCreators
})

describe('actionTypes Test', () => {
  it('rewrite action types', () => {
    expect(actionTypes.SET_ID === 'TEST/SET_ID').toBeTruthy()
  })
})

describe('Reducer Test', () => {
  it('rewrite reducer match', () => {
    const result = reducer('', { type: 'TEST/SET_ID', payload: 'fuga' })
    expect(result).toEqual('fuga')
    const miss = reducer('initital', { type: 'SET_ID', payload: 'fuga' })
    expect(miss).toEqual('initital')
  })
})

describe('actionCreators Test', () => {
  it('rewrite actionCreators type', () => {
    const type = actionCreators.setId('test').type
    expect(type).toEqual('TEST/SET_ID')
  })
})

/redux-duplicator/

    Package Sidebar

    Install

    npm i redux-duplicator

    Weekly Downloads

    66

    Version

    0.0.12

    License

    MIT

    Unpacked Size

    30.8 kB

    Total Files

    10

    Last publish

    Collaborators

    • tkow