11import React from "react" ;
2- import { render } from "@testing-library/react" ;
2+ import { describe , it , expect } from "vitest" ;
3+ import { renderToString } from "react-dom/server" ;
34import { TextBlockWidget } from "./TextBlockWidget" ;
45
56describe ( "TextBlockWidget id attribute" , ( ) => {
@@ -20,49 +21,47 @@ describe("TextBlockWidget id attribute", () => {
2021 "Display" ,
2122 ] as const ;
2223
23- test . each ( variants ) ( "%s variant renders id attribute" , ( variant ) => {
24- const testId = `test-anchor-${ variant . toLowerCase ( ) } ` ;
25- const { container } = render (
26- < TextBlockWidget
27- id = "test-widget"
28- content = "Test content"
29- variant = { variant }
30- anchor = { testId }
31- />
32- ) ;
24+ variants . forEach ( ( variant ) => {
25+ it ( `${ variant } variant renders id attribute` , ( ) => {
26+ const testId = `test-anchor-${ variant . toLowerCase ( ) } ` ;
27+ const html = renderToString (
28+ < TextBlockWidget
29+ id = "test-widget"
30+ content = "Test content"
31+ variant = { variant }
32+ anchor = { testId }
33+ /> ,
34+ ) ;
3335
34- const element = container . querySelector ( `#${ testId } ` ) ;
35- expect ( element ) . not . toBeNull ( ) ;
36- expect ( element ?. id ) . toBe ( testId ) ;
36+ expect ( html ) . toContain ( `id="${ testId } "` ) ;
37+ } ) ;
3738 } ) ;
3839
39- test ( "heading variants also render id attribute" , ( ) => {
40+ it ( "heading variants render id attribute" , ( ) => {
4041 const headingVariants = [ "H1" , "H2" , "H3" , "H4" , "H5" , "H6" ] as const ;
4142
4243 headingVariants . forEach ( ( variant ) => {
4344 const testId = `test-anchor-${ variant . toLowerCase ( ) } ` ;
44- const { container } = render (
45+ const html = renderToString (
4546 < TextBlockWidget
4647 id = "test-widget"
4748 content = "Test heading"
4849 variant = { variant }
4950 anchor = { testId }
50- />
51+ /> ,
5152 ) ;
5253
53- const element = container . querySelector ( `#${ testId } ` ) ;
54- expect ( element ) . not . toBeNull ( ) ;
55- expect ( element ?. id ) . toBe ( testId ) ;
54+ expect ( html ) . toContain ( `id="${ testId } "` ) ;
5655 } ) ;
5756 } ) ;
5857
59- test ( "no id attribute when anchor is not provided" , ( ) => {
60- const { container } = render (
61- < TextBlockWidget id = "test-widget" content = "Test content" variant = "P" />
58+ it ( "no id attribute when anchor is not provided" , ( ) => {
59+ const html = renderToString (
60+ < TextBlockWidget id = "test-widget" content = "Test content" variant = "P" /> ,
6261 ) ;
6362
64- const paragraph = container . querySelector ( "p" ) ;
65- expect ( paragraph ) . not . toBeNull ( ) ;
66- expect ( paragraph ?. id ) . toBe ( "" ) ;
63+ const paragraph = html . match ( / < p [ ^ > ] * > / ) ?. [ 0 ] ;
64+ expect ( paragraph ) . toBeDefined ( ) ;
65+ expect ( paragraph ) . not . toContain ( 'id="' ) ;
6766 } ) ;
6867} ) ;
0 commit comments