0

When i am learning the vue-test-utils from the offical site conditional-rendering.

I tried to change the option api to composition api. It seems like the mount option data not working with the composition api. enter image description here

Nav.vue Composition API test FAIL

<template>
  <div>
    <a id="profile" href="/profile">My Profile</a>
    <a v-if="admin" id="admin" href="/admin">Admin</a>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const admin = ref(false)
</script>

Nav.vue Option API test PASS

<template>
  <div>
    <a id="profile" href="/profile">My Profile</a>
    <a v-if="admin" id="admin" href="/admin">Admin</a>
  </div>
</template>

<script>
export default {
  data() {
    return {
      admin: false,
    }
  },
}
</script>

Nav.spec.js test

test('renders an admin link', () => {
  const wrapper = mount(Nav, {
    data() {
      return {
        admin: true
      }
    }
  })

  // Again, by using `get()` we are implicitly asserting that
  // the element exists.
  expect(wrapper.get('#admin').text()).toEqual('Admin')
})
1

1 Answer 1

1

I found a solution but I don't know if it's a good solution

test("renders an admin link", async () => {
   const wrapper = mount(Nav);
   wrapper.vm.admin = true;
   await nextTick();

   expect(wrapper.get("#admin").text()).toEqual("Admin");
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.