Let's assume a basic Bootstrap driven HTML form as part of a custom Vue component MyForm.vue
<template>
<form>
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</template>
A unit test for testing if the template is rendered successfully is pretty simple
describe('MyForm', () => {
let wrapper;
beforeEach(...);
it('Should be rendered', () => {
let field = wrapper.find('#email');
expect(field.element.value).toEqual('');
});
});
This line works field.element.value works because field.element is of native type HtmlInputElement with value attribute.
What if I want access an attribute of a complex component let's say of b-form-input, the Bootstrap-Vue's default input element? b-form-input is of type BFormInput how to deal with it? Just cast the HtmlElement to BFormInput?
<template>
<b-form>
<b-form-group label="Email">
<b-form-input type="email" id="email"></b-form-input>
</b-form-group>
<b-button type="submit" variant="primary">Submit</button>
</b-form>
</template>
How to test non-native components? Specially with type-safety means TypeScript. Any ideas?
Edit 03/01/2020
Following up to muka.gergely's answer I found this article. shallowMount is stubbing all child components by default which prevents also event handling. Moreover shallowMount allows to manually unstub components, in my case to unstub b-form and b-button for submit event testing.
const stubs = { // Originally intended to provide custom stubs, here used to unstub components
BButton,
BForm,
};
wrapper = shallowMount<MyComponent>(MyComponent, {stubs});
This effects that these components are rendered instead of stubbed. All remaining components like the b-form-input are still automatically stubbed.