From cd28cbd84bf71f17ac84c3dd4370d46bac0aa3fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Mon, 27 Sep 2021 09:49:12 +0200 Subject: [PATCH 1/2] fix: fixed crashing on circular React elements --- src/formatter/sortObject.js | 9 +++++++-- src/index.spec.js | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/formatter/sortObject.js b/src/formatter/sortObject.js index 85655415c..2415cba56 100644 --- a/src/formatter/sortObject.js +++ b/src/formatter/sortObject.js @@ -1,4 +1,5 @@ /* @flow */ +import * as React from 'react'; export default function sortObject(value: any): any { // return non-object value as is @@ -6,8 +7,12 @@ export default function sortObject(value: any): any { return value; } - // return date and regexp values as is - if (value instanceof Date || value instanceof RegExp) { + // return date, regexp and react element values as is + if ( + value instanceof Date || + value instanceof RegExp || + React.isValidElement(value) + ) { return value; } diff --git a/src/index.spec.js b/src/index.spec.js index de66c1e53..c6b6a1ea3 100644 --- a/src/index.spec.js +++ b/src/index.spec.js @@ -1334,4 +1334,21 @@ describe('reactElementToJSXString(ReactElement)', () => { expect(reactElementToJSXString()).toEqual(``); }); + + it('should not crash when stringifying circular `forwardRef` element', () => { + function TagList({ tags }) { + return tags; + } + + const Tag = React.forwardRef(function Tag({ text }, ref) { + return {text}; + }); + Tag.emotionReal = Tag; + + expect(() => + reactElementToJSXString( + ]} /> + ) + ).not.toThrow(); + }); }); From 257294197a66a171aabbaa2429da7dbcd05b7a8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Tue, 28 Sep 2021 20:55:04 +0200 Subject: [PATCH 2/2] chore: adjust a test implementation to be more meaningful --- src/index.spec.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/index.spec.js b/src/index.spec.js index c6b6a1ea3..ce1d400c3 100644 --- a/src/index.spec.js +++ b/src/index.spec.js @@ -1335,7 +1335,7 @@ describe('reactElementToJSXString(ReactElement)', () => { expect(reactElementToJSXString()).toEqual(``); }); - it('should not crash when stringifying circular `forwardRef` element', () => { + it('should stringify `forwardRef` element with a circular property', () => { function TagList({ tags }) { return tags; } @@ -1345,10 +1345,14 @@ describe('reactElementToJSXString(ReactElement)', () => { }); Tag.emotionReal = Tag; - expect(() => + expect( reactElementToJSXString( ]} /> ) - ).not.toThrow(); + ).toEqual(` + ]} + />`); }); });