Skip to content

Commit 13808aa

Browse files
author
Sebastian Schürmann
committed
test(vanillin): test the TestHelpers auto mode
1 parent 5b47c9d commit 13808aa

File tree

4 files changed

+97
-2
lines changed

4 files changed

+97
-2
lines changed

packages/vanillin/src/test-helper.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ export class TestHelper {
7676
* @returns Promise resolving to a {@link MountContext}
7777
* @throws Error if the compiler host is undefined
7878
*/
79-
async compileAndMountAsScript(tagName: string, fileName: string, compilerOptions: CompilerOptions): Promise<MountContext> {
79+
async compileAndMountAsScript(tagName: string, fileName: string, compilerOptions: CompilerOptions = Compiler.DEFAULT_COMPILER_OPTIONS): Promise<MountContext> {
8080
const compiler = await Compiler.withVirtualFs([fileName], compilerOptions);
8181
compiler.emit();
8282
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8383
const { host } = compiler as any;
8484
if (!host) {
8585
throw new Error('Host is undefined');
8686
}
87-
const outFile = `/dist/${basename(fileName).replace('.ts', '.js')}`;
87+
const outFile = `${compilerOptions.outDir}/${basename(fileName).replace('.ts', '.js')}`;
8888
const compiledCode = host.volume.readFileSync(outFile, 'utf8');
8989
return this.mountAsScript(tagName, compiledCode);
9090
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class MyCircle extends HTMLElement {
2+
private _size: number = 50;
3+
private _color: string = 'red';
4+
5+
constructor() {
6+
super();
7+
this.attachShadow({ mode: 'open' });
8+
this.render();
9+
}
10+
11+
static get observedAttributes() {
12+
return ['size', 'color'];
13+
}
14+
15+
get size() {
16+
return this._size;
17+
}
18+
19+
set size(value: number) {
20+
this._size = value;
21+
this.render();
22+
}
23+
24+
get color() {
25+
return this._color;
26+
}
27+
28+
set color(value: string) {
29+
this._color = value;
30+
this.render();
31+
}
32+
33+
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) {
34+
if (oldValue === newValue) return;
35+
36+
switch (name) {
37+
case 'size':
38+
this.size = parseInt(newValue || '50');
39+
break;
40+
case 'color':
41+
this.color = newValue || 'red';
42+
break;
43+
}
44+
}
45+
46+
private render() {
47+
if (!this.shadowRoot) return;
48+
49+
this.shadowRoot.innerHTML = `
50+
<svg width="${this._size * 2}" height="${this._size * 2}" viewBox="0 0 ${this._size * 2} ${this._size * 2}">
51+
<circle
52+
cx="${this._size}"
53+
cy="${this._size}"
54+
r="${this._size}"
55+
fill="${this._color}"
56+
/>
57+
</svg>
58+
`;
59+
}
60+
}
61+
62+
customElements.define('my-circle', MyCircle);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, it, beforeEach, afterEach } from 'node:test';
2+
import assert from 'node:assert';
3+
import { resolve, dirname } from 'path';
4+
import { fileURLToPath } from 'url';
5+
import { readFileSync } from 'fs';
6+
import { TestHelper, MountContext } from '../src/test-helper.js';
7+
8+
const __filename = fileURLToPath(import.meta.url);
9+
const __dirname = dirname(__filename);
10+
const myCircleJs = readFileSync(resolve(__dirname, './fixtures/my-circle.js'), 'utf8');
11+
12+
describe('TestHelper.compileAndMountAsScript', () => {
13+
14+
var buildResult: MountContext;
15+
beforeEach(async () => {
16+
const helper = new TestHelper();
17+
helper.compileAndMountAsScript('my-circle', resolve(__dirname, './fixtures/my-circle.ts'));
18+
buildResult = await helper.mountAsScript('my-circle', myCircleJs);
19+
});
20+
21+
afterEach(async () => {
22+
const { jsdom } = buildResult;
23+
jsdom.window.close();
24+
});
25+
26+
it('returns a document', async() => {
27+
const { document } = buildResult;
28+
assert.ok(document);
29+
});
30+
31+
32+
33+
});

0 commit comments

Comments
 (0)