fix(tests): Improve decorator tests and add LitElement component tests for better validation

This commit is contained in:
Juergen Kunz
2025-06-26 19:58:45 +00:00
parent bbe96ea1f5
commit 40d6b8c40d
4 changed files with 85 additions and 1 deletions

41
test/test-decorators.ts Normal file
View File

@ -0,0 +1,41 @@
// Test file to verify decorator functionality
function sealed(constructor: Function) {
Object.seal(constructor);
Object.seal(constructor.prototype);
}
@sealed
class TestClass {
name = 'test';
modify() {
this.name = 'modified';
}
}
// Test that the class is sealed
const instance = new TestClass();
console.log('Initial name:', instance.name);
// This should work (modifying existing property)
instance.modify();
console.log('Modified name:', instance.name);
// This should fail silently in non-strict mode or throw in strict mode
try {
(instance as any).newProperty = 'should not work';
console.log('Adding new property:', (instance as any).newProperty);
} catch (e) {
console.log('Error adding property (expected):', e.message);
}
// Test that we can't add to prototype
try {
(TestClass.prototype as any).newMethod = function() {};
console.log('Prototype is NOT sealed (unexpected)');
} catch (e) {
console.log('Prototype is sealed (expected)');
}
console.log('Is TestClass sealed?', Object.isSealed(TestClass));
console.log('Is TestClass.prototype sealed?', Object.isSealed(TestClass.prototype));

37
test/ts_web/test-lit.ts Normal file
View File

@ -0,0 +1,37 @@
import { LitElement, html, css } from 'lit';
import { customElement, property } from 'lit/decorators.js';
@customElement('my-element')
export class MyElement extends LitElement {
static styles = css`
:host {
display: block;
padding: 16px;
}
`;
@property({ type: String })
name = 'World';
@property({ type: Number })
count = 0;
render() {
return html`
<h1>Hello, ${this.name}!</h1>
<button @click=${this._onClick}>
Click Count: ${this.count}
</button>
`;
}
private _onClick() {
this.count++;
}
}
// Test instantiation
const element = new MyElement();
console.log('Element created:', element);
console.log('Element name:', element.name);
console.log('Element count:', element.count);