|
1 | | -import type { Action, ThunkAction } from "@reduxjs/toolkit" |
2 | | -import { combineSlices, configureStore } from "@reduxjs/toolkit" |
3 | | -import { setupListeners } from "@reduxjs/toolkit/query" |
4 | | -import { counterSlice } from "../features/counter/counterSlice" |
5 | | -import { quotesApiSlice } from "../features/quotes/quotesApiSlice" |
| 1 | +import type { Action, ThunkAction } from "@reduxjs/toolkit"; |
| 2 | +import { combineSlices, configureStore } from "@reduxjs/toolkit"; |
| 3 | +import { setupListeners } from "@reduxjs/toolkit/query"; |
| 4 | +import { counterSlice } from "../features/counter/counterSlice"; |
| 5 | +import { quotesApiSlice } from "../features/quotes/quotesApiSlice"; |
6 | 6 |
|
7 | 7 | // `combineSlices` automatically combines the reducers using |
8 | 8 | // their `reducerPath`s, therefore we no longer need to call `combineReducers`. |
9 | | -const rootReducer = combineSlices(counterSlice, quotesApiSlice) |
| 9 | +const rootReducer = combineSlices(counterSlice, quotesApiSlice); |
10 | 10 | // Infer the `RootState` type from the root reducer |
11 | | -export type RootState = ReturnType<typeof rootReducer> |
| 11 | +export type RootState = ReturnType<typeof rootReducer>; |
12 | 12 |
|
| 13 | +// The store setup is wrapped in `makeStore` to allow reuse |
| 14 | +// when setting up tests that need the same store config |
13 | 15 | export const makeStore = (preloadedState?: Partial<RootState>) => { |
14 | 16 | const store = configureStore({ |
15 | 17 | reducer: rootReducer, |
16 | 18 | // Adding the api middleware enables caching, invalidation, polling, |
17 | 19 | // and other useful features of `rtk-query`. |
18 | | - middleware: getDefaultMiddleware => { |
19 | | - return getDefaultMiddleware().concat(quotesApiSlice.middleware) |
| 20 | + middleware: (getDefaultMiddleware) => { |
| 21 | + return getDefaultMiddleware().concat(quotesApiSlice.middleware); |
20 | 22 | }, |
21 | 23 | preloadedState, |
22 | | - }) |
| 24 | + }); |
23 | 25 | // configure listeners using the provided defaults |
24 | 26 | // optional, but required for `refetchOnFocus`/`refetchOnReconnect` behaviors |
25 | | - setupListeners(store.dispatch) |
26 | | - return store |
27 | | -} |
| 27 | + setupListeners(store.dispatch); |
| 28 | + return store; |
| 29 | +}; |
28 | 30 |
|
29 | | -export const store = makeStore() |
| 31 | +export const store = makeStore(); |
30 | 32 |
|
31 | 33 | // Infer the type of `store` |
32 | | -export type AppStore = typeof store |
| 34 | +export type AppStore = typeof store; |
33 | 35 | // Infer the `AppDispatch` type from the store itself |
34 | | -export type AppDispatch = AppStore["dispatch"] |
| 36 | +export type AppDispatch = AppStore["dispatch"]; |
35 | 37 | export type AppThunk<ThunkReturnType = void> = ThunkAction< |
36 | 38 | ThunkReturnType, |
37 | 39 | RootState, |
38 | 40 | unknown, |
39 | 41 | Action |
40 | | -> |
| 42 | +>; |
0 commit comments