mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Basically make `console.error` and `console.warn` behave like normal - when a component stack isn't appended. I need this because I need to be able to print rich logs with the component stack option and to be able to disable instrumentation completely in `console.createTask` environments that don't need it. Currently we can't print logs with richer objects because they're toString:ed first. In practice, pretty much all arguments we log are already toString:ed so it's not necessary anyway. Some might be like a number. So it would only be a problem if some environment can't handle proper consoles but then it's up to that environment to toString it before logging. The `Warning: ` prefix is historic and is both noisy and confusing. It's mostly unnecessary since the UI surrounding `console.error` and `console.warn` tend to have visual treatment around it anyway. However, it's actively misleading when `console.error` gets prefixed with a Warning that we consider an error level. There's an argument to be made that some of our `console.error` don't make the bar for an error but then the argument is to downgrade each of those to `console.warn` - not to brand all our actual error logging with `Warning: `. Apparently something needs to change in React Native before landing this because it depends on the prefix somehow which probably doesn't make sense already.
131 lines
3.6 KiB
JavaScript
131 lines
3.6 KiB
JavaScript
/**
|
|
* 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';
|
|
|
|
const ReactDOMServerIntegrationUtils = require('./utils/ReactDOMServerIntegrationTestUtils');
|
|
|
|
let React;
|
|
let ReactDOMClient;
|
|
let ReactDOMServer;
|
|
|
|
function initModules() {
|
|
// Reset warning cache.
|
|
jest.resetModules();
|
|
React = require('react');
|
|
ReactDOMClient = require('react-dom/client');
|
|
ReactDOMServer = require('react-dom/server');
|
|
|
|
// Make them available to the helpers.
|
|
return {
|
|
ReactDOMClient,
|
|
ReactDOMServer,
|
|
};
|
|
}
|
|
|
|
const {
|
|
resetModules,
|
|
asyncReactDOMRender,
|
|
clientRenderOnServerString,
|
|
expectMarkupMatch,
|
|
} = ReactDOMServerIntegrationUtils(initModules);
|
|
|
|
describe('ReactDOMServerIntegration', () => {
|
|
beforeEach(() => {
|
|
resetModules();
|
|
});
|
|
|
|
describe('refs', function () {
|
|
it('should not run ref code on server', async () => {
|
|
let refCount = 0;
|
|
class RefsComponent extends React.Component {
|
|
render() {
|
|
return <div ref={e => refCount++} />;
|
|
}
|
|
}
|
|
await expectMarkupMatch(<RefsComponent />, <div />);
|
|
expect(refCount).toBe(0);
|
|
});
|
|
|
|
it('should run ref code on client', async () => {
|
|
let refCount = 0;
|
|
class RefsComponent extends React.Component {
|
|
render() {
|
|
return <div ref={e => refCount++} />;
|
|
}
|
|
}
|
|
await expectMarkupMatch(<div />, <RefsComponent />);
|
|
expect(refCount).toBe(1);
|
|
});
|
|
|
|
it('should send the correct element to ref functions on client', async () => {
|
|
let refElement = null;
|
|
class RefsComponent extends React.Component {
|
|
render() {
|
|
return <div ref={e => (refElement = e)} />;
|
|
}
|
|
}
|
|
const e = await clientRenderOnServerString(<RefsComponent />);
|
|
expect(refElement).not.toBe(null);
|
|
expect(refElement).toBe(e);
|
|
});
|
|
|
|
// @gate !disableStringRefs
|
|
it('should have string refs on client when rendered over server markup', async () => {
|
|
class RefsComponent extends React.Component {
|
|
render() {
|
|
return <div ref="myDiv" />;
|
|
}
|
|
}
|
|
|
|
const markup = ReactDOMServer.renderToString(<RefsComponent />);
|
|
const root = document.createElement('div');
|
|
root.innerHTML = markup;
|
|
let component = null;
|
|
resetModules();
|
|
await expect(async () => {
|
|
await asyncReactDOMRender(
|
|
<RefsComponent ref={e => (component = e)} />,
|
|
root,
|
|
true,
|
|
);
|
|
}).toErrorDev([
|
|
'Component "RefsComponent" contains the string ref "myDiv". ' +
|
|
'Support for string refs will be removed in a future major release. ' +
|
|
'We recommend using useRef() or createRef() instead. ' +
|
|
'Learn more about using refs safely here: https://react.dev/link/strict-mode-string-ref\n' +
|
|
' in div (at **)\n' +
|
|
' in RefsComponent (at **)',
|
|
]);
|
|
expect(component.refs.myDiv).toBe(root.firstChild);
|
|
});
|
|
});
|
|
|
|
it('should forward refs', async () => {
|
|
const divRef = React.createRef();
|
|
|
|
class InnerComponent extends React.Component {
|
|
render() {
|
|
return <div ref={this.props.forwardedRef}>{this.props.value}</div>;
|
|
}
|
|
}
|
|
|
|
const OuterComponent = React.forwardRef((props, ref) => (
|
|
<InnerComponent {...props} forwardedRef={ref} />
|
|
));
|
|
|
|
await clientRenderOnServerString(
|
|
<OuterComponent ref={divRef} value="hello" />,
|
|
);
|
|
|
|
expect(divRef.current).not.toBe(null);
|
|
expect(divRef.current.textContent).toBe('hello');
|
|
});
|
|
});
|