-
-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathrenderComponent.test.js
More file actions
144 lines (131 loc) · 4.18 KB
/
Copy pathrenderComponent.test.js
File metadata and controls
144 lines (131 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import renderComponent from "./renderComponent";
describe("renderComponent", () => {
it("should pass both render and children prop", () => {
const children = "some children";
const render = () => {};
const props = {
component: () => null,
children,
render,
};
const name = "TestComponent";
const result = renderComponent(props, {}, name);
expect(result.props).toEqual({ children, render });
});
it("should include children when rendering with render", () => {
const children = "some children";
const render = jest.fn();
const props = {
children,
render,
};
const name = "TestComponent";
renderComponent(props, {}, name);
expect(render).toHaveBeenCalled();
expect(render).toHaveBeenCalledTimes(1);
expect(render.mock.calls[0][0].children).toBe(children);
});
it("should throw error if no render strategy is provided", () => {
const children = "some children";
const props = {
children,
};
const name = "TestComponent";
expect(() => renderComponent(props, {}, name)).toThrow(
`Must specify either a render prop, a render function as children, or a component prop to ${name}`,
);
});
it("should not evaluate any of the keys given in the second argument", () => {
const children = "some children";
const render = jest.fn();
const props = {
children,
render,
};
const getA = jest.fn();
const getB = jest.fn();
const name = "TestComponent";
renderComponent(
props,
{
get a() {
getA();
return 1;
},
get b() {
getB();
return 2;
},
},
name,
);
expect(render).toHaveBeenCalled();
expect(render).toHaveBeenCalledTimes(1);
expect(render.mock.calls[0][0].children).toBe(children);
expect(getA).not.toHaveBeenCalled();
expect(getB).not.toHaveBeenCalled();
});
it("should not overwrite getter-only properties when using component prop", () => {
// This test reproduces issue #1055
// When lazyProps has getter-only properties (like 'active'), and props contains
// a property with the same name, it should not attempt to overwrite the getter
const Component = () => null;
const props = {
component: Component,
active: "value-from-props", // This would cause "Cannot set property active" error
customProp: "custom",
};
const lazyProps = {};
Object.defineProperty(lazyProps, "active", {
get: () => "value-from-getter",
enumerable: true,
// Note: no setter - this is getter-only
});
const name = "TestComponent";
// Should not throw "Cannot set property active"
let result;
expect(() => {
result = renderComponent(props, lazyProps, name);
}).not.toThrow();
// Check the React element was created with correct props
expect(result.type).toBe(Component);
// The getter-only property should remain and use the getter value
expect(result.props.active).toBe("value-from-getter");
// Custom props should still be passed through
expect(result.props.customProp).toBe("custom");
});
it("should handle getter-only properties in all render paths", () => {
const lazyProps = {};
Object.defineProperty(lazyProps, "active", {
get: () => "getter-value",
enumerable: true,
});
// Test with render prop
const render = jest.fn();
renderComponent(
{ render, active: "prop-value" },
lazyProps,
"TestComponent",
);
expect(render).toHaveBeenCalled();
expect(render.mock.calls[0][0].active).toBe("getter-value");
// Test with children function
const children = jest.fn();
renderComponent(
{ children, active: "prop-value" },
lazyProps,
"TestComponent",
);
expect(children).toHaveBeenCalled();
expect(children.mock.calls[0][0].active).toBe("getter-value");
// Test with component prop
const Component = () => null;
const result = renderComponent(
{ component: Component, active: "prop-value" },
lazyProps,
"TestComponent",
);
expect(result.type).toBe(Component);
expect(result.props.active).toBe("getter-value");
});
});