markkaylor 917a40b555
refactor(helper-plugin): migrate to TS (#17746)
Co-authored-by: Fernando Chávez <fc9chavez@gmail.com>
Co-authored-by: Josh <37798644+joshuaellis@users.noreply.github.com>
Co-authored-by: markkaylor <mark.kaylor@strapi.io>
Co-authored-by: Mark Kaylor <mark.kaylor@strapi.io>
Co-authored-by: Madhuri Sandbhor <madhurisandbhor@gmail.com>
Co-authored-by: Jackson Hammond <malts18@gmail.com>
Co-authored-by: yurimutti <muttiyuri@gmail.com>
Co-authored-by: Fernando Chávez <fernando.chavez@strapi.io>
Co-authored-by: GJunior <samaritanojr006@gmail.com>
Co-authored-by: Milan K <57148574+mkcy3@users.noreply.github.com>
Co-authored-by: Rémi de Juvigny <remi.dejuvigny@strapi.io>
Co-authored-by: boris.shulyak <borysshulyak@gmail.com>
Co-authored-by: Alec Winter <34278963+nolliebigspin@users.noreply.github.com>
Co-authored-by: Boris Shulyak <55171497+BorysShulyak@users.noreply.github.com>
Co-authored-by: 0xATHERIS <ath3ris@proton.me>
Co-authored-by: Andrew <112592168+fletch-r@users.noreply.github.com>
Co-authored-by: Rémi de Juvigny <8087692+remidej@users.noreply.github.com>
Co-authored-by: Dallas Clark <542948+dallasclark@users.noreply.github.com>
Co-authored-by: Okorare Tega <contact@tegacreatives.com>
Co-authored-by: Júlíus Guðni <julius@extis.one>
Co-authored-by: Simone <startae14@gmail.com>
2023-09-15 10:08:21 +02:00

113 lines
3.2 KiB
TypeScript

/* eslint-disable check-file/filename-naming-convention */
import * as React from 'react';
import { fixtures } from '@strapi/admin-test-utils';
import { DesignSystemProvider } from '@strapi/design-system';
import {
renderHook as renderHookRTL,
render as renderRTL,
waitFor,
RenderOptions as RTLRenderOptions,
RenderResult,
act,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import PropTypes from 'prop-types';
import { IntlProvider } from 'react-intl';
import { QueryClient, QueryClientProvider } from 'react-query';
import { MemoryRouter, MemoryRouterProps } from 'react-router-dom';
import { RBACContext } from '../src/features/RBAC';
import { server } from './server';
interface ProvidersProps {
children: React.ReactNode;
initialEntries?: MemoryRouterProps['initialEntries'];
}
const Providers = ({ children, initialEntries }: ProvidersProps) => {
const rbacContextValue = React.useMemo(
() => ({
allPermissions: fixtures.permissions.allPermissions,
}),
[]
);
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
// no more errors on the console for tests
// eslint-disable-next-line @typescript-eslint/no-empty-function
onError() {},
},
},
});
return (
// en is the default locale of the admin app.
<MemoryRouter initialEntries={initialEntries}>
<IntlProvider locale="en" textComponent="span">
<DesignSystemProvider locale="en">
<QueryClientProvider client={queryClient}>
<RBACContext.Provider value={rbacContextValue}>{children}</RBACContext.Provider>
</QueryClientProvider>
</DesignSystemProvider>
</IntlProvider>
</MemoryRouter>
);
};
Providers.defaultProps = {
initialEntries: undefined,
};
Providers.propTypes = {
children: PropTypes.node.isRequired,
initialEntries: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.object])),
};
// eslint-disable-next-line react/jsx-no-useless-fragment
const fallbackWrapper = ({ children }: { children: React.ReactNode }) => <>{children}</>;
export interface RenderOptions {
renderOptions?: RTLRenderOptions;
userEventOptions?: Parameters<typeof userEvent.setup>[0];
initialEntries?: MemoryRouterProps['initialEntries'];
}
const render = (
ui: React.ReactElement,
{ renderOptions, userEventOptions, initialEntries }: RenderOptions = {}
): RenderResult & { user: ReturnType<typeof userEvent.setup> } => {
const { wrapper: Wrapper = fallbackWrapper, ...restOptions } = renderOptions ?? {};
return {
...renderRTL(ui, {
wrapper: ({ children }) => (
<Providers initialEntries={initialEntries}>
<Wrapper>{children}</Wrapper>
</Providers>
),
...restOptions,
}),
user: userEvent.setup(userEventOptions),
};
};
const renderHook: typeof renderHookRTL = (hook, options) => {
const { wrapper: Wrapper = fallbackWrapper, ...restOptions } = options ?? {};
return renderHookRTL(hook, {
wrapper: ({ children }) => (
<Providers>
<Wrapper>{children}</Wrapper>
</Providers>
),
...restOptions,
});
};
export { render, renderHook, waitFor, server, act };