I have a component:
<template>
<div class="toggle">
<div v-show="toggle">
{{ text }}
<slot :toggle="toggleSlots" name="first"></slot>
</div>
<div v-if="!toggle">
<slot :toggle="toggleSlots" name="second"></slot>
</div>
</div>
</template>
<script setup>
import { ref } from 'vue';
const toggle = ref(true);
const text = ref('hi');
function toggleSlots() {
toggle.value = !toggle.value;
}
setTimeout(() => {
text.value = 'hii';
}, 1000);
</script>
And a Jest test:
import { mount } from '@vue/test-utils';
import AppToggle from './toggle.vue';
describe('Toggle', () => {
it('should toggle the slots', async () => {
const wrapper = mount(AppToggle);
expect(wrapper.vm.toggle).toBeTruthy(); // vm.toggle is undefined
wrapper.vm.toggleSlots(); // vm.toggleSlots is undefined
expect(wrapper.vm.toggle).toBeFalsy();
});
});
When I wrote the component using the options API, the test passed. If I use the composition API, all variables or functions I define are not defined on wrapper.vm.
I found other examples where they did the same thing but it does not work for me somehow.