Skip to content

Commit 8cd62a8

Browse files
Throw an error if the first argument for the parser is not a string
This is to give the user gets feedback if the first argument is invalid. Also, add tests to confirm behavior.
1 parent 80ca2e2 commit 8cd62a8

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
* @return {ReactElement|Array}
5252
*/
5353
function HTMLReactParser(html, options) {
54+
if (typeof html !== 'string') {
55+
throw new Error('`HTMLReactParser`: The first argument must be a string.');
56+
}
5457
return domToReact(htmlToDOM(html), options);
5558
}
5659

test/html-to-react.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ describe('html-to-react', function() {
2929
*/
3030
describe('parser', function() {
3131

32+
it('throws an error if first argument is not a string', function() {
33+
assert.throws(function() { Parser(); });
34+
35+
[undefined, null, {}, [], 42].forEach(function(arg) {
36+
assert.throws(function() { Parser(arg); });
37+
});
38+
});
39+
40+
it('returns string if cannot be parsed to HTML', function() {
41+
assert.equal(Parser('foo'), 'foo');
42+
});
43+
3244
it('converts single HTML element to React', function() {
3345
var html = data.html.single;
3446
var reactElement = Parser(html);

0 commit comments

Comments
 (0)