forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReactHTMLServer-test.js
204 lines (182 loc) · 5.77 KB
/
ReactHTMLServer-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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
*/
'use strict';
global.TextDecoder = require('util').TextDecoder;
global.TextEncoder = require('util').TextEncoder;
let React;
let ReactHTML;
if (!__EXPERIMENTAL__) {
it('should not be built in stable', () => {
try {
require('react-html');
} catch (x) {
return;
}
throw new Error('Expected react-html not to exist in stable.');
});
} else {
describe('ReactHTML', () => {
beforeEach(() => {
jest.resetModules();
// We run in the react-server condition.
jest.mock('react', () => require('react/react.react-server'));
if (__EXPERIMENTAL__) {
jest.mock('react-html', () =>
require('react-html/react-html.react-server'),
);
}
React = require('react');
if (__EXPERIMENTAL__) {
ReactHTML = require('react-html');
} else {
try {
require('react-html/react-html.react-server');
} catch (x) {
return;
}
throw new Error('Expected react-html not to exist in stable.');
}
});
it('should be able to render a simple component', async () => {
function Component() {
// We can't use JSX because that's client-JSX in our tests.
return React.createElement('div', null, 'hello world');
}
const html = await ReactHTML.renderToMarkup(
React.createElement(Component),
);
expect(html).toBe('<div>hello world</div>');
});
it('should be able to render a large string', async () => {
function Component() {
// We can't use JSX because that's client-JSX in our tests.
return React.createElement('div', null, 'hello '.repeat(200) + 'world');
}
const html = await ReactHTML.renderToMarkup(
React.createElement(Component),
);
expect(html).toBe('<div>' + ('hello '.repeat(200) + 'world') + '</div>');
});
it('should prefix html tags with a doctype', async () => {
const html = await ReactHTML.renderToMarkup(
// We can't use JSX because that's client-JSX in our tests.
React.createElement(
'html',
null,
React.createElement('body', null, 'hello'),
),
);
expect(html).toBe(
'<!DOCTYPE html><html><head></head><body>hello</body></html>',
);
});
it('should error on useState', async () => {
function Component() {
const [state] = React.useState('hello');
// We can't use JSX because that's client-JSX in our tests.
return React.createElement('div', null, state);
}
await expect(async () => {
await ReactHTML.renderToMarkup(React.createElement(Component));
}).rejects.toThrow();
});
it('should error on refs passed to host components', async () => {
function Component() {
const ref = React.createRef();
// We can't use JSX because that's client-JSX in our tests.
return React.createElement('div', {ref});
}
await expect(async () => {
await ReactHTML.renderToMarkup(React.createElement(Component));
}).rejects.toThrow();
});
it('should error on callbacks passed to event handlers', async () => {
function Component() {
function onClick() {
// This won't be able to be called.
}
// We can't use JSX because that's client-JSX in our tests.
return React.createElement('div', {onClick});
}
await expect(async () => {
await ReactHTML.renderToMarkup(React.createElement(Component));
}).rejects.toThrow();
});
it('supports the useId Hook', async () => {
function Component() {
const firstNameId = React.useId();
const lastNameId = React.useId();
// We can't use JSX because that's client-JSX in our tests.
return React.createElement(
'div',
null,
React.createElement(
'h2',
{
id: firstNameId,
},
'First',
),
React.createElement(
'p',
{
'aria-labelledby': firstNameId,
},
'Sebastian',
),
React.createElement(
'h2',
{
id: lastNameId,
},
'Last',
),
React.createElement(
'p',
{
'aria-labelledby': lastNameId,
},
'Smith',
),
);
}
const html = await ReactHTML.renderToMarkup(
React.createElement(Component),
);
const container = document.createElement('div');
container.innerHTML = html;
expect(container.getElementsByTagName('h2')[0].id).toBe(
container.getElementsByTagName('p')[0].getAttribute('aria-labelledby'),
);
expect(container.getElementsByTagName('h2')[1].id).toBe(
container.getElementsByTagName('p')[1].getAttribute('aria-labelledby'),
);
// It's not the same id between them.
expect(container.getElementsByTagName('h2')[0].id).not.toBe(
container.getElementsByTagName('p')[1].getAttribute('aria-labelledby'),
);
});
// @gate enableCache
it('supports cache', async () => {
let counter = 0;
const getCount = React.cache(() => {
return counter++;
});
function Component() {
const a = getCount();
const b = getCount();
return React.createElement('div', null, a, b);
}
const html = await ReactHTML.renderToMarkup(
React.createElement(Component),
);
expect(html).toBe('<div>00</div>');
});
});
}