reduxjs / redux-mock-store

A mock store for testing Redux async action creators and middleware.
MIT License
2.5k stars 148 forks source link

redux cannot read the property ids of undefined do you know why? #186

Closed kyawkhantwin closed 6 months ago

kyawkhantwin commented 6 months ago

This is injectEndpoints File

import { createEntityAdapter, createSelector } from "@reduxjs/toolkit";
import apiSlice from "../api/apiSlice";

const productsAdapter = createEntityAdapter({})

const initialState = productsAdapter.getInitialState();

export const extendedApiSlice = apiSlice.injectEndpoints({
  endpoints: (builder) => ({
    getProducts: builder.query({
      query: () => "/todos",
      transformResponse: (responseData) => {
        return productsAdapter.setAll(initialState, responseData);
      },
    })
  }),
});

export const { useGetProductQuery } = extendedApiSlice;

const selectProductsResult = extendedApiSlice.endpoints.getProducts.select();

const selectProductsData = createSelector(
  selectProductsResult,
  (productsResult) => productsResult.data
);

export const {
  selectAll: selectAllProducts,
  selectById: selectProductById,
  selectIds: selectProductIds,
} = productsAdapter.getSelectors(
  (state) => selectProductsData(state) ?? initialState
);

This is apiSlice

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

export const apiSlice = createApi({
  reducerPath: 'api', // optional
  baseQuery: fetchBaseQuery({ baseUrl: 'https://jsonplaceholder.typicode.com' }),
  tagTypes: ['Product', 'Category'],
  endpoints: builder => ({})
})
export default apiSlice;