mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-06 13:33:55 -07:00
use component hooks
This commit is contained in:
parent
515dff6d7b
commit
3aa8c654cc
81 changed files with 5203 additions and 3173 deletions
53
webclient/src/__test-utils__/makeHookWrapper.tsx
Normal file
53
webclient/src/__test-utils__/makeHookWrapper.tsx
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
import { ReactNode } from 'react';
|
||||
import { Provider } from 'react-redux';
|
||||
import { configureStore, Reducer } from '@reduxjs/toolkit';
|
||||
|
||||
import { WebClientContext } from '../hooks/useWebClient';
|
||||
import type { WebClient } from '../websocket';
|
||||
import { createMockWebClient } from './mockWebClient';
|
||||
|
||||
// Minimal Provider wrapper for hook-only tests. Use this instead of
|
||||
// `renderWithProviders` when you need `renderHook` — the full provider tree
|
||||
// auto-instantiates the singleton store via `@app/store`, which races with
|
||||
// any test-local store you preload. Deep-import the reducer(s) you need and
|
||||
// pass them here (see useCurrentGame.spec.tsx for the canonical pattern).
|
||||
|
||||
export function makeReduxHookWrapper<S>(
|
||||
reducer: Reducer<S>,
|
||||
preloadedState: S,
|
||||
) {
|
||||
const store = configureStore({
|
||||
reducer,
|
||||
preloadedState: preloadedState as Parameters<typeof configureStore>[0]['preloadedState'],
|
||||
});
|
||||
function Wrapper({ children }: { children: ReactNode }) {
|
||||
return <Provider store={store}>{children}</Provider>;
|
||||
}
|
||||
return { Wrapper, store };
|
||||
}
|
||||
|
||||
export interface MakeReduxWebClientHookWrapperOptions<S> {
|
||||
reducer: Reducer<S>;
|
||||
preloadedState: S;
|
||||
webClient?: WebClient;
|
||||
}
|
||||
|
||||
export function makeReduxWebClientHookWrapper<S>({
|
||||
reducer,
|
||||
preloadedState,
|
||||
webClient,
|
||||
}: MakeReduxWebClientHookWrapperOptions<S>) {
|
||||
const store = configureStore({
|
||||
reducer,
|
||||
preloadedState: preloadedState as Parameters<typeof configureStore>[0]['preloadedState'],
|
||||
});
|
||||
const client = webClient ?? createMockWebClient();
|
||||
function Wrapper({ children }: { children: ReactNode }) {
|
||||
return (
|
||||
<Provider store={store}>
|
||||
<WebClientContext value={client}>{children}</WebClientContext>
|
||||
</Provider>
|
||||
);
|
||||
}
|
||||
return { Wrapper, store, webClient: client };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue