@@ -73,10 +73,16 @@ export class TestHelper {
73
73
* @param tagName - The custom element tag name
74
74
* @param fileName - Path to the TypeScript source file
75
75
* @param compilerOptions - TypeScript compiler options
76
+ * @param attributes - Optional key-value pairs of attributes to set on the mounted component
76
77
* @returns Promise resolving to a {@link MountContext}
77
78
* @throws Error if the compiler host is undefined
78
79
*/
79
- async compileAndMountAsScript ( tagName : string , fileName : string , compilerOptions : CompilerOptions = Compiler . DEFAULT_COMPILER_OPTIONS ) : Promise < MountContext > {
80
+ async compileAndMountAsScript (
81
+ tagName : string ,
82
+ fileName : string ,
83
+ compilerOptions : CompilerOptions = Compiler . DEFAULT_COMPILER_OPTIONS ,
84
+ attributes ?: Record < string , string >
85
+ ) : Promise < MountContext > {
80
86
const compiler = await Compiler . withVirtualFs ( [ fileName ] , compilerOptions ) ;
81
87
compiler . emit ( ) ;
82
88
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -86,7 +92,7 @@ export class TestHelper {
86
92
}
87
93
const outFile = `${ compilerOptions . outDir } /${ basename ( fileName ) . replace ( '.ts' , '.js' ) } ` ;
88
94
const compiledCode = host . volume . readFileSync ( outFile , 'utf8' ) ;
89
- return this . mountAsScript ( tagName , compiledCode ) ;
95
+ return this . mountAsScript ( tagName , compiledCode , attributes ) ;
90
96
}
91
97
92
98
/**
@@ -99,17 +105,25 @@ export class TestHelper {
99
105
*
100
106
* @param tagName - The custom element tag name
101
107
* @param code - The compiled JavaScript code for the component
108
+ * @param attributes - Optional key-value pairs of attributes to set on the mounted component
102
109
* @returns Promise resolving to a {@link MountContext}
103
110
*/
104
- async mountAsScript ( tagName : string , code : string ) : Promise < MountContext > {
111
+ async mountAsScript ( tagName : string , code : string , attributes ?: Record < string , string > ) : Promise < MountContext > {
112
+ const defaultAttributes = { } ;
113
+
114
+ const mergedAttributes = { ...defaultAttributes , ...attributes } ;
115
+ const attributeString = Object . entries ( mergedAttributes )
116
+ . map ( ( [ key , value ] ) => `${ key } ="${ value } "` )
117
+ . join ( ' ' ) ;
118
+
105
119
const dom = new JSDOM ( `
106
120
<!DOCTYPE html>
107
121
<html>
108
122
<head>
109
123
<title>Test Page</title>
110
124
</head>
111
125
<body>
112
- <${ tagName } ></${ tagName } >
126
+ <${ tagName } ${ attributeString } ></${ tagName } >
113
127
</body>
114
128
</html>
115
129
` , this . jsdomOptions ) ;
0 commit comments