Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to mock this.$refs.form.validate in vue

Tags:

vue.js

jestjs

I have a vue code where i do some actions based on this.$refs.form.validate I wanted to write a test for it ...But not sure how can i mock this.$refs.form.validate? I have written only basic ones..can someone point me at right direction?I am using Vue+Jest

methods: {
        sayHello () {

          if (this.$refs.form.validate()) {
            //code goes here
    }

Is there a way to make it return false and true?

like image 716
Janier Avatar asked Oct 28 '25 06:10

Janier


1 Answers

create a stub

const VueFormStub = {
 render: () => {},
 methods: {
  validate: () => {}
 }
}

then in your wrapper add it like this

const wrapper = shallowMount(VueFile, {
 stubs: {
  'v-form': VueFormStub
 }
})
like image 103
Jaye Tan Avatar answered Oct 31 '25 03:10

Jaye Tan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!