This repository has been archived by the owner on Mar 27, 2022. It is now read-only.
forked from idletea/jsx-no-react
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmodule.test.js
248 lines (207 loc) · 6.77 KB
/
module.test.js
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import jsxElem, { render, renderAppend, renderAfter, renderBefore, renderPrepend } from "./module";
import expectExport from "expect";
describe("jsxElement usage", () => {
it("renders a basic html document", () => {
const element = (
<div className="highlight" attribute="true">
Hello <span>world</span>
</div>
);
expect(element.outerHTML).toEqual(
'<div class="highlight" attribute="true">Hello <span>world</span></div>'
);
});
it("renders a style object as string attribute", () => {
const element = (
<div
className="highlight"
style={{ left: "20px", marginBottom: "10px" }}
/>
);
expect(element.outerHTML).toEqual(
'<div class="highlight" style="left: 20px; margin-bottom: 10px"></div>'
);
});
it("renders other objecs as string without camelcasing", () => {
const element = (
<div
className="highlight"
attribute={{ left: "20px", marginBottom: "10px" }}
/>
);
expect(element.outerHTML).toEqual(
'<div class="highlight" attribute="left: 20px; marginbottom: 10px"></div>'
);
});
it("renders a svg with the correct namespace", () => {
const element = (
<svg><circle cx="80" cy="80" r="30" fill="red" /></svg>
);
expect(element.outerHTML).toEqual(
'<svg><circle cx="80" cy="80" r="30" fill="red"></circle></svg>'
);
expect(element.namespaceURI).toEqual("http://www.w3.org/2000/svg")
});
it("sets an attribute if it is true", () => {
const element = <div className="highlight" required={true} />;
expect(element.outerHTML).toEqual(
'<div class="highlight" required="required"></div>'
);
});
it("calls a defined method for components", () => {
function App(props) {
return <div>Hello {props.name}</div>;
}
const element = <App name="world" />;
expect(element.outerHTML).toEqual("<div>Hello world</div>");
});
it("renders an object with a render method", () => {
const Component = {
render() {
return <div>Hello</div>;
}
};
const element = <Component />;
expect(element.outerHTML).toEqual("<div>Hello</div>");
});
it("adds an event function", () => {
const mockFunction = jest.fn();
const element = <div onClick={mockFunction} />;
expect(element.outerHTML).toEqual("<div></div>");
element.click();
expect(mockFunction.mock.calls.length).toBe(1);
});
it("does not render props as attributes", () => {
function Hello(props) {
return <h1>Hello {props.name}</h1>;
}
const CustomSeparator = props => (
<i>{[...Array(props.dots)].map(idx => ".")}</i>
);
const element = <div>
<Hello name="foo" />
<CustomSeparator dots={50} />
<Hello name="bar" />
</div>;
expect(element.outerHTML).toEqual("<div><h1>Hello foo</h1><i>..................................................</i><h1>Hello bar</h1></div>");
});
it("support fragments", () => {
function Hello(props) {
return <>
<h1>Hello</h1>
<h1>world</h1>
</>;
}
const element = <div><Hello /></div>;
expect(element.innerHTML).toEqual("<h1>Hello</h1><h1>world</h1>");
});
});
describe("render", () => {
it("replaces content and adds the output", () => {
function Hello(props) {
return <h1>Hello {props.name}</h1>;
}
const mockParent = <div><h1>Exist</h1></div>;
render(<Hello name="world" />, mockParent);
expect(mockParent.innerHTML).toEqual(
"<h1>Hello world</h1>"
);
});
it("replaces contents when top-level JSX element is a fragment", () => {
const mockParent = <div><h1>Exist</h1></div>;
const fragmentChild = <>
<h1>Hello</h1>
<h1>World</h1>
</>;
render(fragmentChild, mockParent);
expect(mockParent.innerHTML).toEqual(
"<h1>Hello</h1><h1>World</h1>"
);
});
});
describe("renderAppend", () => {
it("adds the output before the end of the element", () => {
function Hello(props) {
return <h1>Hello {props.name}</h1>;
}
const mockParent = <div><h1>Exist</h1></div>;
renderAppend(<Hello name="world" />, mockParent);
expect(mockParent.innerHTML).toEqual(
"<h1>Exist</h1><h1>Hello world</h1>"
);
});
it("appends contents when top-level JSX element is a fragment", () => {
const mockParent = <div><h1>Exist</h1></div>;
const fragmentChild = <>
<h1>Hello</h1>
<h1>World</h1>
</>;
renderAppend(fragmentChild, mockParent);
expect(mockParent.innerHTML).toEqual(
"<h1>Exist</h1><h1>Hello</h1><h1>World</h1>"
);
});
});
describe("renderPrepend", () => {
it("adds the output at the start of the element", () => {
function Hello(props) {
return <h1>Hello {props.name}</h1>;
}
const mockParent = <div><h1>Exist</h1></div>;
renderPrepend(<Hello name="world" />, mockParent);
expect(mockParent.innerHTML).toEqual(
"<h1>Hello world</h1><h1>Exist</h1>"
);
});
it("prepends contents when top-level JSX element is a fragment", () => {
const mockParent = <div><h1>Exist</h1></div>;
const fragmentChild = <>
<h1>Hello</h1>
<h1>World</h1>
</>;
renderPrepend(fragmentChild, mockParent);
expect(mockParent.innerHTML).toEqual(
"<h1>Hello</h1><h1>World</h1><h1>Exist</h1>"
);
});
});
describe("renderAfter", () => {
it("adds the output after the end of the element", () => {
function Hello(props) {
return <h1>Hello {props.name}</h1>;
}
const mockElement = jest.fn();
renderAfter(<Hello name="world" />, { insertAdjacentElement: mockElement });
expect(mockElement.mock.calls.length).toBe(1);
expect(mockElement.mock.calls[0][0]).toBe("afterend");
expect(mockElement.mock.calls[0][1].outerHTML).toEqual(
"<h1>Hello world</h1>"
);
});
it("throws an error when given a fragment", () => {
const mockElement = jest.fn();
expect(() => {
renderAfter(<></>, { insertAdjacentElement: mockElement });
}).toThrow("renderAfter does not support top-level fragment rendering");
});
});
describe("renderBefore", () => {
it("adds the output before the start of the element", () => {
function Hello(props) {
return <h1>Hello {props.name}</h1>;
}
const mockElement = jest.fn();
renderBefore(<Hello name="world" />, { insertAdjacentElement: mockElement });
expect(mockElement.mock.calls.length).toBe(1);
expect(mockElement.mock.calls[0][0]).toBe("beforebegin");
expect(mockElement.mock.calls[0][1].outerHTML).toEqual(
"<h1>Hello world</h1>"
);
});
it("throws an error when given a fragment", () => {
const mockElement = jest.fn();
expect(() => {
renderBefore(<></>, { insertAdjacentElement: mockElement });
}).toThrow("renderBefore does not support top-level fragment rendering");
});
});