• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

KablamoOSS / kerosene / 13799254389

11 Mar 2025 10:14PM UTC coverage: 79.242%. Remained the same
13799254389

Pull #155

github

web-flow
Merge 11db63840 into e260abd59
Pull Request #155: build(deps): bump @babel/helpers from 7.24.1 to 7.26.10

223 of 282 branches covered (79.08%)

878 of 1108 relevant lines covered (79.24%)

6.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/packages/kerosene-test/src/react/createStubComponent.tsx
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2

3
import { identity } from "lodash";
×
4
import * as React from "react";
×
5

6
export default function createStubComponent<
7
  Props extends Record<string, unknown> = Record<string, unknown>,
8
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
9
  RenderProps extends readonly any[] = any[],
10
>(displayName: string, functional?: false): React.ComponentClass<Props>;
11

12
export default function createStubComponent<
13
  Props extends Record<string, unknown> = Record<string, unknown>,
14
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
15
  RenderProps extends readonly any[] = any[],
16
>(displayName: string, functional: true): React.FunctionComponent<Props>;
17

18
export default function createStubComponent<
19
  Props extends Record<string, unknown> = Record<string, unknown>,
20
  RenderProps extends readonly any[] = any[],
21
>(
22
  displayName: string,
23
  functional: false | undefined,
24
  getRenderProps: (props: Omit<Props, "children">) => RenderProps,
25
  transformRenderPropResult?: (
26
    result: (props: any) => React.ReactNode,
27
  ) => React.ReactNode,
28
): React.ComponentClass<
29
  Props & { children: (...args: RenderProps) => React.ReactNode }
30
>;
31

32
export default function createStubComponent<
33
  Props extends Record<string, unknown> = Record<string, unknown>,
34
  RenderProps extends readonly any[] = any[],
35
>(
36
  displayName: string,
37
  functional: true,
38
  getRenderProps: (props: Omit<Props, "children">) => RenderProps,
39
  transformRenderPropResult?: (
40
    result: (props: any) => React.ReactNode,
41
  ) => React.ReactNode,
42
): React.FunctionComponent<
43
  Props & { children: (...args: RenderProps) => React.ReactNode }
44
>;
45

46
/**
47
 * Creates a StubComponent for unit testing
48
 * @param displayName Component displayName which can be used to `EnzymeWrapper#find` the component
49
 * @param functional Whether or not use functional component instead of a class component (defaults to `false`)
50
 * @param getRenderProps Function that returns props that will be passed the the children function
51
 * @param transformRenderPropResult Function to transform the result returned by the render prop (if, for example,
52
 * the returned result is a function itself)
53
 */
54
export default function createStubComponent<
×
55
  Props extends { children?: unknown } = { [prop: string]: unknown },
56
  RenderProps extends readonly any[] = any[],
57
>(
58
  displayName: string,
59
  functional = false,
×
60
  getRenderProps: (props: Omit<Props, "children">) => RenderProps = identity,
×
61
  transformRenderPropResult: (
×
62
    result: (props: any) => React.ReactNode,
63
  ) => React.ReactNode = identity,
64
): React.ComponentType<Props> {
65
  if (functional) {
×
66
    return Object.assign(
×
67
      ({ children, ...props }: Props) => (
68
        // eslint-disable-next-line react/no-unknown-property
69
        <div __displayName__={displayName} {...props}>
×
70
          {typeof children === "function"
×
71
            ? transformRenderPropResult(
72
                (children as (...args: RenderProps) => any)(
73
                  ...getRenderProps(props),
74
                ),
75
              )
76
            : (children as React.ReactNode)}
77
        </div>
78
      ),
79
      { displayName },
80
    );
81
  }
82

83
  // eslint-disable-next-line react/prefer-stateless-function
84
  return class extends React.Component<Props> {
×
85
    public static displayName = displayName;
×
86

87
    public override render() {
88
      const { children, ...props } = this.props;
×
89

90
      return (
×
91
        // eslint-disable-next-line react/no-unknown-property
92
        <div __displayName__={displayName} {...props}>
93
          {typeof children === "function"
×
94
            ? transformRenderPropResult(
95
                (children as (...args: RenderProps) => React.ReactNode)(
96
                  ...getRenderProps(props),
97
                ) as any,
98
              )
99
            : (children as React.ReactNode)}
100
        </div>
101
      );
102
    }
103
  };
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc