-
-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Useful patterns #12
Comments
I would be interested in an example of testing. Is it for example possible to do shallow testing of a component using |
If you use // could also be process.nextTick(r)
const tick = () => new Promise(r => setTimeout(r, 0))
it('test something', async () => {
wrapper.setData({ promise: Promise.resolve('data') })
await tick()
expect(wrapper.text()).toBe('data')
}) |
I would love an example of fetching new data using a computed property, I'm working on that now but having some trouble with it. The problem is that whenever I fetch the new data it wipes out the old data before the new is populated. Edit: I figured it out, so I'll offer up my pattern here for anyone who wants it and for use in the docs if you'd like. This is using the composition API export default defineComponent({
setup() {
//keep the page number in a ref
const page = ref(1)
//get the data in a computed so that if the page changes it re-fetches the data
const myDataPromise = computed(() => {
return usePromise(fetchMyData(page.value))
})
//do not actually expose the data from the promise
const myData = ref(null)
watch(myDataPromise, (dataPromise) => {
//instead, update a ref only when the promise has finished fetching
if(!dataPromise.isPending.value) {
myData.value = dataPromise.data.value
}
}, { deep: true })
//and expose the ref
return { myData }
}
}) |
After #11
I want to add some patterns that I have found useful by showing real examples with comments. Here are some of them:
this.promise
Promised
If you have other suggestions or ways you have used vue-promised that could be useful, please share! 😄
The text was updated successfully, but these errors were encountered: