Testing attributes
Testing custom attributes in Aurelia is analogous to testing components, with the primary difference being that custom attributes do not have a view template. They are, however, responsible for modifying the behavior or appearance of existing DOM elements. By leveraging the same testing techniques used for components, we can effectively validate the functionality of our custom attributes.
Example Custom Attribute
Let's consider a ColorSquareCustomAttribute
that we previously created. This attribute applies a color border and sets the size of an element, resulting in a square of uniform dimensions with a colored background.
Testing the Custom Attribute
We will now write tests to ensure that the ColorSquareCustomAttribute
behaves as expected when applied to an element.
Test Setup
Before writing tests, ensure that your test environment is properly configured. The setup for testing custom attributes is the same as for components, so refer to the Overview section.
Writing Tests
Create a test file for your custom attribute, such as color-square.spec.ts
, and use the following example as a guide:
In the first test, we verify that the default size and color are applied to an element when the custom attribute is used without any bindings. In the second test, we bind the color and size properties and then change the color to ensure the colorChanged
method updates the element's style as expected.
Testing Approach
As with components, we use createFixture
to set up our test environment. The first argument is the HTML view where we use our custom attribute. The second argument is the view model, which can define values to bind in our view model if needed. The third argument specifies any dependencies required by our tests, such as custom elements, value converters, or attributes.
Conclusion
Testing custom attributes in Aurelia 2 is essential to ensure they correctly manipulate DOM elements as intended. By setting up a proper testing environment, creating fixtures, and writing assertions, we can confidently verify that our custom attributes perform their duties correctly. Always remember to clean up your tests to maintain a pristine testing state.
Last updated