1+ /* eslint-disable unicorn/prefer-dom-node-text-content */
2+
13import type http from 'http' ;
24import type https from 'https' ;
35import { join } from 'path' ;
4- import { load } from 'cheerio ' ;
6+ import { DOMParser } from 'domparser-rs ' ;
57import { globSync } from 'glob' ;
68import { createServer } from 'http-server' ;
79import fetch from 'isomorphic-fetch' ;
@@ -23,8 +25,56 @@ describe('site test', () => {
2325 const port = await portPromise ;
2426 const resp = await fetch ( `http://127.0.0.1:${ port } ${ path } ` ) . then ( async ( res ) => {
2527 const html : string = await res . text ( ) ;
26- const $ = load ( html , { xml : true } ) ;
27- return { status : res . status , $ } ;
28+ const root = new DOMParser ( ) . parseFromString ( html , 'text/html' ) ;
29+ function getTextContent ( node : any ) : string {
30+ if ( ! node ) return '' ;
31+ if ( typeof node . textContent === 'string' ) return node . textContent . trim ( ) ;
32+ if ( typeof node . innerText === 'string' ) return node . innerText . trim ( ) ;
33+ // Fallback: recursively get text from children
34+ if ( node . children && node . children . length > 0 ) {
35+ return Array . from ( node . children )
36+ . map ( ( child : any ) => getTextContent ( child ) )
37+ . join ( '' )
38+ . trim ( ) ;
39+ }
40+ return '' ;
41+ }
42+ function wrap ( nodes : any [ ] ) {
43+ const list = Array . isArray ( nodes ) ? nodes : [ ] ;
44+ return {
45+ length : list . length ,
46+ text : ( ) => {
47+ if ( list . length === 0 ) return '' ;
48+ return list . map ( ( n ) => getTextContent ( n ) ) . join ( '' ) ;
49+ } ,
50+ first : ( ) => wrap ( list . slice ( 0 , 1 ) ) ,
51+ } ;
52+ }
53+ const $ = ( selector : string ) => {
54+ if ( ! root . querySelector ) {
55+ console . warn ( 'DOMParser does not support querySelector' ) ;
56+ return wrap ( [ ] ) ;
57+ }
58+
59+ // Handle complex selectors that domparser-rs might not support
60+ if ( selector === '.markdown table' ) {
61+ // Find all .markdown elements and then find tables within them
62+ const markdownElements = root . querySelectorAll ( '.markdown' ) ;
63+ const tables = [ ] ;
64+ for ( const markdown of Array . from ( markdownElements ) ) {
65+ const tablesInMarkdown = markdown . querySelectorAll ( 'table' ) ;
66+ tables . push ( ...Array . from ( tablesInMarkdown ) ) ;
67+ }
68+ return wrap ( tables ) ;
69+ } else {
70+ // Use querySelectorAll for simple selectors
71+ const elements = root . querySelectorAll ( selector ) ;
72+ const elementsArray = Array . from ( elements ) ;
73+ return wrap ( elementsArray ) ;
74+ }
75+ } ;
76+
77+ return { status : res . status , $, root } ;
2878 } ) ;
2979 return resp ;
3080 } ;
@@ -35,9 +85,27 @@ describe('site test', () => {
3585 } ;
3686
3787 const expectComponent = async ( component : string ) => {
38- const { status, $ } = await render ( `/${ component } /` ) ;
88+ const { status, $, root } = await render ( `/${ component } /` ) ;
3989 expect ( status ) . toBe ( 200 ) ;
40- expect ( $ ( 'h1' ) . text ( ) . toLowerCase ( ) ) . toMatch ( handleComponentName ( component ) ) ;
90+
91+ // Get all h1 elements and find the one in main content (not in header)
92+ const h1Elements = root . querySelectorAll ( 'h1' ) ;
93+ let mainH1Text = '' ;
94+
95+ if ( h1Elements . length >= 2 ) {
96+ // The second h1 should be the main content title
97+ const mainH1 = h1Elements [ 1 ] ;
98+ mainH1Text = mainH1 . textContent || ( mainH1 as any ) . innerText || '' ;
99+ } else if ( h1Elements . length === 1 ) {
100+ // If only one h1, check its content
101+ const h1 = h1Elements [ 0 ] ;
102+ mainH1Text = h1 . textContent || ( h1 as any ) . innerText || '' ;
103+ }
104+
105+ // Clean up the text and extract the main component name
106+ mainH1Text = mainH1Text . trim ( ) ;
107+
108+ expect ( mainH1Text . toLowerCase ( ) ) . toMatch ( handleComponentName ( component ) ) ;
41109
42110 /**
43111 * 断言组件的 api table 数量是否符合预期。
0 commit comments