Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not render primitives as json #1366

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions frontend/src/components/DetailsTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,39 @@ describe('DetailsTable', () => {
expect(tree).toMatchSnapshot();
});

it('does render arrays as JSON', () => {
const tree = shallow(<DetailsTable fields={[['key', '[]']]} />);
expect(tree).toMatchSnapshot();
});

it('does render arrays as JSON', () => {
const tree = shallow(<DetailsTable fields={[['key', '{}']]} />);
expect(tree).toMatchSnapshot();
});

it('does not render nulls as JSON', () => {
const tree = shallow(<DetailsTable fields={[['key', 'null']]} />);
expect(tree).toMatchSnapshot();
});

it('does not render numbers as JSON', () => {
const tree = shallow(<DetailsTable fields={[['key', '10']]} />);
expect(tree).toMatchSnapshot();
});

it('does not render strings as JSON', () => {
const tree = shallow(<DetailsTable fields={[['key', '"some string"']]} />);
expect(tree).toMatchSnapshot();
});

it('does not render booleans as JSON', () => {
const tree = shallow(<DetailsTable fields={[
['key1', 'true'],
['key2', 'false']
]} />);
expect(tree).toMatchSnapshot();
});

it('shows keys and values for multiple rows', () => {
const tree = shallow(<DetailsTable fields={[
['key1', 'value1'],
Expand Down
9 changes: 7 additions & 2 deletions frontend/src/components/DetailsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ export default (props: DetailsTableProps) => {
{!!props.title && <div className={commonCss.header}>{props.title}</div>}
<div>
{props.fields.map((f, i) => {
try{
try {
const parsedJson = JSON.parse(f[1]);
// Nulls, booleans, strings, and numbers can all be parsed as JSON, but we don't care
// about rendering those using CodeMirror. Note that `typeOf null` returns 'object'
if (parsedJson === null || typeof parsedJson !== 'object') {
throw new Error('Parsed JSON was neither an array nor an object. Using default renderer');
}
return (
<div key={i} className={css.row}>
<span className={css.key}>{f[0]}</span>
Expand All @@ -74,7 +79,7 @@ export default (props: DetailsTableProps) => {
</div>
);
} catch (err) {
// If the value isn't JSON, just display it as is
// If the value isn't a JSON object, just display it as is
return (
<div key={i} className={css.row}>
<span className={css.key}>{f[0]}</span>
Expand Down
169 changes: 169 additions & 0 deletions frontend/src/components/__snapshots__/DetailsTable.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,174 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`DetailsTable does not render booleans as JSON 1`] = `
<Fragment>
<div>
<div
className="row"
key="0"
>
<span
className="key"
>
key1
</span>
<span
className="valueText"
>
true
</span>
</div>
<div
className="row"
key="1"
>
<span
className="key"
>
key2
</span>
<span
className="valueText"
>
false
</span>
</div>
</div>
</Fragment>
`;

exports[`DetailsTable does not render nulls as JSON 1`] = `
<Fragment>
<div>
<div
className="row"
key="0"
>
<span
className="key"
>
key
</span>
<span
className="valueText"
>
null
</span>
</div>
</div>
</Fragment>
`;

exports[`DetailsTable does not render numbers as JSON 1`] = `
<Fragment>
<div>
<div
className="row"
key="0"
>
<span
className="key"
>
key
</span>
<span
className="valueText"
>
10
</span>
</div>
</div>
</Fragment>
`;

exports[`DetailsTable does not render strings as JSON 1`] = `
<Fragment>
<div>
<div
className="row"
key="0"
>
<span
className="key"
>
key
</span>
<span
className="valueText"
>
"some string"
</span>
</div>
</div>
</Fragment>
`;

exports[`DetailsTable does render arrays as JSON 1`] = `
<Fragment>
<div>
<div
className="row"
key="0"
>
<span
className="key"
>
key
</span>
<UnControlled
className="valueJson"
editorDidMount={[Function]}
options={
Object {
"gutters": Array [
"codeMirrorGutter",
],
"lineWrapping": true,
"mode": "application/json",
"readOnly": true,
"theme": "default",
}
}
value="[]"
/>
</div>
</div>
</Fragment>
`;

exports[`DetailsTable does render arrays as JSON 2`] = `
<Fragment>
<div>
<div
className="row"
key="0"
>
<span
className="key"
>
key
</span>
<UnControlled
className="valueJson"
editorDidMount={[Function]}
options={
Object {
"gutters": Array [
"codeMirrorGutter",
],
"lineWrapping": true,
"mode": "application/json",
"readOnly": true,
"theme": "default",
}
}
value="{}"
/>
</div>
</div>
</Fragment>
`;

exports[`DetailsTable shows a row with a title 1`] = `
<Fragment>
<div
Expand Down