Skip to content

Commit 694cb75

Browse files
eps1longaearon
authored andcommitted
Convert CSSProperty to createRoot (#28181)
1 parent ff1ac47 commit 694cb75

File tree

1 file changed

+75
-38
lines changed

1 file changed

+75
-38
lines changed

packages/react-dom/src/__tests__/CSSPropertyOperations-test.js

+75-38
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
'use strict';
1111

1212
const React = require('react');
13-
const ReactDOM = require('react-dom');
13+
const ReactDOMClient = require('react-dom/client');
1414
const ReactDOMServer = require('react-dom/server');
15+
const act = require('internal-test-utils').act;
1516

1617
describe('CSSPropertyOperations', () => {
1718
it('should automatically append `px` to relevant styles', () => {
@@ -66,15 +67,19 @@ describe('CSSPropertyOperations', () => {
6667
expect(html).toContain('"--someColor:#000000"');
6768
});
6869

69-
it('should set style attribute when styles exist', () => {
70+
it('should set style attribute when styles exist', async () => {
7071
const styles = {
7172
backgroundColor: '#000',
7273
display: 'none',
7374
};
74-
let div = <div style={styles} />;
75-
const root = document.createElement('div');
76-
div = ReactDOM.render(div, root);
77-
expect(/style=".*"/.test(root.innerHTML)).toBe(true);
75+
const container = document.createElement('div');
76+
const root = ReactDOMClient.createRoot(container);
77+
await act(() => {
78+
root.render(<div style={styles} />);
79+
});
80+
81+
const div = container.firstChild;
82+
expect(/style=".*"/.test(container.innerHTML)).toBe(true);
7883
});
7984

8085
it('should not set style attribute when no styles exist', () => {
@@ -87,7 +92,7 @@ describe('CSSPropertyOperations', () => {
8792
expect(/style=/.test(html)).toBe(false);
8893
});
8994

90-
it('should warn when using hyphenated style names', () => {
95+
it('should warn when using hyphenated style names', async () => {
9196
class Comp extends React.Component {
9297
static displayName = 'Comp';
9398

@@ -96,16 +101,20 @@ describe('CSSPropertyOperations', () => {
96101
}
97102
}
98103

99-
const root = document.createElement('div');
100-
101-
expect(() => ReactDOM.render(<Comp />, root)).toErrorDev(
104+
const container = document.createElement('div');
105+
const root = ReactDOMClient.createRoot(container);
106+
await expect(async () => {
107+
await act(() => {
108+
root.render(<Comp />);
109+
});
110+
}).toErrorDev(
102111
'Warning: Unsupported style property background-color. Did you mean backgroundColor?' +
103112
'\n in div (at **)' +
104113
'\n in Comp (at **)',
105114
);
106115
});
107116

108-
it('should warn when updating hyphenated style names', () => {
117+
it('should warn when updating hyphenated style names', async () => {
109118
class Comp extends React.Component {
110119
static displayName = 'Comp';
111120

@@ -118,10 +127,16 @@ describe('CSSPropertyOperations', () => {
118127
'-ms-transform': 'translate3d(0, 0, 0)',
119128
'-webkit-transform': 'translate3d(0, 0, 0)',
120129
};
121-
const root = document.createElement('div');
122-
ReactDOM.render(<Comp />, root);
123-
124-
expect(() => ReactDOM.render(<Comp style={styles} />, root)).toErrorDev([
130+
const container = document.createElement('div');
131+
const root = ReactDOMClient.createRoot(container);
132+
await act(() => {
133+
root.render(<Comp />);
134+
});
135+
await expect(async () => {
136+
await act(() => {
137+
root.render(<Comp style={styles} />);
138+
});
139+
}).toErrorDev([
125140
'Warning: Unsupported style property -ms-transform. Did you mean msTransform?' +
126141
'\n in div (at **)' +
127142
'\n in Comp (at **)',
@@ -131,7 +146,7 @@ describe('CSSPropertyOperations', () => {
131146
]);
132147
});
133148

134-
it('warns when miscapitalizing vendored style names', () => {
149+
it('warns when miscapitalizing vendored style names', async () => {
135150
class Comp extends React.Component {
136151
static displayName = 'Comp';
137152

@@ -148,9 +163,13 @@ describe('CSSPropertyOperations', () => {
148163
}
149164
}
150165

151-
const root = document.createElement('div');
152-
153-
expect(() => ReactDOM.render(<Comp />, root)).toErrorDev([
166+
const container = document.createElement('div');
167+
const root = ReactDOMClient.createRoot(container);
168+
await expect(async () => {
169+
await act(() => {
170+
root.render(<Comp />);
171+
});
172+
}).toErrorDev([
154173
// msTransform is correct already and shouldn't warn
155174
'Warning: Unsupported vendor-prefixed style property oTransform. ' +
156175
'Did you mean OTransform?' +
@@ -163,7 +182,7 @@ describe('CSSPropertyOperations', () => {
163182
]);
164183
});
165184

166-
it('should warn about style having a trailing semicolon', () => {
185+
it('should warn about style having a trailing semicolon', async () => {
167186
class Comp extends React.Component {
168187
static displayName = 'Comp';
169188

@@ -181,9 +200,13 @@ describe('CSSPropertyOperations', () => {
181200
}
182201
}
183202

184-
const root = document.createElement('div');
185-
186-
expect(() => ReactDOM.render(<Comp />, root)).toErrorDev([
203+
const container = document.createElement('div');
204+
const root = ReactDOMClient.createRoot(container);
205+
await expect(async () => {
206+
await act(() => {
207+
root.render(<Comp />);
208+
});
209+
}).toErrorDev([
187210
"Warning: Style property values shouldn't contain a semicolon. " +
188211
'Try "backgroundColor: blue" instead.' +
189212
'\n in div (at **)' +
@@ -195,7 +218,7 @@ describe('CSSPropertyOperations', () => {
195218
]);
196219
});
197220

198-
it('should warn about style containing a NaN value', () => {
221+
it('should warn about style containing a NaN value', async () => {
199222
class Comp extends React.Component {
200223
static displayName = 'Comp';
201224

@@ -204,27 +227,34 @@ describe('CSSPropertyOperations', () => {
204227
}
205228
}
206229

207-
const root = document.createElement('div');
208-
209-
expect(() => ReactDOM.render(<Comp />, root)).toErrorDev(
230+
const container = document.createElement('div');
231+
const root = ReactDOMClient.createRoot(container);
232+
await expect(async () => {
233+
await act(() => {
234+
root.render(<Comp />);
235+
});
236+
}).toErrorDev(
210237
'Warning: `NaN` is an invalid value for the `fontSize` css style property.' +
211238
'\n in div (at **)' +
212239
'\n in Comp (at **)',
213240
);
214241
});
215242

216-
it('should not warn when setting CSS custom properties', () => {
243+
it('should not warn when setting CSS custom properties', async () => {
217244
class Comp extends React.Component {
218245
render() {
219246
return <div style={{'--foo-primary': 'red', backgroundColor: 'red'}} />;
220247
}
221248
}
222249

223-
const root = document.createElement('div');
224-
ReactDOM.render(<Comp />, root);
250+
const container = document.createElement('div');
251+
const root = ReactDOMClient.createRoot(container);
252+
await act(() => {
253+
root.render(<Comp />);
254+
});
225255
});
226256

227-
it('should warn about style containing an Infinity value', () => {
257+
it('should warn about style containing an Infinity value', async () => {
228258
class Comp extends React.Component {
229259
static displayName = 'Comp';
230260

@@ -233,25 +263,32 @@ describe('CSSPropertyOperations', () => {
233263
}
234264
}
235265

236-
const root = document.createElement('div');
237-
238-
expect(() => ReactDOM.render(<Comp />, root)).toErrorDev(
266+
const container = document.createElement('div');
267+
const root = ReactDOMClient.createRoot(container);
268+
await expect(async () => {
269+
await act(() => {
270+
root.render(<Comp />);
271+
});
272+
}).toErrorDev(
239273
'Warning: `Infinity` is an invalid value for the `fontSize` css style property.' +
240274
'\n in div (at **)' +
241275
'\n in Comp (at **)',
242276
);
243277
});
244278

245-
it('should not add units to CSS custom properties', () => {
279+
it('should not add units to CSS custom properties', async () => {
246280
class Comp extends React.Component {
247281
render() {
248282
return <div style={{'--foo': '5'}} />;
249283
}
250284
}
251285

252-
const root = document.createElement('div');
253-
ReactDOM.render(<Comp />, root);
286+
const container = document.createElement('div');
287+
const root = ReactDOMClient.createRoot(container);
288+
await act(() => {
289+
root.render(<Comp />);
290+
});
254291

255-
expect(root.children[0].style.getPropertyValue('--foo')).toEqual('5');
292+
expect(container.children[0].style.getPropertyValue('--foo')).toEqual('5');
256293
});
257294
});

0 commit comments

Comments
 (0)