Giter Club home page Giter Club logo

createreactreducer's Introduction


Create react reducer

Install it globally:

> yarn global add createreactreducer

to get a createreactreducerexecutable available everywhere.


Motivation

The main reason is my lazyness. Bored to manually setup the whole thing around useReducer... but, let's make one step back, with some assumptions of mine.

  • action names should be constants, definitely not strings developers must remember (or lookup in the reducer)... thus why not Symbol? (actually the only pro in using Symbols would be to avoid clashes).
  • each action should have its own function;
    this also allows implicitely to avoid code noone should write or see anywhere, like:
    function reducer(state, action) {
        switch (action.type) {
            case 'incremented_age': { 
                return { //
  • officially the reducer function you write must return the whole state; wouldn't be better to just return the updates?
    ...
    return {
        ...oldState, // ❌, never forgot about it?
        opNumber: opNumber+1,
        res: res + (parseInt(payload, 10) || 0)
    }
    ...
  • all of the previous points reveal their value when it comes to testing, cause we want to test our reducers right!!!
    The idea is be able to easily add testcases purely based on the only 3 core informations we need:
  • oldState: before the change
  • {type, payload}: the action
  • newState: after the change
    and it would be great to add a new test case for an action simply providing that 3-uple.

Use it

createreactreducer script creates the barebone structure for that, and it is intended to be ran as first thing when one realizes a reducer is needed:

  1. move to the folder where you want to create a new reducer

  2. run createreactreducer add sub reset ....
    here add sub reset are just example labels for 3 actions.
    Within the execution folder it will create the following structure:

    ╠═ reducer/
    ║  ╠═ test/
    ║  ║  ╠═ actions/
    ║  ║  ║  ╟╴ index.js
    ║  ║  ║  ╟╴ sub.js     // as many 
    ║  ║  ║  ╟╴ reset.js   // as specified
    ║  ║  ║  ╙╴ add.js     // parameters
    ║  ║  ╙╴ index.spec.js
    ║  ╟╴ actions.js
    ║  ╙╴ index.js
    

    It will create action-case-test files within reducer/test/actions.
    The "only" thing left for us is the implementation of the actions in reducer/index.js and synch the test action accordingly, then just test the test/index.spec.js.
    For further actions You can rely on Your AI-revamped c & v talent.
    Most likely You will never have to edit reducer/test/index.spec.js:

    how the created reducer test looks like
    import TESTACTIONS from "./actions"
    import ACTIONS from "../actions"
    import rx from "../index.js"
    const { func: reducer } = rx;
    describe("test reducer", () => {
      const testReducerAction = label => {
        describe(`tests for action: ${label}`, () => {
          it.each(TESTACTIONS[label])(
            `${ACTIONS[label].description}: %s`,
            (_, state, action, expected) => 
              expect(
                reducer(state, action)
              ).toMatchObject(expected)
            );
        })
      }
      Object.keys(ACTIONS).forEach(testReducerAction);
    })

  3. Proceed implementing the actions functions in the reducer and updating/creating the testcases accordingly.

  4. Enjoy how fast You can spot problems and reach 100% coverage dinamically loading all the testcases exported through reducer/test/actions/index.js.

That's all.

createreactreducer's People

Contributors

fedeghe avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.