Helpful Properties, Functions, and Advanced Querying
Aurelia's testing utilities provide a rich set of properties and functions that go beyond basic component rendering. This section explores the advanced capabilities of the ComponentTester and auxiliary testing functions.
ComponentTester Properties
Core Properties
ComponentTester provides several key properties that help in component testing:
elementWhat it is: The root HTML element of the rendered component
Use cases:
Direct DOM manipulation checks
Accessing rendered component's root element
Verifying element attributes or classes
describe('Element Property', () => { let component; beforeEach(() => { component = StageComponent .withResources(PLATFORM.moduleName('my-component')) .inView('<my-component></my-component>'); }); it('provides access to the rendered element', done => { component.create(bootstrap).then(() => { // Check element existence expect(component.element).toBeTruthy(); // Verify element attributes expect(component.element.className).toContain('expected-class'); // Check element properties expect(component.element.tagName).toBe('DIV'); done(); }); }); });viewModelWhat it is: The actual instance of the component's view-model
Key benefits:
Direct access to component's internal state
Ability to call methods and check properties
Simulate interactions programmatically
describe('ViewModel Property', () => { let component; beforeEach(() => { class TestComponent { someMethod() { return 'test'; } @bindable propertyToTest; } component = StageComponent .withResources(PLATFORM.moduleName('test-component')) .inView('<test-component></test-component>') .boundTo(new TestComponent()); }); it('provides access to view-model methods and properties', done => { component.create(bootstrap).then(() => { // Call view-model methods expect(component.viewModel.someMethod()).toBe('test'); // Set and check bindable properties component.viewModel.propertyToTest = 'new value'; expect(component.viewModel.propertyToTest).toBe('new value'); done(); }); }); });
Lifecycle Control Methods
Lifecycle control methods allow precise management of component lifecycle:
manuallyHandleLifecycle()Purpose: Take full control of component lifecycle methods
Key benefits:
Step-by-step lifecycle testing
Precise timing control
Simulate different lifecycle scenarios
Key Lifecycle Methods
bind(): Manually trigger data bindingunbind(): Manually remove data bindingsattached(): Simulate component attachment to DOMdetached(): Simulate component removal from DOM
Element Waiting Utilities
waitForElement and waitForElements
waitForElement and waitForElementsWaiting utilities help manage asynchronous rendering:
waitForElementWaits for a single element to be present or absent
Configurable timeout and polling interval
Useful for testing async content loading
waitForElementsWaits for multiple elements
Similar configuration options to
waitForElement
Advanced Querying with waitFor
waitForFlexible Waiting Mechanism
waitForMost flexible waiting utility
Works with custom conditions
Supports complex querying scenarios
Last updated
Was this helpful?