Skip to content

Commit e4301d1

Browse files
author
Sebastian Schürmann
committed
feature(vanillin): pass parameters into the generated dom
1 parent e8e341a commit e4301d1

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

packages/vanillin/src/test-helper.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,16 @@ export class TestHelper {
7373
* @param tagName - The custom element tag name
7474
* @param fileName - Path to the TypeScript source file
7575
* @param compilerOptions - TypeScript compiler options
76+
* @param attributes - Optional key-value pairs of attributes to set on the mounted component
7677
* @returns Promise resolving to a {@link MountContext}
7778
* @throws Error if the compiler host is undefined
7879
*/
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> {
8086
const compiler = await Compiler.withVirtualFs([fileName], compilerOptions);
8187
compiler.emit();
8288
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -86,7 +92,7 @@ export class TestHelper {
8692
}
8793
const outFile = `${compilerOptions.outDir}/${basename(fileName).replace('.ts', '.js')}`;
8894
const compiledCode = host.volume.readFileSync(outFile, 'utf8');
89-
return this.mountAsScript(tagName, compiledCode);
95+
return this.mountAsScript(tagName, compiledCode, attributes);
9096
}
9197

9298
/**
@@ -99,17 +105,25 @@ export class TestHelper {
99105
*
100106
* @param tagName - The custom element tag name
101107
* @param code - The compiled JavaScript code for the component
108+
* @param attributes - Optional key-value pairs of attributes to set on the mounted component
102109
* @returns Promise resolving to a {@link MountContext}
103110
*/
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+
105119
const dom = new JSDOM(`
106120
<!DOCTYPE html>
107121
<html>
108122
<head>
109123
<title>Test Page</title>
110124
</head>
111125
<body>
112-
<${tagName}></${tagName}>
126+
<${tagName} ${attributeString}></${tagName}>
113127
</body>
114128
</html>
115129
`, this.jsdomOptions);

packages/vanillin/test/test-helper.mount-as-script.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,32 @@ describe('TestHelper', () => {
3535
});
3636

3737
});
38+
39+
40+
41+
42+
describe('TestHelper with attributes', () => {
43+
44+
var buildResult: MountContext;
45+
beforeEach(async () => {
46+
const helper = new TestHelper();
47+
buildResult = await helper.mountAsScript('my-circle', myCircleJs, {'size': '20'});
48+
});
49+
50+
afterEach(async () => {
51+
const { jsdom } = buildResult;
52+
jsdom.window.close();
53+
});
54+
55+
it('returns a document', async() => {
56+
const { document } = buildResult;
57+
assert.ok(document);
58+
});
59+
60+
it('should have 20 set as size', () => {
61+
const { document } = buildResult;
62+
const circle = document.querySelector('my-circle');
63+
assert.strictEqual(circle?.getAttribute('size'), '20');
64+
});
65+
66+
});

0 commit comments

Comments
 (0)