I'm trying to make a unit test for my vue page but I was stucked in a button trigger test. The parent cannot trigger the click function
the button is a child component that accept props such as disabled, function and text
The parent called the button component and it was disabled if the checkbox value is false
Child
<div>
<b-button
:disabled= disabled
class="myButton"
@click="function"
{{text}}
</b-button>
</div>
Parent
<div>
<input v-model="check" type="checkbox" value="Ok">Agree
<div class="button-verification">
<Button
:disabled= !check
:text= "'Ok'"
:function= buttonFunction
/>
</div>
</div>
buttonFunction() {
router.push('/another-page')
}
The Unit test
import { mount, createLocalVue } from '@vue/test-utils'
import BootstrapVue from 'bootstrap-vue'
import Parent from '@/view/parent'
import Button from '@/component/child'
const localVue = createLocalVue()
localVue.use(BootstrapVue)
describe('test parent', () => {
it('Trigger button correctly', function () {
const wrapper = mount(Parent, {
localVue
})
wrapper.setData({ check: true })
const childComponent = wrapper.find(Button)
console.log('child', childComponent)
childComponent.trigger('click')
expect(router.history.current.path).toBe('/another-page')
})
})
If I used a normal button, the test will be successful, but I made the button component to be reusable so I need to test the click function using the button component
the console.log childComponent just log an empty wrapper
VueWrapper {
isFunctionalComponent: undefined,
_emitted: {},
_emittedByOrder: [] }