Files
mostlymatter/webapp/channels/src/tests/react_testing_utils.tsx
Harrison Healey be34a5d2df Run component tests with a real store and use renderWithContext everywhere (#25217)
* Change renderWithFullContext to not mock Redux store

* Remove renderWithIntl and renderWithIntlAndStore

* Rename renderWithFullContext to renderWithContext

* Use renderWithContext for WS context
2023-11-08 17:02:40 -05:00

106 строки
3.6 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {render} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {createBrowserHistory} from 'history';
import React from 'react';
import {IntlProvider} from 'react-intl';
import {Provider} from 'react-redux';
import {Router} from 'react-router-dom';
import type {DeepPartial} from '@mattermost/types/utilities';
import configureStore from 'store';
import WebSocketClient from 'client/web_websocket_client';
import mergeObjects from 'packages/mattermost-redux/test/merge_objects';
import mockStore from 'tests/test_store';
import {WebSocketContext} from 'utils/use_websocket';
import type {GlobalState} from 'types/store';
export * from '@testing-library/react';
export {userEvent};
export type FullContextOptions = {
locale?: string;
useMockedStore?: boolean;
}
export const renderWithContext = (
component: React.ReactElement,
initialState: DeepPartial<GlobalState> = {},
partialOptions?: FullContextOptions,
) => {
const options = {
locale: partialOptions?.locale ?? 'en',
useMockedStore: partialOptions?.useMockedStore ?? false,
};
const testStore = configureOrMockStore(initialState, options.useMockedStore);
// Store these in an object so that they can be maintained through rerenders
const renderState = {
component,
history: createBrowserHistory(),
options,
store: testStore,
};
// This should wrap the component in roughly the same providers used in App and RootProvider
function WrapComponent(props: {children: React.ReactElement}) {
// Every time this is called, these values should be updated from `renderState`
return (
<Provider store={renderState.store}>
<Router history={renderState.history}>
<IntlProvider
locale={renderState.options.locale}
>
<WebSocketContext.Provider value={WebSocketClient}>
{props.children}
</WebSocketContext.Provider>
</IntlProvider>
</Router>
</Provider>
);
}
const results = render(component, {wrapper: WrapComponent});
return {
...results,
rerender: (newComponent: React.ReactElement) => {
renderState.component = newComponent;
results.rerender(renderState.component);
},
/**
* Rerenders the component after replacing the entire store state with the provided one.
*/
replaceStoreState: (newInitialState: DeepPartial<GlobalState>) => {
renderState.store = configureOrMockStore(newInitialState, renderState.options.useMockedStore);
results.rerender(renderState.component);
},
/**
* Rerenders the component after merging the current store state with the provided one.
*/
updateStoreState: (stateDiff: DeepPartial<GlobalState>) => {
const newInitialState = mergeObjects(renderState.store.getState(), stateDiff);
renderState.store = configureOrMockStore(newInitialState, renderState.options.useMockedStore);
results.rerender(renderState.component);
},
};
};
function configureOrMockStore<T>(initialState: DeepPartial<T>, useMockedStore: boolean) {
let testStore = configureStore(initialState);
if (useMockedStore) {
testStore = mockStore(testStore.getState());
}
return testStore;
}