Testing Custom Attributes
Custom attributes in Aurelia are powerful ways to extend HTML elements with additional behavior. Testing these attributes requires a systematic approach to verify their functionality, binding, and interactions.
Basic Custom Attribute Test Structure
Simple Attribute Implementation
export class HighlightAttribute {
static inject = [Element];
constructor(element) {
this.element = element;
}
valueChanged(newValue) {
this.element.style.backgroundColor = newValue;
}
}Comprehensive Attribute Testing
import {StageComponent} from 'aurelia-testing';
import {bootstrap} from 'aurelia-bootstrapper';
describe('Highlight Attribute', () => {
let component;
beforeEach(() => {
component = StageComponent
.withResources(PLATFORM.moduleName('highlight'))
.inView('<div highlight.bind="color">Test Content</div>')
.boundTo({ color: 'yellow' });
});
it('should apply background color', done => {
component.create(bootstrap).then(() => {
const element = component.element;
expect(element.style.backgroundColor).toBe('yellow');
done();
}).catch(done.fail);
});
afterEach(() => {
component.dispose();
});
});Advanced Attribute Testing Scenarios
Dynamic Attribute Behavior
Attribute with Multiple Parameters
Lifecycle and Interaction Testing
Attribute Lifecycle Methods
Testing Strategies and Best Practices
Test initial attribute application
Verify dynamic updates
Check lifecycle method invocations
Test edge cases and different input types
Ensure proper cleanup and disposal
Mock external dependencies
Use meaningful assertions
Common Pitfalls to Avoid
Don't test implementation details
Avoid over-complex test scenarios
Ensure proper async handling
Clean up DOM between tests
Be cautious of timing-related tests
Use meaningful and descriptive test names
Last updated
Was this helpful?