Skip to content

How do I test a composable that uses onMounted and other injections. #1281

Answered by freakzlike
MarkTallentire asked this question in Q&A
Discussion options

You must be logged in to vote

Create a simple test component which only uses the composable you want to test. You can pass the parameters as props and return the results directly from the setup method.

Here a quick example:

import { defineComponent } from 'vue'

const TestComponent = defineComponent({
  props: {
    dataFetchFunction: {
      type: Function,
      required: true
    }
  },
  setup (props) {
    return usePagination(props.dataFetchFunction)
  }
})

it('should fetch data on mount', () => {
  const dataFetchFunction = jest.fn()
  const wrapper = mount(TestComponent, {
    props: {
      dataFetchFunction
    }
  })

  expect(dataFetchFunction).toHaveBeenCalledTimes(1)
  expect(wrapper.vm.pagination.page).t…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@ebisbe
Comment options

@freakzlike
Comment options

@ebisbe
Comment options

Answer selected by cexbrayat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants