@@ -220,4 +220,78 @@ describe('hast-util-to-dom', () => {
220220 const htmlExpected = '<title>Hi</title><h2>Hello world!</h2>' ;
221221 expect ( htmlActual ) . toEqual ( htmlExpected ) ;
222222 } ) ;
223+
224+ describe ( 'booleanish property' , ( ) => {
225+ it ( 'handles booleanish attribute with `true` value correctly' , ( ) => {
226+ const tree = h ( 'div' , { ariaChecked : true } ) ;
227+ const htmlActual = serializeNodeToHtmlString ( toDOM ( tree ) ) ;
228+ const htmlExpected = '<div aria-checked="true"></div>' ;
229+ expect ( htmlActual ) . toEqual ( htmlExpected ) ;
230+ } ) ;
231+
232+ it ( 'handles booleanish attribute with `false` value correctly' , ( ) => {
233+ const tree = h ( 'div' , { ariaChecked : false } ) ;
234+ const htmlActual = serializeNodeToHtmlString ( toDOM ( tree ) ) ;
235+ const htmlExpected = '<div aria-checked="false"></div>' ;
236+ expect ( htmlActual ) . toEqual ( htmlExpected ) ;
237+ } ) ;
238+
239+ it ( 'handles booleanish attribute with value correctly' , ( ) => {
240+ const tree = h ( 'div' , { ariaChecked : 'mixed' } ) ;
241+ const htmlActual = serializeNodeToHtmlString ( toDOM ( tree ) ) ;
242+ const htmlExpected = '<div aria-checked="mixed"></div>' ;
243+ expect ( htmlActual ) . toEqual ( htmlExpected ) ;
244+ } ) ;
245+ } ) ;
246+
247+ describe ( 'data properties' , ( ) => {
248+ it ( 'ignores value when property is `false`' , ( ) => {
249+ const tree = h ( 'div' , { dataTest : false } ) ;
250+ const htmlActual = serializeNodeToHtmlString ( toDOM ( tree ) ) ;
251+ const htmlExpected = '<div></div>' ;
252+ expect ( htmlActual ) . toEqual ( htmlExpected ) ;
253+ } ) ;
254+
255+ it ( 'ignores value when property is `NaN`' , ( ) => {
256+ const tree = h ( 'div' , { dataTest : NaN } ) ;
257+ const htmlActual = serializeNodeToHtmlString ( toDOM ( tree ) ) ;
258+ const htmlExpected = '<div></div>' ;
259+ expect ( htmlActual ) . toEqual ( htmlExpected ) ;
260+ } ) ;
261+
262+ it ( 'encodes value as string when property is a number' , ( ) => {
263+ const tree = h ( 'div' , { dataTest : 0 } ) ;
264+ const htmlActual = serializeNodeToHtmlString ( toDOM ( tree ) ) ;
265+ const htmlExpected = '<div data-test="0"></div>' ;
266+ expect ( htmlActual ) . toEqual ( htmlExpected ) ;
267+ } ) ;
268+
269+ it ( 'encodes without value when property is `true`' , ( ) => {
270+ const tree = h ( 'div' , { dataTest : true } ) ;
271+ const htmlActual = serializeNodeToHtmlString ( toDOM ( tree ) ) ;
272+ const htmlExpected = '<div data-test=""></div>' ;
273+ expect ( htmlActual ) . toEqual ( htmlExpected ) ;
274+ } ) ;
275+
276+ it ( 'encodes an empty value when property is an empty string' , ( ) => {
277+ const tree = h ( 'div' , { dataTest : '' } ) ;
278+ const htmlActual = serializeNodeToHtmlString ( toDOM ( tree ) ) ;
279+ const htmlExpected = '<div data-test=""></div>' ;
280+ expect ( htmlActual ) . toEqual ( htmlExpected ) ;
281+ } ) ;
282+
283+ it ( 'encodes a string value as-is' , ( ) => {
284+ const tree = h ( 'div' , { dataTest : 'data-test' } ) ;
285+ const htmlActual = serializeNodeToHtmlString ( toDOM ( tree ) ) ;
286+ const htmlExpected = '<div data-test="data-test"></div>' ;
287+ expect ( htmlActual ) . toEqual ( htmlExpected ) ;
288+ } ) ;
289+
290+ it ( 'encodes a string value as-is' , ( ) => {
291+ const tree = h ( 'div' , { data123 : 'dataTest' } ) ;
292+ const htmlActual = serializeNodeToHtmlString ( toDOM ( tree ) ) ;
293+ const htmlExpected = '<div data-123="dataTest"></div>' ;
294+ expect ( htmlActual ) . toEqual ( htmlExpected ) ;
295+ } ) ;
296+ } ) ;
223297} ) ;
0 commit comments