Home > Blockchain >  Jest testing Vuetify v-img
Jest testing Vuetify v-img

Time:11-23

I have a component in which I want to test the size of an v-img is being set, for the life of me I can not figure out what to hook into. I have tried grabbing the span tag that surrounds the code and then doing a > div to get the div that gets generated by vuetify so that I can then get the style attribute and test the size is set properly, but it doesnt seem to want to do that, when I add a .length to it it return 0 every time.

My Vue component (footer.vue)

    <template>
      <span >
        <v-img 
         :height="easg_logo_height"
         :src="$store.state.app.easg_logo.src"
         :width="easg_logo_width"
        contain
         />
     <v-img 
         :height="oma_logo_height"
         :src="$store.state.app.oma_logo.src"
         :width="oma_logo_width"
        contain
         />
       </div>
    </template>
<script>
   export default {
      data(){
         easg_logo_width: this.$store.state.app.easg_logo.top.width, 
         easg_logo_height: this.$store.state.app.easg_logo.top.height,
         oma_logo_width: this.$store.state.app.oma_logo.top.width,
         oma_logo_width: this.$store.state.app.oma_logo.top.width,
      }
   }
</script>

My test (footer.test.js)

import {shallowMount, createLocalVue} from '@vue/test-utils'
import Vuex from 'vuex';
import Footer from '@components/layouts/default/footer'
import Vuetify from 'vuetify';

const vuetify = new Vuetify();
const localVue = createLocalVue();
localVue.use(Vuex);

describe("Footer tests", ()=> {
  let wrapper;
  let store;
  let state;


beforeEach(() => {
   state= {
     app: {
        easg_logo:{
           src: "~/assets/images/easg.jpg",
           text: "EASG", 
           top:{
             height: 72,
             width: 82
           }
         },
    oma_logo:{
           src: "~/assets/images/oma.jpg",
           text: "OMA", 
           top:{
             height: 72,
             width: 82
           }
         }
      }
}

store = new Vuex.Store({
            modules:{
               state
            }
     })

})

test('store test', ()=> {
   wrapper = shallowMount(Footer, {store, localVue, vuetify})
   console.log(wrapper.findAll('.footerImagesLayout > div').length) // this returns 0
   const a = 'a'
    expect(a).toBe('a')
});

});

CodePudding user response:

You probably want mount instead of shallowMount, since v-img is not rendered but stubbed with shallowMount.

From the documentation: "Like mount, it (shallowMount) creates a Wrapper that contains the mounted and rendered Vue component, but with stubbed child components."

  • Related