Testing Methods, Filters and Mixins
Since filters and mixins generate their output based solely on function parameters, they are straightforward to unit test. It is not recommended to test methods unless it's strictly necessary since the user doesn't call methods on the component directly. The users see the rendered UI, and their interactions with the application are manifested as events (for example, click
, input change
, focus change
, and scroll
).
For example, a filter that truncates its input to eight characters would be implemented as follows:
<script> export default { filters: { truncate(value) { return value && value.slice(0, 8) } } } </script>
There are two options to test it. We could test it directly by importing the component and calling truncate
on some input, as per the truncate.test.js
file:
import PostListItem from ...