How do I test a composable that uses onMounted and other injections. #1281
Answered
by
freakzlike
MarkTallentire
asked this question in
Q&A
-
Hi, I have this composable
As you can see it relies on some vue specific behaviours such as Watch and OnMounted. I have been unable to test this component due to this as it throws errors, so I'm unsure how I can test it, any tips/hints? |
Beta Was this translation helpful? Give feedback.
Answered by
freakzlike
Feb 1, 2022
Replies: 1 comment 3 replies
-
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).toBe(1)
}) For |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
cexbrayat
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: