Skip to content

Commit

Permalink
feat: export nextTick
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 22, 2020
1 parent 58d8d9a commit 391a102
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -21,6 +21,7 @@ if (currentVue && typeof window !== 'undefined' && window.Vue) {
}

export default plugin
export { nextTick } from './nextTick'
export { default as createElement } from './createElement'
export { SetupContext }
export {
Expand Down
13 changes: 13 additions & 0 deletions src/nextTick.ts
@@ -0,0 +1,13 @@
import Vue from 'vue'
import { currentVM, currentVue } from './runtimeContext'

type NextTick = Vue['$nextTick']

export const nextTick: NextTick = function nextTick(...args: any) {
if (currentVM) {
return currentVM.$nextTick.apply(currentVM, args)
} else {
// @ts-ignore
return currentVue?.nextTick(...args)
}
} as any
29 changes: 29 additions & 0 deletions test/misc.spec.js
@@ -0,0 +1,29 @@
const Vue = require('vue/dist/vue.common.js')
const { ref, nextTick } = require('../src')

describe('nextTick', () => {
it('should works', () => {
const vm = new Vue({
template: `<div>{{a}}</div>`,
setup() {
return {
a: ref(1),
}
},
}).$mount()

expect(vm.$el.textContent).toBe('1')
vm.a = 2
expect(vm.$el.textContent).toBe('1')

nextTick(() => {
expect(vm.$el.textContent).toBe('2')
vm.a = 3
expect(vm.$el.textContent).toBe('2')

nextTick(() => {
expect(vm.$el.textContent).toBe('3')
})
})
})
})

0 comments on commit 391a102

Please sign in to comment.