Establishing a Recyclable Testing Rendering Method in redux toolkit version 2

In the era of Redux Toolkit v2, a noticeable change occurred with the absence of the EmptyObject type and the unavailability of the PreloadedState type in the @reduxjs/toolkit package. This has led to a requirement of defining all reducers inside the preloaded state when working with Redux. Are there any updated solutions or alternatives to accommodate these changes in Redux Toolkit v2?

import type { AppState, AppStore } from "@/store";
import uiReducer from "@/store/slices/ui-slice";
import type { PreloadedState } from "@reduxjs/toolkit";
import { configureStore } from "@reduxjs/toolkit";
import type { RenderOptions } from "@testing-library/react";
import { render } from "@testing-library/react";
import React, { PropsWithChildren } from "react";
import { Provider } from "react-redux";

interface ExtendedRenderOptions extends Omit<RenderOptions, "queries"> {
  preloadedState?: PreloadedState<AppState>;
  store?: AppStore;
}

export function renderWithProviders(
  ui: React.ReactElement,
  {
    preloadedState = {},
    store = configureStore({
      reducer: {
        ui: uiReducer,
      },
      preloadedState,
    }),
    ...renderOptions
  }: ExtendedRenderOptions = {}
) {
  function Wrapper({ children }: PropsWithChildren<{}>): React.JSX.Element {
    return <Provider store={store}>{children}</Provider>;
  }

  return { store, ...render(ui, { wrapper: Wrapper, ...renderOptions }) };
}

Answer №1

InitialState can consist of any valid input for your main reducer function. In the scenario provided, it should look like this:

{
  settings: Parameters<typeof settingsReducer>[0]
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I am facing a problem with React Hooks useRef where I am unable to retrieve the updated state value

Trying to use useRef with React hooks, I encountered an issue where the state of the child component changes when calling the setAccountVal method, but upon alerting the value it remains as "Ege". Any ideas on how to resolve this? import React, { useS ...

Is it possible to nest HTML within a Route component?

<Router> <Routes> <Route path='/' element={<Navbar />} /> <Route path='/' element={<div className='recipes'> {query ? query.map((object, i) => ( <Recipe ...

unable to utilize makeStyles

As someone who is completely new to Reactjs and material UI, I am encountering a plethora of errors in the makeStyles() function. Any assistance with the code would be greatly appreciated. const useStyles = makeStyles((theme) => ({ sectionDesktop: { ...

Accessing the host in NextJS using _document.js or _app.js

I am currently working on a NextJs website that operates on various hostnames. Upon the application's initialization, I need to fetch data from an API based on the specific URL where NextJS is deployed. For instance, if my NextJS is running on websit ...

The default value for the MUI object in the Select component is not displayed initially, but is shown once

Is there a way to set a default value for the dropdown in this code snippet? Currently, the "chosenValue" is set by default, but I need to change it so that something else appears selected when the page loads. How can I achieve this? You can refer to thi ...

What is the best way to create a React component that renders a class component as a functional component?

My Objective: At the moment, I am in the process of developing an AuthUserRole HOC component to manage user roles like Manager and Employee. However, I encountered a tutorial that uses a functional component to return a class component as referenced here. ...

Issues with displaying images have been encountered with the Chessboardjs NPM package

Currently, I am attempting to utilize the https://www.npmjs.com/package/chessboardjs package in conjunction with meteor 1.13. Despite developing a simple react component to display the board, the images are not rendering as expected. Below is the code for ...

initiate an animated sequence upon the initialization of the Angular server

Is there a way to launch a Netflix animation after my server has started without success using setTimeout? I don't want to share the lengthy HTML and CSS code. You can view the code for the animation in question by visiting: https://codepen.io/claudi ...

Is it possible to access API services from a different backend server in Next.js without utilizing the default api route?

I have Nextjs set up to handle APIs with a default route "api" in my pages folder, but I want to use endpoints from a different backend server address (eg: api.domain.com) instead of the default "api" route. Is it possible to do that? ...

Personalizing the website icon for a nextJS app on Firebase

My nextJS app on firebase was created using the following commands in the terminal: % npx create-next-app@latest % cd myapp % firebase experiments:enable webframeworks % firebase init hosting % npm i react-firebase-hooks I'm currently attempting ...

Tips for sidestepping the need for casting the class type of an instance

Looking to develop a function that can accept an argument containing a service name and return an instance of that particular service, all while ensuring proper typing. Casting the instance seems necessary in order to achieve the desired result. To illust ...

Tips for displaying an associative object array as td elements within a tbody in Nuxt

I'm having trouble displaying the property of an associative object array in my code. I attempted to utilize a v-for loop and wanted to showcase the property information within the td elements of a tbody. I am aware that v-data-table components have a ...

Caution in Next.js: The `href` prop does not match between the server and client

I have been using localStorage and useEffect to check the authentication state, and conditionally rendering Navbar items based on the status. However, I encountered an error: Warning: Prop href did not match. Server: "/LoginForm" Client: " ...

Encountering an Uncaught TypeError due to failure to read the property 'bool' of an undefined value

Encountering a runtime error in my code that I can't seem to pinpoint. Everything was functioning correctly before this error surfaced after updating npm. Here's the error message: Uncaught TypeError: Cannot read property 'bool' of und ...

The error message states: "TypeError: a.map is not a function"

I recently deployed my React JS app on Heroku and encountered some issues with the routing. While everything worked smoothly on my local host, I faced errors after fixing the routing problem that I couldn't resolve. Despite extensive research, I haven ...

substitute placeholders when clicked in react applications

I'm still learning the ropes of React, especially when it comes to dealing with states. Currently, I have an accordion component set up with multiple items and a list of text that I need to iterate through. Here's a snippet of the code: Accord ...

Issues with the inheritance functionality in styled components are causing errors

The issue arises when I try to customize the styling of my PrimaryButton component, separate from the DefaultButton. Despite writing style properties for it, the changes do not take effect. Here is the folder structure: https://i.stack.imgur.com/0KjyH.pn ...

How can we avoid excessive re-rendering of a child component in React when making changes to the parent's state?

In my React application, I am facing a situation where a parent component controls a state variable and sends it to a child component. The child component utilizes this state in its useEffect hook and at times modifies the parent's state. As a result, ...

Issue with @reach/router where the URL path is updated but the component fails to render after clicking a Link

I recently switched to using @reach/router for a new project after receiving a recommendation on the react-router-dom GitHub page. Despite what should be a simple use case, I am encountering some issues. Initially, I attempted to integrate the @mui BottomN ...

When using TypeScript, my sorting function returns a value of 0 for all time values

I have been trying to sort this JSON data by date using the provided code, but it does not seem to work as expected. Below is a snippet of my JSON: { "StatusCode":0, "StatusMessage":"OK", "StatusDescription":[ { "id":"1", ...