Skip to content

Commit 9545a90

Browse files
fix: app crash when elements params of Result component was wrong (#114)
Co-authored-by: Stephan Meijer <[email protected]>
1 parent 279a09e commit 9545a90

File tree

5 files changed

+231
-14
lines changed

5 files changed

+231
-14
lines changed

package-lock.json

Lines changed: 191 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@babel/core": "^7.10.2",
4040
"@babel/preset-env": "^7.10.2",
4141
"@babel/preset-react": "^7.10.1",
42+
"@testing-library/jest-dom": "^5.9.0",
4243
"@testing-library/react": "^10.2.0",
4344
"@testing-library/user-event": "^11.2.0",
4445
"babel-eslint": "^10.1.0",
@@ -49,6 +50,7 @@
4950
"husky": "^4.2.5",
5051
"jest": "^26.0.1",
5152
"jest-extended": "^0.11.5",
53+
"jest-in-case": "^1.0.2",
5254
"lint-staged": "^10.2.9",
5355
"npm-run-all": "^4.1.5",
5456
"parcel": "^2.0.0-nightly.280",

src/components/Result.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ function Result({ result, dispatch }) {
1111
);
1212
}
1313

14-
if (!result.expression) {
14+
if (
15+
!result.expression ||
16+
!Array.isArray(result.elements) ||
17+
result.elements.length === 0
18+
) {
1519
return (
1620
<div className="space-y-4 text-sm">
1721
<div className="min-h-8">
@@ -32,11 +36,10 @@ function Result({ result, dispatch }) {
3236
</div>
3337
);
3438
}
35-
36-
const { data, suggestion } = result.elements?.[0] || {};
39+
const { data, suggestion } = result.elements[0];
3740

3841
return (
39-
<div className="flex flex-col overflow-hidden w-full h-full">
42+
<div className="flex flex-col w-full h-full overflow-hidden">
4043
<div className="flex-none pb-4 border-b">
4144
<ResultSuggestion
4245
result={result}

src/components/Result.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import cases from 'jest-in-case';
4+
import Result from './Result';
5+
6+
cases(
7+
'should show "I don\'t know" if elements is not an array or if is empty',
8+
(opts) => {
9+
render(
10+
<Result
11+
result={{ error: '', expression: {}, elements: opts.elements }}
12+
/>,
13+
);
14+
expect(screen.getByText(/i don't know what to say/i)).toBeInTheDocument();
15+
},
16+
[
17+
{
18+
name: 'undefined',
19+
elements: undefined,
20+
},
21+
{
22+
name: 'null',
23+
elements: null,
24+
},
25+
{
26+
name: 'empty array',
27+
elements: [],
28+
},
29+
],
30+
);

tests/setupTests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'regenerator-runtime/runtime';
22
import 'jest-extended';
3+
import '@testing-library/jest-dom';
34

45
if (window.document) {
56
window.document.createRange = () => ({

0 commit comments

Comments
 (0)