Testing Custom Elements
Basic Component Test
import {StageComponent} from 'aurelia-testing';
import {bootstrap} from 'aurelia-bootstrapper';
describe('UserProfile Component', () => {
let component;
// Custom element to be tested
class UserProfile {
@bindable firstName;
@bindable lastName;
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
}
beforeEach(() => {
// Stage the component for testing
component = StageComponent
.withResources(PLATFORM.moduleName('user-profile'))
.inView('<user-profile first-name.bind="firstName" last-name.bind="lastName"></user-profile>')
.boundTo({
firstName: 'John',
lastName: 'Doe'
});
});
it('should render full name correctly', done => {
component.create(bootstrap).then(() => {
const nameElement = document.querySelector('.full-name');
expect(nameElement.textContent).toBe('John Doe');
done();
}).catch(done.fail);
});
afterEach(() => {
component.dispose();
});
});Detailed Binding and Property Tests
Lifecycle Method Testing
Complex Binding Scenarios
Best Practices for Component Testing
Common Pitfalls to Avoid
Last updated
Was this helpful?