Skip to content

Commit 24f6078

Browse files
authored
Fixed React warnings/errors about custom property names (#20)
2 parents 37b96b1 + f35b0ed commit 24f6078

File tree

14 files changed

+63
-22
lines changed

14 files changed

+63
-22
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.0.2] - 2023-01-13
11+
12+
- Fixed React console logging errors and warnings for properties it doesn't recognize as default HTML attributes
13+
1014
## [0.0.1] - 2023-01-06
1115

1216
- Fixed ReturnType causing typescript compiile issues with createMockComponent

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@
7474
},
7575
"sideEffects": false,
7676
"type": "module",
77-
"version": "0.0.1"
77+
"version": "0.0.2"
7878
}

src/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export type Options = {
1515
/**
1616
* Generates a new mock component with the prop signature provided.
1717
*
18-
* @param {string} elementType the HTMLElement type to render. Default is div.
1918
* @returns A mock component to be used in conjunction with other utilities in this library
2019
*/
2120
export function createMockComponent<TProps>(options?: Options): jest.Mocked<React.ComponentType<TProps>> {
@@ -27,14 +26,16 @@ export function createMockComponent<TProps>(options?: Options): jest.Mocked<Reac
2726
// Our implementation renders all components (class and function) as function components
2827
// so forcing the type to function here
2928
return jest.fn((props: TProps) => {
30-
if (props == null) {
31-
return React.createElement(elementType);
32-
} else if (typeof props === "object" && "children" in props) {
33-
const { children, ...rest } = props as React.PropsWithChildren<TProps>;
34-
return React.createElement(elementType, rest, children);
35-
} else {
36-
return React.createElement(elementType, props);
37-
}
29+
const propsWithReactHack = {
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);
3839
});
3940
}
4041

src/tests-class-component/child-with-children/test-asset.child.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import React, { HtmlHTMLAttributes } from "react";
22

3-
export type ChildProps = React.PropsWithChildren<Pick<HtmlHTMLAttributes<HTMLButtonElement>, "onClick">>;
3+
export type ChildProps = React.PropsWithChildren<
4+
Pick<HtmlHTMLAttributes<HTMLButtonElement>, "onClick"> & {
5+
someData: string;
6+
onPointerEnter: () => void;
7+
}
8+
>;
49

510
export class Child extends React.Component<ChildProps> {
611
render() {

src/tests-class-component/child-with-children/test-asset.parent.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ export class Parent extends React.Component<ParentProps, ParentState> {
3636
<div>
3737
Click count is <span data-testid={parentTestIdMap.clickCount}>{clickCount}</span>
3838
</div>
39-
<Child data-testid={parentTestIdMap.child} onClick={this.handleChildClick}>
39+
<Child
40+
data-testid={parentTestIdMap.child}
41+
onClick={this.handleChildClick}
42+
someData="someData"
43+
onPointerEnter={() => {}}
44+
>
4045
Increment
4146
</Child>
4247
</div>

src/tests-class-component/child-with-props/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("Github issue #7 - Using ReturnType on createMockComponent", () => {
4040
it("Renders dynamic import child", async () => {
4141
// Arrange
4242
const spy = jest.fn();
43-
const result = render(<Child onClick={spy} />);
43+
const result = render(<Child onClick={spy} someData="someData" onPointerEnter={() => {}} />);
4444

4545
// Act
4646
await userEvent.click(result.getByRole("button"));

src/tests-class-component/child-with-props/test-asset.child.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import React, { HtmlHTMLAttributes } from "react";
22

3-
export type ChildProps = Pick<HtmlHTMLAttributes<HTMLButtonElement>, "onClick">;
3+
export type ChildProps = Pick<HtmlHTMLAttributes<HTMLButtonElement>, "onClick"> & {
4+
someData: string;
5+
onPointerEnter: () => void;
6+
};
47

58
export class Child extends React.Component<ChildProps> {
69
render() {

src/tests-class-component/child-with-props/test-asset.parent.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ export class Parent extends React.Component<ParentProps, ParentState> {
3636
<div>
3737
Click count is <span data-testid={parentTestIdMap.clickCount}>{clickCount}</span>
3838
</div>
39-
<Child data-testid={parentTestIdMap.child} onClick={this.handleChildClick} />
39+
<Child
40+
data-testid={parentTestIdMap.child}
41+
onClick={this.handleChildClick}
42+
someData="someData"
43+
onPointerEnter={() => {}}
44+
/>
4045
</div>
4146
);
4247
}

src/tests-functional-component/child-with-children/test-asset.child.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import React, { HtmlHTMLAttributes } from "react";
22

3-
export type ChildProps = React.PropsWithChildren<Pick<HtmlHTMLAttributes<HTMLButtonElement>, "onClick">>;
3+
export type ChildProps = React.PropsWithChildren<
4+
Pick<HtmlHTMLAttributes<HTMLButtonElement>, "onClick"> & {
5+
someData: string;
6+
onPointerEnter: () => void;
7+
}
8+
>;
49

510
export function Child(props: ChildProps) {
611
const { onClick, children } = props;

0 commit comments

Comments
 (0)