|
| 1 | +import * as React from 'react' |
| 2 | +import type { ReactReduxContextValue } from 'react-redux' |
| 3 | +import { |
| 4 | + createDispatchHook, |
| 5 | + createSelectorHook, |
| 6 | + createStoreHook, |
| 7 | + Provider, |
| 8 | +} from 'react-redux' |
| 9 | +import { |
| 10 | + buildCreateApi, |
| 11 | + coreModule, |
| 12 | + reactHooksModule, |
| 13 | +} from '@reduxjs/toolkit/query/react' |
| 14 | +import { |
| 15 | + act, |
| 16 | + fireEvent, |
| 17 | + render, |
| 18 | + screen, |
| 19 | + waitFor, |
| 20 | + renderHook, |
| 21 | +} from '@testing-library/react' |
| 22 | +import userEvent from '@testing-library/user-event' |
| 23 | +import { rest } from 'msw' |
| 24 | +import { |
| 25 | + actionsReducer, |
| 26 | + ANY, |
| 27 | + expectExactType, |
| 28 | + expectType, |
| 29 | + setupApiStore, |
| 30 | + withProvider, |
| 31 | + useRenderCounter, |
| 32 | + waitMs, |
| 33 | +} from './helpers' |
| 34 | +import { server } from './mocks/server' |
| 35 | +import type { UnknownAction } from 'redux' |
| 36 | +import type { SubscriptionOptions } from '@reduxjs/toolkit/dist/query/core/apiState' |
| 37 | +import type { SerializedError } from '@reduxjs/toolkit' |
| 38 | +import { createListenerMiddleware, configureStore } from '@reduxjs/toolkit' |
| 39 | +import { delay } from '../../utils' |
| 40 | + |
| 41 | +const MyContext = React.createContext<ReactReduxContextValue>(null as any) |
| 42 | + |
| 43 | +describe('buildCreateApi', () => { |
| 44 | + test('Works with all hooks provided', async () => { |
| 45 | + const customCreateApi = buildCreateApi( |
| 46 | + coreModule(), |
| 47 | + reactHooksModule({ |
| 48 | + hooks: { |
| 49 | + useDispatch: createDispatchHook(MyContext), |
| 50 | + useSelector: createSelectorHook(MyContext), |
| 51 | + useStore: createStoreHook(MyContext), |
| 52 | + }, |
| 53 | + }) |
| 54 | + ) |
| 55 | + |
| 56 | + const api = customCreateApi({ |
| 57 | + baseQuery: async (arg: any) => { |
| 58 | + await waitMs() |
| 59 | + |
| 60 | + return { |
| 61 | + data: arg?.body ? { ...arg.body } : {}, |
| 62 | + } |
| 63 | + }, |
| 64 | + endpoints: (build) => ({ |
| 65 | + getUser: build.query<{ name: string }, number>({ |
| 66 | + query: () => ({ |
| 67 | + body: { name: 'Timmy' }, |
| 68 | + }), |
| 69 | + }), |
| 70 | + }), |
| 71 | + }) |
| 72 | + |
| 73 | + let getRenderCount: () => number = () => 0 |
| 74 | + |
| 75 | + const storeRef = setupApiStore(api, {}, { withoutTestLifecycles: true }) |
| 76 | + |
| 77 | + // Copy of 'useQuery hook basic render count assumptions' from `buildHooks.test.tsx` |
| 78 | + function User() { |
| 79 | + const { isFetching } = api.endpoints.getUser.useQuery(1) |
| 80 | + getRenderCount = useRenderCounter() |
| 81 | + |
| 82 | + return ( |
| 83 | + <div> |
| 84 | + <div data-testid="isFetching">{String(isFetching)}</div> |
| 85 | + </div> |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + function Wrapper({ children }: any) { |
| 90 | + return ( |
| 91 | + <Provider store={storeRef.store} context={MyContext}> |
| 92 | + {children} |
| 93 | + </Provider> |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + render(<User />, { wrapper: Wrapper }) |
| 98 | + // By the time this runs, the initial render will happen, and the query |
| 99 | + // will start immediately running by the time we can expect this |
| 100 | + expect(getRenderCount()).toBe(2) |
| 101 | + |
| 102 | + await waitFor(() => |
| 103 | + expect(screen.getByTestId('isFetching').textContent).toBe('false') |
| 104 | + ) |
| 105 | + expect(getRenderCount()).toBe(3) |
| 106 | + }) |
| 107 | + |
| 108 | + test("Throws an error if you don't provide all hooks", async () => { |
| 109 | + const callBuildCreateApi = () => { |
| 110 | + const customCreateApi = buildCreateApi( |
| 111 | + coreModule(), |
| 112 | + reactHooksModule({ |
| 113 | + // @ts-ignore |
| 114 | + hooks: { |
| 115 | + useDispatch: createDispatchHook(MyContext), |
| 116 | + useSelector: createSelectorHook(MyContext), |
| 117 | + }, |
| 118 | + }) |
| 119 | + ) |
| 120 | + } |
| 121 | + |
| 122 | + expect(callBuildCreateApi).toThrowErrorMatchingInlineSnapshot( |
| 123 | + `"When using custom hooks for context, all 3 hooks need to be provided: useDispatch, useSelector, useStore.\nHook useStore was either not provided or not a function."` |
| 124 | + ) |
| 125 | + }) |
| 126 | +}) |
0 commit comments