in my component the submit btn is disabled until a value is entered in the input field. however after mounting the component, it seems that the component is not yet stable and validation did not run, so the btn is not disabled and its text is 'set status'. so the test fails with:
Expected value to equal:
"set status disabled"
Received:
"set status"
expected is that the button is disabled and has the text 'set status disabled'.
in the browser the behaviour is correct.
HelloWorld.vue
<template>
<div class="hello">
<ValidationObserver>
<form slot-scope="{ invalid }" @submit.prevent="postStatus">
<ValidationProvider name="email" rules="required">
<div slot-scope="{ errors }">
<input v-model="status">
<p>{{ errors[0] }}</p>
</div>
</ValidationProvider>
<button id="submitBtn" @click="postStatus" :disabled="invalid" type="submit">set status {{invalid? 'disabled' :''}} </button>
</form>
</ValidationObserver>
</div>
<script lang="ts">
import {Component, Prop, Vue} from 'vue-property-decorator';
import {Action} from 'vuex-class';
import {ValidationProvider, ValidationObserver} from 'vee-validate';
@Component({
components: {
ValidationProvider, ValidationObserver
},
})
export default class HelloWorld extends Vue {
status: string = '';
@Action('setStatus') private setStatus: any;
public postStatus() {
this.setStatus(this.status);
}
}
HelloWorld.spec.ts
import HelloWorld from '@/components/HelloWorld.vue';
import Vuex from "vuex"
import {mount, createLocalVue} from "@vue/test-utils"
import {ValidationObserver, ValidationProvider} from "vee-validate";
import VeeValidate from 'vee-validate';
const localVue = createLocalVue()
localVue.use(Vuex)
localVue.use(VeeValidate, {
events: 'blur'
});
describe('HelloWorld.vue', () => {
it('button should be initially disabled', () => {
const wrapper = mount(HelloWorld, {
components: {ValidationObserver, ValidationProvider}
});
let submitBtn = wrapper.find("#submitBtn");
expect(submitBtn).toBeTruthy();
expect(submitBtn.text()).toEqual('set status disabled')
});
});
invalid
field. However, I can't see it on the component. Did you forget to copy/paste theinvalid
field? – Sergeon