@thepalaceproject/circulation-admin 1.19.0 → 1.20.0-post.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,71 @@
1
+ import * as React from "react";
2
+ import ContextProvider, {
3
+ ContextProviderProps,
4
+ } from "../../../src/components/ContextProvider";
5
+ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
6
+ import { render, RenderOptions, RenderResult } from "@testing-library/react";
7
+
8
+ export type TestProviderWrapperOptions = {
9
+ contextProviderProps?: Partial<ContextProviderProps>;
10
+ queryClient?: QueryClient;
11
+ };
12
+ export type TestRenderWrapperOptions = TestProviderWrapperOptions & {
13
+ renderOptions?: Omit<RenderOptions, "queries">;
14
+ };
15
+
16
+ // The `csrfToken` context provider prop is required, so we provide
17
+ // a default value here, so it can be easily merged with other props.
18
+ const defaultContextProviderProps = { csrfToken: "" };
19
+
20
+ /**
21
+ * Returns a component, composed with our providers, that can be used to wrap
22
+ * a React element for testing.
23
+ *
24
+ * @param {TestProviderWrapperOptions} options
25
+ * @param {ContextProviderProps} options.contextProviderProps Props to pass to the ContextProvider wrapper
26
+ * @param {QueryClient} options.queryClient A `tanstack/react-query` QueryClient
27
+ * @returns {React.FunctionComponent} A React component that wraps children with our providers
28
+ */
29
+ export const componentWithProviders = ({
30
+ contextProviderProps = { csrfToken: "" },
31
+ queryClient = new QueryClient(),
32
+ }: TestProviderWrapperOptions): React.FunctionComponent => {
33
+ const effectiveContextProviderProps = {
34
+ ...defaultContextProviderProps,
35
+ ...contextProviderProps,
36
+ };
37
+ const wrapper = ({ children }) => (
38
+ <ContextProvider {...effectiveContextProviderProps}>
39
+ <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
40
+ </ContextProvider>
41
+ );
42
+ wrapper.displayName = "TestWrapperComponent";
43
+ return wrapper;
44
+ };
45
+
46
+ /**
47
+ * Renders a React element with specified providers and provides a function for re-rendering with the same context.
48
+ *
49
+ * @param {React.ReactElement} renderChildren The element to render
50
+ * @param {TestRenderWrapperOptions} testRenderOptions Options for rendering with providers
51
+ * @returns {RenderResult} The result of rendering, including re-rendering functionality
52
+ */
53
+ export const renderWithProviders = (
54
+ renderChildren: React.ReactElement,
55
+ testRenderOptions: TestRenderWrapperOptions = {}
56
+ ): RenderResult => {
57
+ const wrapper = componentWithProviders(testRenderOptions);
58
+ const renderResult = render(
59
+ wrapper({ children: renderChildren }),
60
+ testRenderOptions.renderOptions
61
+ );
62
+
63
+ const rerenderWithProviders = (reRenderChildren: React.ReactElement) => {
64
+ return renderResult.rerender(wrapper({ children: reRenderChildren }));
65
+ };
66
+
67
+ return {
68
+ ...renderResult,
69
+ rerender: rerenderWithProviders,
70
+ };
71
+ };