@@ -4,6 +4,8 @@ import PropTypes from "prop-types"
44import React from "react"
55import { describe , it , expect , assert } from "vitest"
66
7+ import { R2WCElement } from "@r2wc/core"
8+
79import r2wc from "./react-to-web-component"
810
911expect . extend ( matchers )
@@ -364,90 +366,97 @@ describe("react-to-web-component 1", () => {
364366 } )
365367 } )
366368
367- it ( "Supports class function to react props using method transform" , async ( ) => {
368- const ClassGreeting : React . FC < { name : string ; sayHello : ( ) => void } > = ( {
369- name,
370- sayHello,
371- } ) => (
372- < div >
373- < h1 > Hello, { name } </ h1 >
374- < button onClick = { sayHello } > Click me</ button >
375- </ div >
376- )
369+ it . each ( [ [ undefined ] , [ "open" ] , [ "closed" ] ] ) (
370+ `Supports class function to react props using method transform: (shadow: %s)` ,
371+ async ( shadow ) => {
372+ const ClassGreeting : React . FC < { name : string ; sayHello : ( ) => void } > = ( {
373+ name,
374+ sayHello,
375+ } ) => (
376+ < div >
377+ < h1 > Hello, { name } </ h1 >
378+ < button onClick = { sayHello } > Click me</ button >
379+ </ div >
380+ )
377381
378- const WebClassGreeting = r2wc ( ClassGreeting , {
379- props : {
380- name : "string" ,
381- sayHello : "method" ,
382- } ,
383- } )
382+ const WebClassGreeting = r2wc ( ClassGreeting , {
383+ props : {
384+ name : "string" ,
385+ sayHello : "method" ,
386+ } ,
387+ shadow : shadow as unknown as Exclude <
388+ Parameters < typeof r2wc > [ 1 ] ,
389+ undefined
390+ > [ "shadow" ] ,
391+ } )
384392
385- customElements . define ( " class-greeting" , WebClassGreeting )
393+ const tagName = ` class-greeting${ shadow ? `- ${ shadow } ` : "" } `
386394
387- document . body . innerHTML = `<class-greeting name='Christopher'></class-greeting>`
395+ customElements . define ( tagName , WebClassGreeting )
388396
389- const el = document . querySelector < HTMLElement & { sayHello ?: ( ) => void } > (
390- "class-greeting" ,
391- )
397+ document . body . innerHTML = `<${ tagName } name='Christopher'></class-greeting>`
392398
393- if ( ! el ) {
394- throw new Error ( "Element not found" )
395- }
399+ const el = document . querySelector <
400+ R2WCElement & { sayHello ?: ( ) => void }
401+ > ( tagName )
396402
397- const sayHello = function ( this : HTMLElement ) {
398- const nameElement = this . querySelector ( "h1" )
399- if ( nameElement ) {
400- nameElement . textContent = "Hello, again"
403+ if ( ! el ) {
404+ throw new Error ( "Element not found" )
401405 }
402- }
403406
404- el . sayHello = sayHello . bind ( el )
407+ const sayHello = function ( this : R2WCElement ) {
408+ const nameElement = this . container . querySelector ( "h1" )
409+ if ( nameElement ) {
410+ nameElement . textContent = "Hello, again"
411+ }
412+ }
405413
406- await new Promise ( ( resolve , reject ) => {
407- const failIfNotClicked = setTimeout ( ( ) => {
408- reject ( )
409- } , 1000 )
414+ el . sayHello = sayHello
410415
411- setTimeout ( ( ) => {
412- document
413- . querySelector < HTMLButtonElement > ( "class-greeting button" )
414- ?. click ( )
416+ const docRoot = el . container . getRootNode ( ) as Document | DocumentFragment
417+
418+ await new Promise ( ( resolve , reject ) => {
419+ const failIfNotClicked = setTimeout ( ( ) => {
420+ reject ( )
421+ } , 1000 )
415422
416423 setTimeout ( ( ) => {
417- const element = document . querySelector ( "h1" )
418- expect ( element ?. textContent ) . toEqual ( "Hello, again" )
419- clearTimeout ( failIfNotClicked )
420- resolve ( true )
424+ docRoot . querySelector < HTMLButtonElement > ( `button` ) ?. click ( )
425+
426+ setTimeout ( ( ) => {
427+ const element = docRoot . querySelector ( "h1" )
428+ expect ( element ?. textContent ) . toEqual ( "Hello, again" )
429+ clearTimeout ( failIfNotClicked )
430+ resolve ( true )
431+ } , 0 )
421432 } , 0 )
422- } , 0 )
423- } )
433+ } )
424434
425- const sayHelloRerendered = function ( this : HTMLElement ) {
426- const nameElement = this . querySelector ( "h1" )
427- if ( nameElement ) {
428- nameElement . textContent = "Hello, again rerendered"
435+ const sayHelloRerendered = function ( this : R2WCElement ) {
436+ const nameElement = this . container . querySelector ( "h1" )
437+ if ( nameElement ) {
438+ nameElement . textContent = "Hello, again rerendered"
439+ }
429440 }
430- }
431-
432- el . sayHello = sayHelloRerendered . bind ( el )
433441
434- await new Promise ( ( resolve , reject ) => {
435- const failIfNotClicked = setTimeout ( ( ) => {
436- reject ( )
437- } , 1000 )
442+ el . sayHello = sayHelloRerendered
438443
439- setTimeout ( ( ) => {
440- document
441- . querySelector < HTMLButtonElement > ( "class-greeting button" )
442- ?. click ( )
444+ await new Promise ( ( resolve , reject ) => {
445+ const failIfNotClicked = setTimeout ( ( ) => {
446+ reject ( )
447+ } , 1000 )
443448
444449 setTimeout ( ( ) => {
445- const element = document . querySelector < HTMLHeadingElement > ( "h1" )
446- expect ( element ?. textContent ) . toEqual ( "Hello, again rerendered" )
447- clearTimeout ( failIfNotClicked )
448- resolve ( true )
450+ docRoot . querySelector < HTMLButtonElement > ( `button` ) ?. click ( )
451+
452+ setTimeout ( ( ) => {
453+ const element = docRoot . querySelector < HTMLHeadingElement > ( "h1" )
454+ expect ( element ?. textContent ) . toEqual ( "Hello, again rerendered" )
455+ clearTimeout ( failIfNotClicked )
456+ resolve ( true )
457+ } , 0 )
449458 } , 0 )
450- } , 0 )
451- } )
452- } )
459+ } )
460+ } ,
461+ )
453462} )
0 commit comments