11import { elementToJson } from '../src/helper/html-to-json' ;
22import { assetDisplayJson , assetDisplayLink , assetDisplayLinkJson } from './mock/embedded-object-mock' ;
3- import { parse } from 'node-html-parser' ;
43import { entryBlock ,
54 entryInline ,
65 entryLink ,
@@ -12,27 +11,31 @@ import { entryBlock,
1211 entryInlineJson ,
1312 entryBlockJson
1413 } from './mock/embedded-object-mock' ;
15- const noChildNodeHTML = parse ( noChildNode )
14+ const dom = new DOMParser ( ) ;
15+ const getBody = ( content : string ) => {
16+ return dom . parseFromString ( content , 'text/html' ) . body
17+ }
18+
1619describe ( 'HTML To JSON test' , ( ) => {
1720 it ( 'HTML To JSON figure tag with no child test' , done => {
18- expect ( elementToJson ( noChildNodeHTML ) ) . toEqual ( { figure : noChildNodeJson } )
21+ expect ( elementToJson ( getBody ( noChildNode ) ) ) . toEqual ( { figure : noChildNodeJson } )
1922 done ( )
2023 } )
2124
2225 it ( 'HTML to JSON entry figure tag test' , done => {
23- expect ( elementToJson ( parse ( entryLink ) ) ) . toEqual ( { figure : entryLinkJson } )
24- expect ( elementToJson ( parse ( entryInline ) ) ) . toEqual ( { figure : entryInlineJson } )
25- expect ( elementToJson ( parse ( entryBlock ) ) ) . toEqual ( { figure : entryBlockJson } )
26+ expect ( elementToJson ( getBody ( entryLink ) ) ) . toEqual ( { figure : entryLinkJson } )
27+ expect ( elementToJson ( getBody ( entryInline ) ) ) . toEqual ( { figure : entryInlineJson } )
28+ expect ( elementToJson ( getBody ( entryBlock ) ) ) . toEqual ( { figure : entryBlockJson } )
2629 done ( )
2730 } )
2831
2932 it ( 'HTML to JSON asset figure tag test' , done => {
30- expect ( elementToJson ( parse ( assetDisplay ) ) ) . toEqual ( { figure : assetDisplayJson } )
31- expect ( elementToJson ( parse ( assetDisplayLink ) ) ) . toEqual ( { figure : assetDisplayLinkJson } )
33+ expect ( elementToJson ( getBody ( assetDisplay ) ) ) . toEqual ( { figure : assetDisplayJson } )
34+ expect ( elementToJson ( getBody ( assetDisplayLink ) ) ) . toEqual ( { figure : assetDisplayLinkJson } )
3235 done ( )
3336 } )
3437 it ( 'HTML to JSON false tag test' , done => {
35- expect ( elementToJson ( parse ( unexpectedCloseTag ) ) ) . toEqual ( { "figur2" :{
38+ expect ( elementToJson ( getBody ( unexpectedCloseTag ) ) ) . toEqual ( { "figur2" :{
3639 "#text" : " \n" ,
3740 "class" : "embedded-asset" ,
3841 "data-sys-content-type-uid" : "data-sys-content-type-uid" ,
@@ -41,18 +44,8 @@ describe('HTML To JSON test', () => {
4144 "sys-style-type" : "inline" ,
4245 "type" : "asset"
4346 } } )
44- expect ( elementToJson ( parse ( 'String' ) ) ) . toEqual ( { "#text" : "String" } )
45- expect ( elementToJson ( parse ( '' ) ) ) . toEqual ( { } )
46- expect ( elementToJson ( parse ( `<data><![CDATA[This text contains a CEND ]]]]><![CDATA[>]]></data>` ) ) ) . toEqual ( {
47- "data" : {
48- "#text" : "<![CDATA[This text contains a CEND ]]]]><![CDATA[>]]>" ,
49- } ,
50- } )
51- done ( )
52- } )
53-
54- it ( 'HTML comment to JSON false tag test' , done => {
55- expect ( elementToJson ( parse ( '<html><!-- Comment in html --><html>' , { comment : true } ) ) ) . toEqual ( { html : { } } )
47+ expect ( elementToJson ( getBody ( 'String' ) ) ) . toEqual ( { "#text" : "String" } )
48+ expect ( elementToJson ( getBody ( '' ) ) ) . toEqual ( { } )
5649 done ( )
5750 } )
5851} )
0 commit comments