@@ -10,16 +10,19 @@ interface TestComponentProps {
1010 theme : Theme ;
1111 transformers ?: ShikiTransformer [ ] ;
1212 tabindex ?: string ;
13+ langAlias ?: Record < string , string > ;
1314}
1415
1516const TestComponent = ( {
1617 code,
1718 language,
1819 theme,
1920 transformers,
21+ langAlias,
2022} : TestComponentProps ) => {
2123 const highlighted = useShikiHighlighter ( code , language , theme , {
2224 transformers,
25+ langAlias,
2326 } ) ;
2427 return < div data-testid = "highlighted" > { highlighted } </ div > ;
2528} ;
@@ -36,37 +39,22 @@ describe('useShikiHighlighter Hook', () => {
3639 return render ( < TestComponent { ...defaultProps } /> ) ;
3740 } ;
3841
39- test ( 'renders pre element with correct theme classes ' , async ( ) => {
42+ test ( 'renders correct DOM structure ' , async ( ) => {
4043 const { getByTestId } = renderComponent ( ) ;
4144 await waitFor ( ( ) => {
4245 const container = getByTestId ( 'highlighted' ) ;
46+
47+ // Check pre element with theme classes
4348 const preElement = container . querySelector (
4449 'pre.shiki.github-light'
4550 ) ;
4651 expect ( preElement ) . toBeInTheDocument ( ) ;
47- } ) ;
48- } ) ;
4952
50- test ( 'renders code element inside pre element' , async ( ) => {
51- const { getByTestId } = renderComponent ( ) ;
52- await waitFor ( ( ) => {
53- const container = getByTestId ( 'highlighted' ) ;
54- const preElement = container . querySelector (
55- 'pre.shiki.github-light'
56- ) ;
53+ // Check code element inside pre
5754 const codeElement = preElement ?. querySelector ( 'code' ) ;
5855 expect ( codeElement ) . toBeInTheDocument ( ) ;
59- } ) ;
60- } ) ;
6156
62- test ( 'renders line spans inside code element' , async ( ) => {
63- const { getByTestId } = renderComponent ( ) ;
64- await waitFor ( ( ) => {
65- const container = getByTestId ( 'highlighted' ) ;
66- const preElement = container . querySelector (
67- 'pre.shiki.github-light'
68- ) ;
69- const codeElement = preElement ?. querySelector ( 'code' ) ;
57+ // Check line spans inside code
7058 const lineSpan = codeElement ?. querySelector ( 'span.line' ) ;
7159 expect ( lineSpan ) . toBeInTheDocument ( ) ;
7260 } ) ;
@@ -124,12 +112,47 @@ describe('useShikiHighlighter Hook', () => {
124112 } ) ;
125113 } ) ;
126114
127- test ( 'matches snapshot for hook rendered output for known language' , async ( ) => {
128- const code = '<div>Hello World</div>' ;
129- const { getByTestId } = renderComponent ( { code } ) ;
115+ test ( 'applies highlighting on aliased language' , async ( ) => {
116+ const code = 'package main' ;
117+ const { getByTestId } = renderComponent ( {
118+ code,
119+ language : 'golang' ,
120+ langAlias : {
121+ golang : 'go' ,
122+ } ,
123+ } ) ;
124+
125+ await waitFor ( ( ) => {
126+ const container = getByTestId ( 'highlighted' ) ;
127+ const preElement = container . querySelector ( 'pre' ) ;
128+ const spanElement = preElement ?. querySelector (
129+ 'code>span>span'
130+ ) as HTMLSpanElement | null ;
131+
132+ expect ( spanElement ) . toBeInTheDocument ( ) ;
133+ expect ( spanElement ) . toHaveStyle ( 'color: #D73A49' ) ;
134+ } ) ;
135+ } ) ;
136+
137+ test ( 'handles multiple language aliases' , async ( ) => {
138+ const code = 'def hello():\n print("world")' ;
139+ const { getByTestId } = renderComponent ( {
140+ code,
141+ language : 'indents' ,
142+ langAlias : {
143+ indents : 'python' ,
144+ } ,
145+ } ) ;
146+
130147 await waitFor ( ( ) => {
131148 const container = getByTestId ( 'highlighted' ) ;
132- expect ( container ) . toMatchSnapshot ( ) ;
149+ const preElement = container . querySelector ( 'pre' ) ;
150+ const defKeyword = Array . from (
151+ preElement ?. querySelectorAll ( 'span' ) || [ ]
152+ ) . find ( ( span ) => span . textContent === 'def' ) ;
153+
154+ expect ( defKeyword ) . toBeInTheDocument ( ) ;
155+ expect ( defKeyword ) . toHaveStyle ( 'color: #D73A49' ) ;
133156 } ) ;
134157 } ) ;
135158} ) ;
0 commit comments