Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
test conditional inside reducer switch in react

Here i have a reducer which i'm testing at the moment, in my test coverage it shows Uncovered Line : this part 'return action.payload;' which is inside 'changeNode' function, any advice on how to test if else ? (i have many cases inside my switch and few of them have if else, if i solve this one i could solves those others easily)

export function graphArticle(
  state = initialGraph,
  action: graphArticle
) {
  switch (action.type) {
   case ActionType.EDIT_NODE:
      const changeN = (n: any) => {
        if (n.id == action.payload.id) {
          return action.payload;
        } else {
          return n;
        }
      };
      return {
        ...state,
        graph: {
          ...state.graph,
          cameras: updateGraphC(state.graph, action.payload),
          model: {
            ...state.graph.model,
            nodes: state.graph.model?.nodes.map(changeN),
          },
          currentNode: changeN(state.currentNode),
        },
      };
      }
      
      
      
      

test:

 it("EDIT_NODE ", () => {
        expect(
          reducers.graphArticle(undefined, {
            type: ActionType.EDIT_NODE,
            payload: {
              analyser_id: "yureg",
              id: 6,
            },
          })
        ).toEqual({
          floorplan: "",
          graph: {
            cameras: [],
            currentNode: "",
            model: {
              nodes: undefined,
            },
          },
        });
      });

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Well, your test does not actually test the "edit" functionality of the reducer, as you are testing against the initial state, which has no nodes.

You need to provide an initial state, currently the undefined you pass to reducers.graphArticle, with a couple of nodes, and then pass an action as the other parameter that will edit one of them, so that the test will go through the if and the else.

Something like

it("EDIT_NODE ", () => {
  const stateToEdit: InitialGraph = {
    floorplan: "",
    currentNode: "",
    graph: {
      cameras: [],
      model: {
        nodes: [{
          id: 1,
          analyser_id: "first"
        }, {
          id: 6,
          analyser_id: 'original'
        }, ],
      },
    },
  };
  const resultingStateAfterEdit: InitialGraph = {
    floorplan: "",
    currentNode: "",
    graph: {
      cameras: [],
      currentNode: "",
      model: {
        nodes: [{
          id: 1,
          analyser_id: "first"
        }, {
          id: 6,
          analyser_id: 'yureg'
        }, ],
      },
    },
  };

  expect(
    reducers.graphArticle(stateToEdit, {
      type: ActionType.EDIT_NODE,
      payload: {
        analyser_id: "yureg",
        id: 6,
      },
    })
  ).toEqual(resultingStateAfterEdit);
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!