2

After a number of days hitting a wall, I understood that testing functional components with vue-test-utils causes some issues

To summarize, I am using Bootstrap-Vue's B-Button with a @click event on it that calls some function/method. When I try to run a test to understand whether the method is called, the test fails. However, when I swap the functional B-Buttonwith a <button> the test passes.

Here is JobSearch.vue component

<template>
  <b-input-group>
    <b-form-input
      class="mr-2 rounded-0"
      placeholder="Enter Search term..."
      id="input-keyword"
    />
<!--    <b-button-->
<!--      @click="searchJobs"-->
<!--      class="rounded-0 ml-2"-->
<!--      variant="primary"-->
<!--      id="reset-button"-->
<!--    >-->
<!--      Reset-->
<!--    </b-button>-->

    <button
      @click="resetFilter"
      class="rounded-0 ml-2"
      id="reset-button">
      Reset
    </button>
  </b-input-group>
</template>
<script>
export default {
  name: 'job-search-test',
  methods: {
    async searchJobs () {
      console.log('Calling Search Jobs from JobsSearchTest')
    },
    resetFilter () {
      console.log('Clicked On Reset')
    }
  },
  mounted () {
    // this.searchJobs()
  }
}
</script>

Here is JobSearch.spec.js

import { shallowMount, createLocalVue } from '@vue/test-utils'
import BootstrapVue from 'bootstrap-vue'
import JobSearchTest from '@/components/jobs/JobSearchTest'

const localVue = createLocalVue()
localVue.use(BootstrapVue)

describe('JobsSearchTest.vue', () => {
  it('should call searchJobs method when component is mounted', () => {
    let searchJobs = jest.fn()

    shallowMount(JobSearchTest, {
      methods: {
        searchJobs
      },
      localVue })
    expect(searchJobs).toHaveBeenCalledTimes(1)
  })

  it('should call resetFilter method on reset button click event', () => {
    let resetFilter = jest.fn()
    const wrapper = shallowMount(JobSearchTest, {
      methods: {
        resetFilter
      },
      localVue
    })
    expect(resetFilter).not.toHaveBeenCalled()
    wrapper.find('#reset-button').trigger('click')
    console.log(wrapper.find('#reset-button'))
    expect(resetFilter).toHaveBeenCalled()
  })
})

By commenting out the <b-button> part and commenting the <button> the test fails, however, it will be nice if it can pass since for this project we would like to use Bootstrap Vue.

Any ideas for a work around, which would not take away the value of the test? For example, according to an earlier question I asked, functional components don't operate well with directives, hence by using a non-functional stub the directive works fine, however, this takes away the value of the test.

Use more than one directive to add data attributes to components

3
  • Have you tried to wrap the b-button with the <b-button-group> tag? Commented Sep 12, 2019 at 14:56
  • @Skyentao I haven't tried this in particular but I tried stubbing the b-button inside shallowMount which worked. However, I then spoke with someone on Vue Land Discord channel and he advised to not use shallowMount. Shallow mount is really only good when you are testing a component and not any children... and since b-button is a child component... I need to bring it in Commented Sep 12, 2019 at 15:52
  • You can start using the boostrap-vue boilerplate for this case bootstrap-vue.js.org/docs/components/button-group Commented Sep 12, 2019 at 23:28

1 Answer 1

3

As far as I understood there are two answers to this.

When using shallowMount the functional components should be stubbed when creating the wrapper.

The other solution is to use mount. Shallow mount is really only good when only the component is tested in isolation. Here I am testing children... and since b-button is a child component... I need to bring it in

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.