@@ -64,7 +64,8 @@ describe('HTMLElement Plugin', () => {
6464 it ( 'supports an HTML element with attribute and text content' , ( ) => {
6565 const parent = document . createElement ( 'div' ) ;
6666 parent . setAttribute ( 'style' , 'color: #99424F' ) ;
67- parent . innerHTML = 'Jest' ;
67+ const text = document . createTextNode ( 'Jest' ) ;
68+ parent . appendChild ( text ) ;
6869
6970 expect ( parent ) . toPrettyPrintTo (
7071 '<div\n style="color: #99424F"\n>\n Jest\n</div>' ,
@@ -73,7 +74,8 @@ describe('HTMLElement Plugin', () => {
7374
7475 it ( 'supports an element with text content' , ( ) => {
7576 const parent = document . createElement ( 'div' ) ;
76- parent . innerHTML = 'texty texty' ;
77+ const child = document . createTextNode ( 'texty texty' ) ;
78+ parent . appendChild ( child ) ;
7779
7880 expect ( parent ) . toPrettyPrintTo ( '<div>\n texty texty\n</div>' ) ;
7981 } ) ;
@@ -99,6 +101,20 @@ describe('HTMLElement Plugin', () => {
99101 ) ;
100102 } ) ;
101103
104+ it ( 'supports nested elements with attribute and text content' , ( ) => {
105+ const parent = document . createElement ( 'div' ) ;
106+ const child = document . createElement ( 'span' ) ;
107+ parent . appendChild ( child ) ;
108+
109+ child . setAttribute ( 'style' , 'color: #99424F' ) ;
110+ const text = document . createTextNode ( 'Jest' ) ;
111+ child . appendChild ( text ) ;
112+
113+ expect ( parent ) . toPrettyPrintTo (
114+ '<div>\n <span\n style="color: #99424F"\n >\n Jest\n </span>\n</div>' ,
115+ ) ;
116+ } ) ;
117+
102118 it ( 'supports nested elements with text content' , ( ) => {
103119 const parent = document . createElement ( 'div' ) ;
104120 const child = document . createElement ( 'span' ) ;
@@ -128,57 +144,162 @@ describe('HTMLElement Plugin', () => {
128144 ) ;
129145 } ) ;
130146
131- it ( 'trims unnecessary whitespace' , ( ) => {
132- const parent = document . createElement ( 'div' ) ;
133- parent . innerHTML = `
134- <span>
135- some
136- apple
137- pseudo-multilne text
138- </span>
139- <span>text</span>
140- ` ;
147+ it ( 'supports multiline text node in pre' , ( ) => {
148+ const parent = document . createElement ( 'pre' ) ;
149+ parent . innerHTML = [
150+ // prettier-ignore
151+ 'function sum(a, b) {' ,
152+ ' return a + b;' ,
153+ '}' ,
154+ ] . join ( '\n' ) ;
155+
156+ // Ouch. Two lines of text have same indentation for different reason:
157+ // First line of text node because it is at child level.
158+ // Second line of text node because they are in its content.
159+ expect ( parent ) . toPrettyPrintTo (
160+ // prettier-ignore
161+ [
162+ '<pre>' ,
163+ ' function sum(a, b) {' ,
164+ ' return a + b;' ,
165+ '}' ,
166+ '</pre>'
167+ ] . join ( '\n' ) ,
168+ ) ;
169+ } ) ;
170+
171+ it ( 'supports multiline text node preceding span in pre' , ( ) => {
172+ const parent = document . createElement ( 'pre' ) ;
173+ parent . innerHTML = [
174+ '<span class="token keyword">function</span> sum(a, b) {' ,
175+ ' <span class="token keyword">return</span> a + b;' ,
176+ '}' ,
177+ ] . join ( '\n' ) ;
141178
142179 expect ( parent ) . toPrettyPrintTo (
143180 [
144- '<div>' ,
145- ' <span>' ,
146- ' some apple pseudo-multilne text' ,
181+ '<pre>' ,
182+ ' <span' ,
183+ ' class="token keyword"' ,
184+ ' >' ,
185+ ' function' ,
147186 ' </span>' ,
148- ' <span>' ,
149- ' text' ,
187+ ' sum(a, b) {' ,
188+ ' ' ,
189+ ' <span' ,
190+ ' class="token keyword"' ,
191+ ' >' ,
192+ ' return' ,
150193 ' </span>' ,
151- '</div>' ,
194+ ' a + b;' ,
195+ '}' ,
196+ '</pre>' ,
152197 ] . join ( '\n' ) ,
153198 ) ;
154199 } ) ;
155200
156- it ( 'supports text node' , ( ) => {
157- const parent = document . createElement ( 'div' ) ;
158- parent . innerHTML = 'some <span>text</span>' ;
201+ it ( 'supports multiline text node in textarea' , ( ) => {
202+ const textarea = document . createElement ( 'textarea' ) ;
203+ textarea . setAttribute ( 'name' , 'tagline' ) ;
204+ textarea . innerHTML = `Painless.
205+ JavaScript.
206+ Testing.` ;
207+
208+ expect ( textarea ) . toPrettyPrintTo (
209+ [
210+ '<textarea' ,
211+ ' name="tagline"' ,
212+ '>' ,
213+ ' Painless.' ,
214+ 'JavaScript.' ,
215+ 'Testing.' ,
216+ '</textarea>' ,
217+ ] . join ( '\n' ) ,
218+ ) ;
219+ } ) ;
220+
221+ it ( 'supports empty text node' , ( ) => {
222+ // React 16 does not render text in comments (see below)
223+ const parent = document . createElement ( 'span' ) ;
224+ const text = document . createTextNode ( '' ) ;
225+ parent . appendChild ( text ) ;
226+ const abbr = document . createElement ( 'abbr' ) ;
227+ abbr . setAttribute ( 'title' , 'meter' ) ;
228+ abbr . innerHTML = 'm' ;
229+ parent . appendChild ( abbr ) ;
230+
231+ expect ( parent ) . toPrettyPrintTo (
232+ [
233+ '<span>' ,
234+ ' ' ,
235+ ' <abbr' ,
236+ ' title="meter"' ,
237+ ' >' ,
238+ ' m' ,
239+ ' </abbr>' ,
240+ '</span>' ,
241+ ] . join ( '\n' ) ,
242+ ) ;
243+ } ) ;
244+
245+ it ( 'supports non-empty text node' , ( ) => {
246+ // React 16 does not render text in comments (see below)
247+ const parent = document . createElement ( 'p' ) ;
248+ parent . innerHTML = [
249+ '<strong>Jest</strong>' ,
250+ ' means ' ,
251+ '<em>painless</em>' ,
252+ ' Javascript testing' ,
253+ ] . join ( '' ) ;
159254
160- // prettier-ignore
161- expect ( parent ) . toPrettyPrintTo ( [
162- '<div>' ,
163- ' some ' ,
164- ' <span>' ,
165- ' text' ,
166- ' </span>' ,
167- '</div>' ,
168- ] . join ( '\n' ) ) ;
255+ expect ( parent ) . toPrettyPrintTo (
256+ [
257+ '<p>' ,
258+ ' <strong>' ,
259+ ' Jest' ,
260+ ' </strong>' ,
261+ ' means ' ,
262+ ' <em>' ,
263+ ' painless' ,
264+ ' </em>' ,
265+ ' Javascript testing' ,
266+ '</p>' ,
267+ ] . join ( '\n' ) ,
268+ ) ;
169269 } ) ;
170270
171271 it ( 'supports comment node' , ( ) => {
172- const parent = document . createElement ( 'div' ) ;
173- parent . innerHTML = 'some <!-- comments -->' ;
272+ // React 15 does render text in comments
273+ const parent = document . createElement ( 'p' ) ;
274+ parent . innerHTML = [
275+ '<strong>Jest</strong>' ,
276+ '<!-- react-text: 3 -->' ,
277+ ' means ' ,
278+ '<!-- /react-text -->' ,
279+ '<em>painless</em>' ,
280+ '<!-- react-text: 5 -->' ,
281+ ' Javascript testing' ,
282+ '<!-- /react-text -->' ,
283+ ] . join ( '' ) ;
174284
175- // prettier-ignore
176- expect ( parent ) . toPrettyPrintTo ( [
177- '<div>' ,
178- ' some ' ,
179- ' <!-- comments -->' ,
180- '</div>' ,
181- ] . join ( '\n' ) ) ;
285+ expect ( parent ) . toPrettyPrintTo (
286+ [
287+ '<p>' ,
288+ ' <strong>' ,
289+ ' Jest' ,
290+ ' </strong>' ,
291+ ' <!-- react-text: 3 -->' ,
292+ ' means ' ,
293+ ' <!-- /react-text -->' ,
294+ ' <em>' ,
295+ ' painless' ,
296+ ' </em>' ,
297+ ' <!-- react-text: 5 -->' ,
298+ ' Javascript testing' ,
299+ ' <!-- /react-text -->' ,
300+ '</p>' ,
301+ ] . join ( '\n' ) ,
302+ ) ;
182303 } ) ;
183304
184305 it ( 'supports indentation for array of elements' , ( ) => {
@@ -231,13 +352,13 @@ describe('HTMLElement Plugin', () => {
231352 ' <dd>' ,
232353 ' to talk in a ' ,
233354 ' <em … />' ,
234- ' manner' , // plugin incorrectly trims preceding space
355+ ' manner' ,
235356 ' </dd>' ,
236357 ' <dd' ,
237358 ' style="color: #99424F"' ,
238359 ' >' ,
239360 ' <em … />' ,
240- ' JavaScript testing' , // plugin incorrectly trims preceding space
361+ ' JavaScript testing' ,
241362 ' </dd>' ,
242363 '</dl>' ,
243364 ] . join ( '\n' ) ,
0 commit comments