Skip to content

Commit 0a06528

Browse files
author
Erik Rasmussen
committed
Fix test: Check React element props instead of component calls
React.createElement doesn't call the component immediately - it returns an element. Updated tests to check result.type and result.props instead of expecting component function to be called.
1 parent eba0bb6 commit 0a06528

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

src/renderComponent.test.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe("renderComponent", () => {
7474
// This test reproduces issue #1055
7575
// When lazyProps has getter-only properties (like 'active'), and props contains
7676
// a property with the same name, it should not attempt to overwrite the getter
77-
const Component = jest.fn(() => null);
77+
const Component = () => null;
7878
const props = {
7979
component: Component,
8080
active: "value-from-props", // This would cause "Cannot set property active" error
@@ -89,27 +89,30 @@ describe("renderComponent", () => {
8989
const name = "TestComponent";
9090

9191
// Should not throw "Cannot set property active"
92-
expect(() => renderComponent(props, lazyProps, name)).not.toThrow();
93-
94-
expect(Component).toHaveBeenCalled();
95-
const componentProps = Component.mock.calls[0][0];
92+
let result;
93+
expect(() => {
94+
result = renderComponent(props, lazyProps, name);
95+
}).not.toThrow();
9696

97+
// Check the React element was created with correct props
98+
expect(result.type).toBe(Component);
99+
97100
// The getter-only property should remain and use the getter value
98-
expect(componentProps.active).toBe("value-from-getter");
101+
expect(result.props.active).toBe("value-from-getter");
99102

100103
// Custom props should still be passed through
101-
expect(componentProps.customProp).toBe("custom");
104+
expect(result.props.customProp).toBe("custom");
102105
});
103106

104107
it("should handle getter-only properties in all render paths", () => {
105-
// Test with render prop
106-
const render = jest.fn();
107108
const lazyProps = {};
108109
Object.defineProperty(lazyProps, "active", {
109110
get: () => "getter-value",
110111
enumerable: true,
111112
});
112113

114+
// Test with render prop
115+
const render = jest.fn();
113116
renderComponent(
114117
{ render, active: "prop-value" },
115118
lazyProps,
@@ -127,5 +130,15 @@ describe("renderComponent", () => {
127130
);
128131
expect(children).toHaveBeenCalled();
129132
expect(children.mock.calls[0][0].active).toBe("getter-value");
133+
134+
// Test with component prop
135+
const Component = () => null;
136+
const result = renderComponent(
137+
{ component: Component, active: "prop-value" },
138+
lazyProps,
139+
"TestComponent",
140+
);
141+
expect(result.type).toBe(Component);
142+
expect(result.props.active).toBe("getter-value");
130143
});
131144
});

0 commit comments

Comments
 (0)