Skip to content

Commit

Permalink
Update to Decorator that it acts as FC
Browse files Browse the repository at this point in the history
  • Loading branch information
matthijsgroen committed Jun 21, 2024
1 parent 1f67588 commit 724d5d6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/support/testing/testComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export type Decorator<
* @param settings settings to apply on the decoration
* @returns an updated JSX structure
*/
decorator: (Component: React.FC, settings: TSettings) => JSX.Element;
Decorator: React.FC<{ Component: React.FC; settings: TSettings }>;
};

const hasAlreadyRendered = (
Expand All @@ -71,7 +71,10 @@ const hasAlreadyRendered = (
document.body.firstChild === lastRender.baseElement.firstChild;

/**
* Set the component subject of this test
* Set the component subject of this test.
*
* This component should be a FunctionalComponent. If you want
* to test a ClassComponent, you can wrap your component with `makeFC`
*
* @example ```
* const { renderComponent } = setComponent(YourComponent)
Expand Down Expand Up @@ -128,8 +131,12 @@ export const setComponent = <
}

const jsxStructure = (settings.decorators ?? []).reduce(
(result, dec) =>
dec.decorator(() => result, decoratorSettings[dec.name]),
(result, dec) => (
<dec.Decorator
Component={() => result}
settings={decoratorSettings[dec.name]}
/>
),
<Component {...initialProps} {...props} />,
);

Expand Down Expand Up @@ -173,6 +180,11 @@ export const setComponent = <
};
};

/**
* Convert a ClassComponent into a FunctionalComponent.
* The ref that can be supplied will be a ref to the instance of the
* ClassComponent.
*/
export const makeFC = <TComponentProps,>(
Component: React.ComponentClass<TComponentProps>,
): React.ForwardRefExoticComponent<
Expand Down
8 changes: 4 additions & 4 deletions src/support/testing/testDecorators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Context as ResponsiveContext } from "react-responsive";
export const responsiveDecorator: Decorator<"responsive", { width: number }> = {
name: "responsive",
settings: { width: 1200 },
decorator: (Component, settings) => (
Decorator: ({ Component, settings }) => (
<ResponsiveContext.Provider value={{ width: settings.width }}>
<Component />
</ResponsiveContext.Provider>
Expand All @@ -23,7 +23,7 @@ export const dataDecorator = <TSerialized extends object>(
> => ({
name: "application",
settings: { useTypeName: true, mocks, cache },
decorator: (Component, settings) => (
Decorator: ({ Component, settings }) => (
<MockedProvider
mocks={settings.mocks}
addTypename={settings.useTypeName}
Expand All @@ -42,7 +42,7 @@ export const routingDecorator = (): Decorator<
settings: {
paths: {},
},
decorator: (Component, settings) => (
Decorator: ({ Component, settings }) => (
<MemoryRouter>
<Component />
{Object.entries(settings.paths).map(([path, content]) => (
Expand All @@ -57,7 +57,7 @@ export const routingDecorator = (): Decorator<
export const tableDecorator: Decorator<"table", Record<string, unknown>> = {
name: "table",
settings: {},
decorator: (Component) => (
Decorator: ({ Component }) => (
<table>
<tbody>
<Component />
Expand Down

0 comments on commit 724d5d6

Please sign in to comment.