Description
What problem does this feature solve?
I think it would be nice to be able to extend the Wrapper
class with new methods (helper methods, effectively) to increase readability / reduce test-specific boilerplate.
For example, using Jest, I've added some new matchers to keep things clean whilst testing components. Contrived example below:
test('renders a table cell', () => {
const component = shallow(MyComponent);
expect(component).toHaveElement('td');
});
It would be nice to be able to add similar helper methods to the Wrapper
object so that tests could be written at a slightly higher level of abstraction. See a before and after below:
// Before
test('hides icon on hover', () => {
const component = shallow(MyComponent);
component.trigger('mouseover');
expect(component.find('i').isVisible()).toBe(false);
});
// After
test('hides icon on hover', () => {
const component = shallow(MyComponent);
expect(component.hover().find('i').isVisible()).toBe(false);
});
// With both extended Wrapper and custom Jest matcher
test('hides icon on hover', () => {
const component = shallow(MyComponent);
expect(component.hover()).not.toHaveVisible('i');
});
Although the above examples are somewhat contrived for the sake of example, I think they lead to test code that more clearly documents the component under test.
What does the proposed API look like?
As for an example of how this might look in terms of the API, I'm not too sure; Wrapper is not exposed for reasons I think I agree with (from other issues on the topic). An exported function that provides this ability might work? I'm not much of a JS expert so please forgive me if I don't understand why the following example is unfeasible.
import { extend_wrapper } from "@vue/test-utils";
extend_wrapper({
hover() {
return this.trigger('mouseover');
}
});
I'm interested to hear thoughts on this, thanks for taking the time to read.