Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
How to verify modal window is displayed or not

I'm a newbie of Vuejs.

In vue2, I already have verified whether component is displayed by specific trigger.
However, I cannot verify whether above displayed component is disappeared after another trigger.

I tried to verify wrapper.findComponent('...').exists() is false.
However, I got true when component should be disappeared.

At first, I suspect another trigger does not work well,
so I called wrapper.html(), and there is no component that I want to verify.
That's why, trigger works well, probably.

My question is, as I said at title, How to verify component existance when flag is toggled.

Below is my code.

code for test.

  test('After date pick, picker is closed', async() => {
    let node = document.createElement('body')
    const wrapper = mount(App, {
      localVue,
      vuetify,
      attachTo: node
    })
    
    // modal window does not appear yet.
    expect(wrapper.findComponent('.v-picker').exists()).toBe(false)    
    
    // `enable` is set to true. Then, modal window is displayed.
    // and verification got true
    const menu = wrapper.getComponent(DateMenu)
    await menu.setData({ enable: true })
    expect(wrapper.findComponent('.v-picker').exists()).toBe(true)    

    // $emit input event cause `enable`  set to false
    const picker = wrapper.getComponent('.v-picker')
    await picker.vm.$emit('input', '2020-01-01')    
    
    // html() returns result in which there is no elements related to toggled component
    console.log(wrapper.html())
    expect(menu.vm.$data.enable).toBe(false)   

    // test fail findComponent found a component   
    expect(wrapper.findComponent('.v-picker').exists()).toBe(false)
  })

As reference, source code (short version).

<template>
  <!-- v-model="enable" controls modal window is displayed or not-->
  <v-menu v-model="enable">
    <template v-slot:activator="{ on, attrs }">
      <v-text-field
        v-model="date"
        v-bind="attrs"
        v-on="on"
      >
      </v-text-field>      
    </template>

    <!-- This is displayed and verified existance in test. -->
    <v-date-picker v-model="date" @input="input"> </v-date-picker>
  </v-menu>
</template>
<script>
export default {
  data: () => {
    return {
      // enable controls modal window is displayed or not. 
      //  true: displayed
      // false: disappear
      enable: false, 
    };
  },
  methods: {
    input(date) {        
      this.enable = false
    }
  }
};
</script>
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I think in your test, you encounter an update applied by Vue, which is asynchronous behavior. So after changing the reactive property (enable = false), you should wait for Vue to update DOM by adding this line:

await Vue.nextTick();
expect(wrapper.findComponent('.v-picker').exists()).toBe(false);
about 4 years ago · Juan Pablo Isaza Relatório

0

Vuetify renders its dialogs, menus, pickers (etc.) outside of the application wrapper, that's why wrapper.html() doesn't help you.

I believe the correct way is to use attachTo option, so you can mount your component inside the div with the data-app attribute.

If that doesn't work for you, you can also set data-app attribute on body:

let node = document.createElement('body');
node.setAttribute("data-app", true);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda