From 63f708fa2648e253b7e85b16fbe119ba9f281468 Mon Sep 17 00:00:00 2001 From: EskiMojo14 Date: Fri, 29 Mar 2024 21:32:42 +0000 Subject: [PATCH] add custom context test for apiprovider --- .../src/query/tests/apiProvider.test.tsx | 95 ++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/packages/toolkit/src/query/tests/apiProvider.test.tsx b/packages/toolkit/src/query/tests/apiProvider.test.tsx index 80101a8386..f6a4cea10a 100644 --- a/packages/toolkit/src/query/tests/apiProvider.test.tsx +++ b/packages/toolkit/src/query/tests/apiProvider.test.tsx @@ -1,9 +1,21 @@ import { configureStore } from '@reduxjs/toolkit' -import { ApiProvider, createApi } from '@reduxjs/toolkit/query/react' +import { + ApiProvider, + buildCreateApi, + coreModule, + createApi, + reactHooksModule, +} from '@reduxjs/toolkit/query/react' import { fireEvent, render, waitFor } from '@testing-library/react' import { delay } from 'msw' import * as React from 'react' -import { Provider } from 'react-redux' +import type { ReactReduxContextValue } from 'react-redux' +import { + Provider, + createDispatchHook, + createSelectorHook, + createStoreHook, +} from 'react-redux' const api = createApi({ baseQuery: async (arg: any) => { @@ -70,4 +82,83 @@ describe('ApiProvider', () => { `[Error: Existing Redux context detected. If you already have a store set up, please use the traditional Redux setup.]`, ) }) + test('ApiProvider allows a custom context', async () => { + const customContext = React.createContext( + null, + ) + + const createApiWithCustomContext = buildCreateApi( + coreModule(), + reactHooksModule({ + hooks: { + useStore: createStoreHook(customContext), + useSelector: createSelectorHook(customContext), + useDispatch: createDispatchHook(customContext), + }, + }), + ) + + const customApi = createApiWithCustomContext({ + baseQuery: async (arg: any) => { + await delay(150) + return { data: arg?.body ? arg.body : null } + }, + endpoints: (build) => ({ + getUser: build.query({ + query: (arg) => arg, + }), + updateUser: build.mutation({ + query: (update) => ({ body: update }), + }), + }), + }) + + function User() { + const [value, setValue] = React.useState(0) + + const { isFetching } = customApi.endpoints.getUser.useQuery(1, { + skip: value < 1, + }) + + return ( +
+
{String(isFetching)}
+ +
+ ) + } + + const { getByText, getByTestId } = render( + + + , + ) + + await waitFor(() => + expect(getByTestId('isFetching').textContent).toBe('false'), + ) + fireEvent.click(getByText('Increment value')) + await waitFor(() => + expect(getByTestId('isFetching').textContent).toBe('true'), + ) + await waitFor(() => + expect(getByTestId('isFetching').textContent).toBe('false'), + ) + fireEvent.click(getByText('Increment value')) + // Being that nothing has changed in the args, this should never fire. + expect(getByTestId('isFetching').textContent).toBe('false') + + // won't throw if nested, because context is different + expect(() => + render( + null })}> + + child + + , + ), + ).not.toThrow() + }) })