|
12 | 12 | let React;
|
13 | 13 | let ReactDOM;
|
14 | 14 | let ReactDOMServer;
|
| 15 | +let ReactFeatureFlags; |
15 | 16 | let Scheduler;
|
16 | 17 | let PropTypes;
|
17 | 18 |
|
@@ -893,7 +894,17 @@ describe('context legacy', () => {
|
893 | 894 | ReactDOM.render(<Root />, container);
|
894 | 895 | });
|
895 | 896 |
|
896 |
| - describe('logging', () => { |
| 897 | + describe('enable console logs logging', () => { |
| 898 | + beforeEach(() => { |
| 899 | + jest.resetModules(); |
| 900 | + |
| 901 | + ReactFeatureFlags = require('shared/ReactFeatureFlags'); |
| 902 | + React = require('react'); |
| 903 | + ReactDOM = require('react-dom'); |
| 904 | + |
| 905 | + ReactFeatureFlags.enableConsoleLogsInDoubleRender = true; |
| 906 | + }); |
| 907 | + |
897 | 908 | it('does not disable logs for class double render', () => {
|
898 | 909 | spyOnDevAndProd(console, 'log');
|
899 | 910 |
|
@@ -1084,4 +1095,206 @@ describe('context legacy', () => {
|
1084 | 1095 | expect(console.log).toBeCalledWith('foo 1');
|
1085 | 1096 | });
|
1086 | 1097 | });
|
| 1098 | + |
| 1099 | + describe('disable console logs logging', () => { |
| 1100 | + beforeEach(() => { |
| 1101 | + jest.resetModules(); |
| 1102 | + |
| 1103 | + ReactFeatureFlags = require('shared/ReactFeatureFlags'); |
| 1104 | + React = require('react'); |
| 1105 | + ReactDOM = require('react-dom'); |
| 1106 | + |
| 1107 | + ReactFeatureFlags.enableConsoleLogsInDoubleRender = false; |
| 1108 | + }); |
| 1109 | + |
| 1110 | + it('disable logs for class double render', () => { |
| 1111 | + spyOnDevAndProd(console, 'log'); |
| 1112 | + |
| 1113 | + let count = 0; |
| 1114 | + class Foo extends React.Component { |
| 1115 | + render() { |
| 1116 | + count++; |
| 1117 | + console.log('foo ' + count); |
| 1118 | + return null; |
| 1119 | + } |
| 1120 | + } |
| 1121 | + |
| 1122 | + const container = document.createElement('div'); |
| 1123 | + ReactDOM.render( |
| 1124 | + <React.StrictMode> |
| 1125 | + <Foo /> |
| 1126 | + </React.StrictMode>, |
| 1127 | + container, |
| 1128 | + ); |
| 1129 | + |
| 1130 | + expect(count).toBe(__DEV__ ? 2 : 1); |
| 1131 | + expect(console.log).toBeCalledTimes(1); |
| 1132 | + // Note: we should display the first log because otherwise |
| 1133 | + // there is a risk of suppressing warnings when they happen, |
| 1134 | + // and on the next render they'd get deduplicated and ignored. |
| 1135 | + expect(console.log).toBeCalledWith('foo 1'); |
| 1136 | + }); |
| 1137 | + |
| 1138 | + it('disables logs for class double ctor', () => { |
| 1139 | + spyOnDevAndProd(console, 'log'); |
| 1140 | + |
| 1141 | + let count = 0; |
| 1142 | + class Foo extends React.Component { |
| 1143 | + constructor(props) { |
| 1144 | + super(props); |
| 1145 | + count++; |
| 1146 | + console.log('foo ' + count); |
| 1147 | + } |
| 1148 | + render() { |
| 1149 | + return null; |
| 1150 | + } |
| 1151 | + } |
| 1152 | + |
| 1153 | + const container = document.createElement('div'); |
| 1154 | + ReactDOM.render( |
| 1155 | + <React.StrictMode> |
| 1156 | + <Foo /> |
| 1157 | + </React.StrictMode>, |
| 1158 | + container, |
| 1159 | + ); |
| 1160 | + |
| 1161 | + expect(count).toBe(__DEV__ ? 2 : 1); |
| 1162 | + expect(console.log).toBeCalledTimes(1); |
| 1163 | + // Note: we should display the first log because otherwise |
| 1164 | + // there is a risk of suppressing warnings when they happen, |
| 1165 | + // and on the next render they'd get deduplicated and ignored. |
| 1166 | + expect(console.log).toBeCalledWith('foo 1'); |
| 1167 | + }); |
| 1168 | + |
| 1169 | + it('disable logs for class double getDerivedStateFromProps', () => { |
| 1170 | + spyOnDevAndProd(console, 'log'); |
| 1171 | + |
| 1172 | + let count = 0; |
| 1173 | + class Foo extends React.Component { |
| 1174 | + state = {}; |
| 1175 | + static getDerivedStateFromProps() { |
| 1176 | + count++; |
| 1177 | + console.log('foo ' + count); |
| 1178 | + return {}; |
| 1179 | + } |
| 1180 | + render() { |
| 1181 | + return null; |
| 1182 | + } |
| 1183 | + } |
| 1184 | + |
| 1185 | + const container = document.createElement('div'); |
| 1186 | + ReactDOM.render( |
| 1187 | + <React.StrictMode> |
| 1188 | + <Foo /> |
| 1189 | + </React.StrictMode>, |
| 1190 | + container, |
| 1191 | + ); |
| 1192 | + |
| 1193 | + expect(count).toBe(__DEV__ ? 2 : 1); |
| 1194 | + expect(console.log).toBeCalledTimes(1); |
| 1195 | + // Note: we should display the first log because otherwise |
| 1196 | + // there is a risk of suppressing warnings when they happen, |
| 1197 | + // and on the next render they'd get deduplicated and ignored. |
| 1198 | + expect(console.log).toBeCalledWith('foo 1'); |
| 1199 | + }); |
| 1200 | + |
| 1201 | + it('disable logs for class double shouldComponentUpdate', () => { |
| 1202 | + spyOnDevAndProd(console, 'log'); |
| 1203 | + |
| 1204 | + let count = 0; |
| 1205 | + class Foo extends React.Component { |
| 1206 | + state = {}; |
| 1207 | + shouldComponentUpdate() { |
| 1208 | + count++; |
| 1209 | + console.log('foo ' + count); |
| 1210 | + return {}; |
| 1211 | + } |
| 1212 | + render() { |
| 1213 | + return null; |
| 1214 | + } |
| 1215 | + } |
| 1216 | + |
| 1217 | + const container = document.createElement('div'); |
| 1218 | + ReactDOM.render( |
| 1219 | + <React.StrictMode> |
| 1220 | + <Foo /> |
| 1221 | + </React.StrictMode>, |
| 1222 | + container, |
| 1223 | + ); |
| 1224 | + // Trigger sCU: |
| 1225 | + ReactDOM.render( |
| 1226 | + <React.StrictMode> |
| 1227 | + <Foo /> |
| 1228 | + </React.StrictMode>, |
| 1229 | + container, |
| 1230 | + ); |
| 1231 | + |
| 1232 | + expect(count).toBe(__DEV__ ? 2 : 1); |
| 1233 | + expect(console.log).toBeCalledTimes(1); |
| 1234 | + // Note: we should display the first log because otherwise |
| 1235 | + // there is a risk of suppressing warnings when they happen, |
| 1236 | + // and on the next render they'd get deduplicated and ignored. |
| 1237 | + expect(console.log).toBeCalledWith('foo 1'); |
| 1238 | + }); |
| 1239 | + |
| 1240 | + it('disable logs for class state updaters', () => { |
| 1241 | + spyOnDevAndProd(console, 'log'); |
| 1242 | + |
| 1243 | + let inst; |
| 1244 | + let count = 0; |
| 1245 | + class Foo extends React.Component { |
| 1246 | + state = {}; |
| 1247 | + render() { |
| 1248 | + inst = this; |
| 1249 | + return null; |
| 1250 | + } |
| 1251 | + } |
| 1252 | + |
| 1253 | + const container = document.createElement('div'); |
| 1254 | + ReactDOM.render( |
| 1255 | + <React.StrictMode> |
| 1256 | + <Foo /> |
| 1257 | + </React.StrictMode>, |
| 1258 | + container, |
| 1259 | + ); |
| 1260 | + inst.setState(() => { |
| 1261 | + count++; |
| 1262 | + console.log('foo ' + count); |
| 1263 | + return {}; |
| 1264 | + }); |
| 1265 | + |
| 1266 | + expect(count).toBe(__DEV__ ? 2 : 1); |
| 1267 | + expect(console.log).toBeCalledTimes(1); |
| 1268 | + // Note: we should display the first log because otherwise |
| 1269 | + // there is a risk of suppressing warnings when they happen, |
| 1270 | + // and on the next render they'd get deduplicated and ignored. |
| 1271 | + expect(console.log).toBeCalledWith('foo 1'); |
| 1272 | + }); |
| 1273 | + |
| 1274 | + it('disable logs for function double render', () => { |
| 1275 | + spyOnDevAndProd(console, 'log'); |
| 1276 | + |
| 1277 | + let count = 0; |
| 1278 | + function Foo() { |
| 1279 | + count++; |
| 1280 | + console.log('foo ' + count); |
| 1281 | + return null; |
| 1282 | + } |
| 1283 | + |
| 1284 | + const container = document.createElement('div'); |
| 1285 | + ReactDOM.render( |
| 1286 | + <React.StrictMode> |
| 1287 | + <Foo /> |
| 1288 | + </React.StrictMode>, |
| 1289 | + container, |
| 1290 | + ); |
| 1291 | + |
| 1292 | + expect(count).toBe(__DEV__ ? 2 : 1); |
| 1293 | + expect(console.log).toBeCalledTimes(1); |
| 1294 | + // Note: we should display the first log because otherwise |
| 1295 | + // there is a risk of suppressing warnings when they happen, |
| 1296 | + // and on the next render they'd get deduplicated and ignored. |
| 1297 | + expect(console.log).toBeCalledWith('foo 1'); |
| 1298 | + }); |
| 1299 | + }); |
1087 | 1300 | });
|
0 commit comments