From 537a99539b4a0fd1f12f086451827615c4705126 Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Tue, 26 Jun 2018 13:31:05 -0700 Subject: [PATCH] YellowBox: Fix Off-By-1 Content Rendering Bug Summary: Fixes an off-by-one bug that was causing trailing single characters to be truncated from YellowBox. This snuck in during the revamp of YellowBox. Whoops! @public Reviewed By: TheSavior Differential Revision: D8607388 fbshipit-source-id: d0efac4dec361456ef58347a60eb1486a888624a --- Libraries/YellowBox/Data/YellowBoxCategory.js | 2 +- .../Data/__tests__/YellowBoxCategory-test.js | 85 +++++++++++++++++ .../YellowBoxCategory-test.js.snap | 95 +++++++++++++++++++ 3 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 Libraries/YellowBox/Data/__tests__/__snapshots__/YellowBoxCategory-test.js.snap diff --git a/Libraries/YellowBox/Data/YellowBoxCategory.js b/Libraries/YellowBox/Data/YellowBoxCategory.js index 5e22fa96bdf710..b0c5984ce950ff 100644 --- a/Libraries/YellowBox/Data/YellowBoxCategory.js +++ b/Libraries/YellowBox/Data/YellowBoxCategory.js @@ -134,7 +134,7 @@ const YellowBoxCategory = { 0, ); - if (lastOffset < content.length - 1) { + if (lastOffset < content.length) { const lastPart = content.substr(lastOffset); elements.push({lastPart}); } diff --git a/Libraries/YellowBox/Data/__tests__/YellowBoxCategory-test.js b/Libraries/YellowBox/Data/__tests__/YellowBoxCategory-test.js index 5c7ab61b409a0d..e835485ec1b566 100644 --- a/Libraries/YellowBox/Data/__tests__/YellowBoxCategory-test.js +++ b/Libraries/YellowBox/Data/__tests__/YellowBoxCategory-test.js @@ -97,4 +97,89 @@ describe('YellowBoxCategory', () => { }, }); }); + + it('renders content with no substitutions', () => { + expect( + YellowBoxCategory.render( + {content: 'A', substitutions: []}, + {fontWeight: 'bold'}, + ), + ).toMatchSnapshot(); + }); + + it('renders a single substitution', () => { + expect( + YellowBoxCategory.render( + { + content: '"A"', + substitutions: [ + { + length: 3, + offset: 0, + }, + ], + }, + {fontWeight: 'bold'}, + ), + ).toMatchSnapshot(); + }); + + it('renders multiple substitutions', () => { + expect( + YellowBoxCategory.render( + { + content: '"A" "B" "C"', + substitutions: [ + { + length: 3, + offset: 0, + }, + { + length: 3, + offset: 4, + }, + { + length: 3, + offset: 8, + }, + ], + }, + {fontWeight: 'bold'}, + ), + ).toMatchSnapshot(); + }); + + it('renders substitutions with leading content', () => { + expect( + YellowBoxCategory.render( + { + content: '!"A"', + substitutions: [ + { + length: 3, + offset: 1, + }, + ], + }, + {fontWeight: 'bold'}, + ), + ).toMatchSnapshot(); + }); + + it('renders substitutions with trailing content', () => { + expect( + YellowBoxCategory.render( + { + content: '"A"!', + substitutions: [ + { + length: 3, + offset: 0, + }, + ], + }, + {fontWeight: 'bold'}, + ), + ).toMatchSnapshot(); + }); }); diff --git a/Libraries/YellowBox/Data/__tests__/__snapshots__/YellowBoxCategory-test.js.snap b/Libraries/YellowBox/Data/__tests__/__snapshots__/YellowBoxCategory-test.js.snap new file mode 100644 index 00000000000000..ee88875c3717c9 --- /dev/null +++ b/Libraries/YellowBox/Data/__tests__/__snapshots__/YellowBoxCategory-test.js.snap @@ -0,0 +1,95 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`YellowBoxCategory renders a single substitution 1`] = ` +Array [ + + "A" + , +] +`; + +exports[`YellowBoxCategory renders content with no substitutions 1`] = ` +Array [ + + A + , +] +`; + +exports[`YellowBoxCategory renders multiple substitutions 1`] = ` +Array [ + + "A" + , + + + , + + "B" + , + + + , + + "C" + , +] +`; + +exports[`YellowBoxCategory renders substitutions with leading content 1`] = ` +Array [ + + ! + , + + "A" + , +] +`; + +exports[`YellowBoxCategory renders substitutions with trailing content 1`] = ` +Array [ + + "A" + , + + ! + , +] +`;