Skip to content

Commit 4c3f41b

Browse files
Merge pull request #779 from remarkablemark/test/types
test(types): refactor dtslint tests
2 parents 9782311 + 608873a commit 4c3f41b

File tree

8 files changed

+89
-88
lines changed

8 files changed

+89
-88
lines changed

test/types/error.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import parse from 'html-react-parser';
2+
3+
// $ExpectError
4+
parse();

test/types/exports.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { domToReact, htmlToDOM } from 'html-react-parser';
2+
3+
// $ExpectType (Element | Text | Comment | ProcessingInstruction)[]
4+
const domNodes = htmlToDOM('<div>text</div>');
5+
6+
// $ExpectType string | Element | Element[]
7+
domToReact(domNodes);

test/types/index.tsx

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import parse, {
2-
Element,
3-
HTMLReactParserOptions,
4-
domToReact,
5-
htmlToDOM
6-
} from 'html-react-parser';
7-
import * as React from 'react';
8-
9-
// $ExpectError
10-
parse();
1+
import parse from 'html-react-parser';
112

123
// $ExpectType string | Element | Element[]
134
parse('');
@@ -20,74 +11,3 @@ parse('<p>text</p>');
2011

2112
// $ExpectType string | Element | Element[]
2213
parse('<li>1</li><li>2</li>');
23-
24-
// $ExpectType string | Element | Element[]
25-
parse('<br id="replace">', {
26-
replace: domNode => {
27-
if (domNode instanceof Element && domNode.attribs.id === 'replace') {
28-
return <span>replaced</span>;
29-
}
30-
}
31-
});
32-
33-
// $ExpectType string | Element | Element[]
34-
parse('<br id="remove">', {
35-
replace: domNode => {
36-
if (domNode instanceof Element && domNode.attribs.id === 'remove') {
37-
return <></>;
38-
}
39-
}
40-
});
41-
42-
let options: HTMLReactParserOptions;
43-
44-
options = {
45-
replace: domNode => {
46-
if (domNode instanceof Element && domNode.attribs.id === 'header') {
47-
return;
48-
}
49-
}
50-
};
51-
52-
options = {
53-
replace: domNode => {
54-
if (domNode instanceof Element) {
55-
return <>{domToReact(domNode.children)}</>;
56-
}
57-
}
58-
};
59-
60-
// $ExpectType string | Element | Element[]
61-
parse('<a id="header" href="#">Heading</a>', options);
62-
63-
// $ExpectType string | Element | Element[]
64-
parse('<hr>', {
65-
library: {
66-
cloneElement: (element, props, children) =>
67-
React.cloneElement(element, props, children),
68-
createElement: (type, props, children) =>
69-
React.createElement(type, props, children),
70-
isValidElement: element => React.isValidElement(element)
71-
}
72-
});
73-
74-
// $ExpectType string | Element | Element[]
75-
parse('<p/><p/>', {
76-
htmlparser2: {
77-
xmlMode: true,
78-
decodeEntities: true,
79-
lowerCaseTags: false,
80-
lowerCaseAttributeNames: false,
81-
recognizeCDATA: true,
82-
recognizeSelfClosing: true
83-
}
84-
});
85-
86-
// $ExpectType string | Element | Element[]
87-
parse('\t<p>text \r</p>\n', { trim: true });
88-
89-
// $ExpectType (Element | Text | Comment | ProcessingInstruction)[]
90-
const domNodes = htmlToDOM('<div>text</div>');
91-
92-
// $ExpectType string | Element | Element[]
93-
domToReact(domNodes);

test/types/options/htmlparser2.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import parse from 'html-react-parser';
2+
3+
// $ExpectType string | Element | Element[]
4+
parse('<p/><p/>', {
5+
htmlparser2: {
6+
xmlMode: true,
7+
decodeEntities: true,
8+
lowerCaseTags: false,
9+
lowerCaseAttributeNames: false,
10+
recognizeCDATA: true,
11+
recognizeSelfClosing: true
12+
}
13+
});

test/types/options/library.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import parse from 'html-react-parser';
2+
import * as React from 'react';
3+
4+
// $ExpectType string | Element | Element[]
5+
parse('<hr>', {
6+
library: {
7+
cloneElement: (element, props, children) =>
8+
React.cloneElement(element, props, children),
9+
createElement: (type, props, children) =>
10+
React.createElement(type, props, children),
11+
isValidElement: element => React.isValidElement(element)
12+
}
13+
});

test/types/options/replace.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import parse, {
2+
Element,
3+
HTMLReactParserOptions,
4+
domToReact
5+
} from 'html-react-parser';
6+
import * as React from 'react';
7+
8+
// $ExpectType string | Element | Element[]
9+
parse('<br id="replace">', {
10+
replace: domNode => {
11+
if (domNode instanceof Element && domNode.attribs.id === 'replace') {
12+
return <span>replaced</span>;
13+
}
14+
}
15+
});
16+
17+
// $ExpectType string | Element | Element[]
18+
parse('<br id="remove">', {
19+
replace: domNode => {
20+
if (domNode instanceof Element && domNode.attribs.id === 'remove') {
21+
return <></>;
22+
}
23+
}
24+
});
25+
26+
let options: HTMLReactParserOptions;
27+
28+
options = {
29+
replace: domNode => {
30+
if (domNode instanceof Element && domNode.attribs.id === 'header') {
31+
return;
32+
}
33+
}
34+
};
35+
36+
options = {
37+
replace: domNode => {
38+
if (domNode instanceof Element) {
39+
return <>{domToReact(domNode.children)}</>;
40+
}
41+
}
42+
};
43+
44+
// $ExpectType string | Element | Element[]
45+
parse('<a id="header" href="#">Heading</a>', options);

test/types/options/trim.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import parse from 'html-react-parser';
2+
3+
// $ExpectType string | Element | Element[]
4+
parse('\t<p>text \r</p>\n', { trim: true });

tsconfig.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
},
1717
"jsx": "react"
1818
},
19-
"files": [
20-
"index.d.ts",
21-
"lib/dom-to-react.d.ts",
22-
"test/types/index.tsx",
23-
"test/types/lib/attributes-to-props.ts",
24-
"test/types/lib/dom-to-react.tsx"
25-
]
19+
"files": ["index.d.ts", "lib/dom-to-react.d.ts"],
20+
"include": ["test/types/**/*.ts", "test/types/**/*.tsx"]
2621
}

0 commit comments

Comments
 (0)