Comments (4)
@AndyOGo I've done some tests to verify return types for toJSON()
and toTree
here are the results:
toJSON()
import * as React from 'react';
import TestRenderer from 'react-test-renderer';
describe('toJSON()', () => {
test('undefined primitive', () => {
const renderer = TestRenderer.create(<div>{undefined}</div>);
expect(JSON.stringify(renderer.toJSON())).toMatchInlineSnapshot(
`"{"type":"div","props":{},"children":null}"`
);
});
test('null primitive', () => {
const renderer = TestRenderer.create(<div>{null}</div>);
expect(JSON.stringify(renderer.toJSON())).toMatchInlineSnapshot(
`"{"type":"div","props":{},"children":null}"`
);
});
test('false primitive', () => {
const renderer = TestRenderer.create(<div>{false}</div>);
expect(JSON.stringify(renderer.toJSON())).toMatchInlineSnapshot(
`"{"type":"div","props":{},"children":null}"`
);
});
test('true primitive', () => {
const renderer = TestRenderer.create(<div>{true}</div>);
expect(JSON.stringify(renderer.toJSON())).toMatchInlineSnapshot(
`"{"type":"div","props":{},"children":null}"`
);
});
test('0 primitive', () => {
const renderer = TestRenderer.create(<div>{0}</div>);
expect(JSON.stringify(renderer.toJSON())).toMatchInlineSnapshot(
`"{"type":"div","props":{},"children":["0"]}"`
);
});
test('1 primitive', () => {
const renderer = TestRenderer.create(<div>{1}</div>);
expect(JSON.stringify(renderer.toJSON())).toMatchInlineSnapshot(
`"{"type":"div","props":{},"children":["1"]}"`
);
});
test('string primitive', () => {
const renderer = TestRenderer.create(<div>Hello</div>);
expect(JSON.stringify(renderer.toJSON())).toMatchInlineSnapshot(
`"{"type":"div","props":{},"children":["Hello"]}"`
);
});
});
As you can see: null
, undefined
, true
, false
result in "children":null
, so all these values are dropped.
Whereas 0
, 1
, and "Hello"
result in "children":["0"]
, etc, so string
and number
values are turned into strings.
toTree()
describe('toTree()', () => {
test('undefined primitive', () => {
const renderer = TestRenderer.create(<div>{undefined}</div>);
expect(JSON.stringify(renderer.toTree())).toMatchInlineSnapshot(
`"{"nodeType":"host","type":"div","props":{},"instance":null,"rendered":[]}"`
);
});
test('null primitive', () => {
const renderer = TestRenderer.create(<div>{null}</div>);
expect(JSON.stringify(renderer.toTree())).toMatchInlineSnapshot(
`"{"nodeType":"host","type":"div","props":{"children":null},"instance":null,"rendered":[]}"`
);
});
test('false primitive', () => {
const renderer = TestRenderer.create(<div>{false}</div>);
expect(JSON.stringify(renderer.toTree())).toMatchInlineSnapshot(
`"{"nodeType":"host","type":"div","props":{"children":false},"instance":null,"rendered":[]}"`
);
});
test('true primitive', () => {
const renderer = TestRenderer.create(<div>{true}</div>);
expect(JSON.stringify(renderer.toTree())).toMatchInlineSnapshot(
`"{"nodeType":"host","type":"div","props":{"children":true},"instance":null,"rendered":[]}"`
);
});
test('0 primitive', () => {
const renderer = TestRenderer.create(<div>{0}</div>);
expect(JSON.stringify(renderer.toTree())).toMatchInlineSnapshot(
`"{"nodeType":"host","type":"div","props":{"children":0},"instance":null,"rendered":["0"]}"`
);
});
test('1 primitive', () => {
const renderer = TestRenderer.create(<div>{1}</div>);
expect(JSON.stringify(renderer.toTree())).toMatchInlineSnapshot(
`"{"nodeType":"host","type":"div","props":{"children":1},"instance":null,"rendered":["1"]}"`
);
});
test('string primitive', () => {
const renderer = TestRenderer.create(<div>Hello</div>);
expect(JSON.stringify(renderer.toTree())).toMatchInlineSnapshot(
`"{"nodeType":"host","type":"div","props":{"children":"Hello"},"instance":null,"rendered":["Hello"]}"`
);
});
});
In this case children
props repeats the passed value, and rendered
prop:
rendered: []
fornull
,undefined
,false
andtrue
rendered: ["0"]
, etc for0
,1
,"Hello"
from react.
@AndyOGo Flowtype does not seem to specify the result of return from toTree()
, it's marked as $FlowFixMe
. The types ReactTestRendererJSON
& ReactTestRendererNode
are for toJSON
method.
from react.
@mdjastrzebski thanks for you quick response.
I came to the same conclusion.
from react.
HELLO IAM NOW IN DISSCUSSION
from react.
Related Issues (20)
- UseEffect only works on localhost but not in production HOT 3
- [DevTools Bug]: React Dev Tools break gstreamer docs HOT 2
- Bug: hoisted stylesheets should not reorder when re-rendered in a transition
- Bug: exhaustive-deps lint should allow omitting unchanging values HOT 1
- Ability to turn off ‘Download DevTools’ console message HOT 3
- The left Sidebar is not scrolling on small Displays HOT 1
- Bug: React 18 Suspense with unstable value would trigger multiple rerenders
- Bug: flushSync on component having useLayoutEffect in some cases causes Maximum update depth exceeded HOT 1
- Bug: Stale values returned from useOptimistic when state changes
- Bug: Re-rendering nested components breaks event listeners on parent HOT 3
- Bug: contentEditable attribute does not allow the 'plaintext-only' value HOT 3
- Bug: manipulating controlled input value breaks native validation HOT 4
- Sponsored issue: fix an issue
- Bug: Re-rendering nested components breaks event listeners on parent HOT 1
- Bug: Nested suspense boundaries `Cannot read properties of null (reading 'parentNode')` on resolution
- Bug: pending attribute from useFormStatus is stuck as "true" when form action takes longer than ~5s to resolve HOT 3
- React developer tools messes with Smartermail HOT 1
- Bug: Unable to build react repo locally in Mac HOT 2
- Bug: Select when passed a value as Prop errors with a suggestion to pass readOnly HOT 14
- Bug: Seems like new hydrateRoot method doesn't provide any promise or callback to catch hydration process finish
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react.