Search code examples
javascriptreactjsimmer.js

How do you spy on an immer reducer?


Say this is my immer slice - how do i write a spy to on the updateValue reducer with Jest?? I would normally import * as helperFunctions from "xyz", and spy on that, like const exposeFormErrorSpy = jest.spyOn(helperFunctions, 'exposeEventToPartner'); to spy on the exposeEventToPartner function of the import called helperFunctions. But how do i do that with immer / how do I test an immer slice?

import { createSlice } from '@reduxjs/toolkit';
import { initialState } from '../../../Data';
/**
 * This is going to be the largest piece of state, it is here where we hold all the required components for the app.
 */
export const components = createSlice({
  name: 'COMPONENTS',
  initialState: initialState.components,
  reducers: {
    updateValue(state, action) {
      const { payload } = action;
      const { componentId, value } = payload;
      state[`component_${componentId}`].component.input.value = value;
    },
  },
});

const { actions, reducer } = components;

export const { updateValue } = actions;

export default reducer;

Solution

  • Solution is to import * as componentsReducer from '../../../src/Lib/redux/slices/components'; and then const selectOptionSpy = jest.spyOn(componentsReducer, 'updateValue');