-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathHTMLView-test.js
196 lines (163 loc) · 5.66 KB
/
HTMLView-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import React from 'react';
import renderer from 'react-test-renderer';
import {StyleSheet, Text} from 'react-native';
import HTMLView from '../HTMLView';
describe('<HTMLView/>', () => {
it('should render a <Text/> element', () => {
const htmlContent = '<p><a href="http://jsdf.co">&hearts nice job!</a></p>';
expect(
renderer.create(<HTMLView value={htmlContent} />).toJSON()
).toMatchSnapshot();
});
it('should render text in a (nested) <Text/> element', () => {
const htmlContent = 'This is some text';
expect(
renderer.create(<HTMLView value={htmlContent} />).toJSON()
).toMatchSnapshot();
});
it('should render ul bullets', () => {
const htmlContent = '<ul><li> a </li><li> b </li></ul>';
expect(
renderer.create(<HTMLView value={htmlContent} />).toJSON()
).toMatchSnapshot();
});
it('should render ol numbers', () => {
const htmlContent = '<ol><li> a </li><li> b </li></ol>';
expect(
renderer.create(<HTMLView value={htmlContent} />).toJSON()
).toMatchSnapshot();
});
it('should handle additional text nodes between list items', () => {
const htmlContent = `<ol>
<li> a </li>
<li> b </li>
</ol>`;
expect(
renderer.create(<HTMLView value={htmlContent} />).toJSON()
).toMatchSnapshot();
});
it('should render an <Image />, with default width/height of 1', () => {
const imgSrc =
'https://facebook.github.io/react-native/img/header_logo.png';
const htmlContent = `<img src="${imgSrc}"/>`;
expect(
renderer.create(<HTMLView value={htmlContent} />).toJSON()
).toMatchSnapshot();
});
it('should render an <Image /> with set width/height', () => {
const imgSrc =
'https://facebook.github.io/react-native/img/header_logo.png';
const htmlContent = `<img src="${imgSrc}" width="66" height="58"/>`;
expect(
renderer.create(<HTMLView value={htmlContent} />).toJSON()
).toMatchSnapshot();
});
it('should render inherited styles correctly', () => {
const htmlContent = '<b>RED<u>BLUE<i>GREEN</i></u></b>';
const stylesheet = StyleSheet.create({
b: {color: 'red'},
u: {color: 'blue'},
i: {color: 'green'},
});
expect(
renderer
.create(<HTMLView value={htmlContent} stylesheet={stylesheet} />)
.toJSON()
).toMatchSnapshot();
});
it('should render shoddy html including headings, links, bold, italic', () => {
const htmlContent = `
<div class="comment">
<span class="c00">
<h2>> Dwayne’s only companion at night was a Labrador retriever named Sparky.</h2>
<p>
<i>Sparky could not wag his tail-because of an automobile accident many years ago, so he had no way of telling other dogs how friendly he was.
He opened the door of the cage, something Bill couldn’t have done in a thousand years. Bill flew over to a windowsill.
<b>The undippable flag was a beauty, and the anthem and the vacant motto might not have mattered much, if it weren’t for this: a lot of citizens were so ignored and cheated and insulted that they thought they might be in the wrong country, or even on the wrong planet, that some terrible mistake had been made.
</p>
<p>
[1] <a href="https://code.facebook.com/posts/1505962329687926/flow-a-new-static-type-checker-for-javascript/" rel="nofollow">https://code.facebook.com/posts/1505962329687926/flow-a-new-...</a>
</p>
</span>
</div>
`;
expect(
renderer.create(<HTMLView value={htmlContent} />).toJSON()
).toMatchSnapshot();
});
it('should not render extra linebreaks if configured not to', () => {
const htmlContent = `
<div>
<h2>Dwayne’s only companion at night was a Labrador retriever named Sparky.</h2>
<p>
Sparky could not wag his tail-because of an automobile accident many years ago, so he had no way of telling other dogs how friendly he was.<br>
He opened the door of the cage, something Bill couldn’t have done in a thousand years. Bill flew over to a windowsill.
</p>
</div>
`;
expect(
renderer
.create(<HTMLView value={htmlContent} addLineBreaks={false} />)
.toJSON()
).toMatchSnapshot();
});
it('should not render extra linebreaks in list items if configured not to', () => {
const htmlContent = '<ul><li> a </li><li> b </li></ul>';
expect(
renderer
.create(<HTMLView value={htmlContent} addLineBreaks={false} />)
.toJSON()
).toMatchSnapshot();
});
it('can use a custom renderer', () => {
const htmlContent = `
<div>
<thing a="b" />
</div>
`;
function renderNode(node, index) {
if (node.name == 'thing') {
return <Text key={index}>{node.attribs.b}</Text>;
}
}
expect(
renderer
.create(<HTMLView value={htmlContent} renderNode={renderNode} />)
.toJSON()
).toMatchSnapshot();
});
it('can use a custom node class', () => {
class Node extends React.Component {
render() {
return <Text {...this.props} selectable={false} />;
}
}
const htmlContent = `
<div>
<div a="b" />
</div>
`;
expect(
renderer
.create(<HTMLView value={htmlContent} NodeComponent={Node} />)
.toJSON()
).toMatchSnapshot();
});
it('can use custom node props', () => {
const htmlContent = `
<div>
<div a="b" />
</div>
`;
expect(
renderer
.create(
<HTMLView
value={htmlContent}
nodeComponentProps={{selectable: false}}
/>
)
.toJSON()
).toMatchSnapshot();
});
});