GithubHelp home page GithubHelp logo

Comments (4)

mdjastrzebski avatar mdjastrzebski commented on December 8, 2023 1

@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: [] for null, undefined, false and true
  • rendered: ["0"], etc for 0, 1, "Hello"

from react.

mdjastrzebski avatar mdjastrzebski commented on December 8, 2023 1

@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.

AndyOGo avatar AndyOGo commented on December 8, 2023

@mdjastrzebski thanks for you quick response.

I came to the same conclusion.

from react.

Yashshiva75 avatar Yashshiva75 commented on December 8, 2023

HELLO IAM NOW IN DISSCUSSION

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.