Skip to content

Commit

Permalink
feat(deep): handle deeply set functions
Browse files Browse the repository at this point in the history
like `<div a={{b: function hello() {}}} />`

BREAKING CHANGE: functions are now stringified to `function noRefCheck()
{}` instead of `function () {code;}`. For various reasons AND to be
specific about the fact that we do not represent the function in a
realistic way.
  • Loading branch information
vvo committed Oct 16, 2015
1 parent 403e404 commit ad21917
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ Turn a ReactElement into the corresponding JSX string.
Useful for unit testing and any other need you may think of.

Features:
- supports nesting
- props: supports string, number, function (inlined as `prop={function () {code;}}`), object, ReactElement (inlined), regex..
- supports nesting and deep nesting like `<div a={{b: {c: {d: <div />}}}} />`
- props: supports string, number, function (inlined as `prop={function noRefCheck() {}}`), object, ReactElement (inlined), regex..
- order props alphabetically
- sort object keys in a deterministic order (`o={{a: 1, b:2}} === o={{b:2, a:1}}`)
- React's documentation indent style for JSX
Expand Down
11 changes: 9 additions & 2 deletions index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ describe(`reactElementToJSXString(ReactElement)`, () => {
it(`reactElementToJSXString(<div fn={() => {}}/>)`, () => {
expect(
reactElementToJSXString(<div fn={() => {}}/>)
).toEqual(`<div fn={function () {code;}} />`);
).toEqual(`<div fn={function noRefCheck() {}} />`);
});

it(`reactElementToJSXString(<div fn={function hello(){}}/>)`, () => {
expect(
reactElementToJSXString(<div fn={function hello() {}}/>)
).toEqual(`<div fn={function () {code;}} />`);
).toEqual(`<div fn={function noRefCheck() {}} />`);
});

it(`reactElementToJSXString(<div co={<div a="1" />} />)`, () => {
Expand Down Expand Up @@ -175,4 +175,11 @@ describe(`reactElementToJSXString(ReactElement)`, () => {
reactElementToJSXString(<div a={undefined} />)
);
});

it(`reactElementToJSXString(<div a={{b: function hello() {}}} />`, () => {
expect(
reactElementToJSXString(<div a={{b: function hello() {}}} />)
).toEqual(`<div a={{b: function noRefCheck() {}}} />`);
});

});
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function formatPropValue(propValue) {

function formatValue(value) {
if (typeof value === 'function') {
return `function () {code;}`;
return function noRefCheck() {};
} else if (isElement(value)) {
return toJSXString({ReactElement: value, inline: true});
} else if (value && Object.keys(value).length > 0) {
Expand Down

0 comments on commit ad21917

Please sign in to comment.