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
This commit is contained in:
Tim Yung 2018-06-26 13:31:05 -07:00 committed by Facebook Github Bot
parent 8b6f1c37ce
commit 537a99539b
3 changed files with 181 additions and 1 deletions

View File

@ -134,7 +134,7 @@ const YellowBoxCategory = {
0,
);
if (lastOffset < content.length - 1) {
if (lastOffset < content.length) {
const lastPart = content.substr(lastOffset);
elements.push(<Text key="-1">{lastPart}</Text>);
}

View File

@ -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();
});
});

View File

@ -0,0 +1,95 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`YellowBoxCategory renders a single substitution 1`] = `
Array [
<Component
style={
Object {
"fontWeight": "bold",
}
}
>
"A"
</Component>,
]
`;
exports[`YellowBoxCategory renders content with no substitutions 1`] = `
Array [
<Component>
A
</Component>,
]
`;
exports[`YellowBoxCategory renders multiple substitutions 1`] = `
Array [
<Component
style={
Object {
"fontWeight": "bold",
}
}
>
"A"
</Component>,
<Component>
</Component>,
<Component
style={
Object {
"fontWeight": "bold",
}
}
>
"B"
</Component>,
<Component>
</Component>,
<Component
style={
Object {
"fontWeight": "bold",
}
}
>
"C"
</Component>,
]
`;
exports[`YellowBoxCategory renders substitutions with leading content 1`] = `
Array [
<Component>
!
</Component>,
<Component
style={
Object {
"fontWeight": "bold",
}
}
>
"A"
</Component>,
]
`;
exports[`YellowBoxCategory renders substitutions with trailing content 1`] = `
Array [
<Component
style={
Object {
"fontWeight": "bold",
}
}
>
"A"
</Component>,
<Component>
!
</Component>,
]
`;