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

snowcoders / jest-react-mock-utils / 17519210517

06 Sep 2025 08:29PM UTC coverage: 100.0%. Remained the same
17519210517

Pull #677

github

web-flow
Merge 4f132ebb4 into d058dd3ec
Pull Request #677: Update dependency lint-staged to v16.1.6

2 of 2 branches covered (100.0%)

Branch coverage included in aggregate %.

39 of 39 relevant lines covered (100.0%)

23.03 hits per line

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

100.0
/src/index.ts
1
import { jest } from "@jest/globals";
2
import React from "react";
3

4
const defaultOptions: Required<Options> = {
14✔
5
  elementType: "div",
6
};
7

8
export type Options = {
9
  /**
10
   * The HTMLElement type to render. Default is div.
11
   */
12
  elementType?: string;
13
};
14

15
/**
16
 * Generates a new mock component with the prop signature provided.
17
 *
18
 * @returns A mock component to be used in conjunction with other utilities in this library
19
 */
20
export function createMockComponent<TProps>(options?: Options): jest.Mocked<React.ComponentType<TProps>> {
21
  const { elementType } = {
22✔
22
    ...defaultOptions,
23
    ...options,
24
  };
25

26
  // Our implementation renders all components (class and function) as function components
27
  // so forcing the type to function here
28
  return jest.fn((props: TProps) => {
22✔
29
    const propsWithReactHack = {
98✔
30
      ...props,
31
      // All the react checks seem to be based off a function isCustomComponent
32
      // - Uses of isCustomComponent https://github.com/facebook/react/search?q=isCustomComponent&type=code
33
      // - Function itself https://github.com/facebook/react/blob/8e2bde6f2751aa6335f3cef488c05c3ea08e074a/packages/react-dom-bindings/src/shared/isCustomComponent.js
34
      // By providing "props.is" we can bypass the property checks all together
35
      is: elementType,
36
    };
37

38
    return React.createElement(elementType, propsWithReactHack);
98✔
39
  });
40
}
41

42
export function getMockComponentPropCalls<TProps>(
43
  mockComponent:
44
    | jest.MockedFunction<React.FC<TProps>> // Function components
45
    | typeof React.Component<TProps> // Class components
46
): Readonly<TProps>[] {
47
  // Our implementation renders all components (class and function) as function components
48
  // so forcing the type to function here
49
  const castedMockComponent = mockComponent as jest.MockedFunction<React.FC<TProps>>;
52✔
50
  const calls = castedMockComponent?.mock?.calls;
52✔
51
  if (calls == null) {
52✔
52
    throw new Error(
4✔
53
      "Parameter to getMockComponentPropCalls must be a MockComponent. Did you forget to call createMockComponent?"
54
    );
55
  }
56
  const propCalls = calls.map((value) => {
48✔
57
    return value[0];
62✔
58
  });
59
  return propCalls;
48✔
60
}
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