|
@@ -280,22 +280,29 @@ describe('api: watch', () => {
|
|
|
expect(cleanup).toHaveBeenCalledTimes(2)
|
|
expect(cleanup).toHaveBeenCalledTimes(2)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('flush timing: post (default)', async () => {
|
|
|
|
|
|
|
+ it('flush timing: pre (default)', async () => {
|
|
|
const count = ref(0)
|
|
const count = ref(0)
|
|
|
|
|
+ const count2 = ref(0)
|
|
|
|
|
+
|
|
|
let callCount = 0
|
|
let callCount = 0
|
|
|
- let result
|
|
|
|
|
- const assertion = jest.fn(count => {
|
|
|
|
|
|
|
+ let result1
|
|
|
|
|
+ let result2
|
|
|
|
|
+ const assertion = jest.fn((count, count2Value) => {
|
|
|
callCount++
|
|
callCount++
|
|
|
// on mount, the watcher callback should be called before DOM render
|
|
// on mount, the watcher callback should be called before DOM render
|
|
|
- // on update, should be called after the count is updated
|
|
|
|
|
- const expectedDOM = callCount === 1 ? `` : `${count}`
|
|
|
|
|
- result = serializeInner(root) === expectedDOM
|
|
|
|
|
|
|
+ // on update, should be called before the count is updated
|
|
|
|
|
+ const expectedDOM = callCount === 1 ? `` : `${count - 1}`
|
|
|
|
|
+ result1 = serializeInner(root) === expectedDOM
|
|
|
|
|
+
|
|
|
|
|
+ // in a pre-flush callback, all state should have been updated
|
|
|
|
|
+ const expectedState = callCount - 1
|
|
|
|
|
+ result2 = count === expectedState && count2Value === expectedState
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
const Comp = {
|
|
const Comp = {
|
|
|
setup() {
|
|
setup() {
|
|
|
watchEffect(() => {
|
|
watchEffect(() => {
|
|
|
- assertion(count.value)
|
|
|
|
|
|
|
+ assertion(count.value, count2.value)
|
|
|
})
|
|
})
|
|
|
return () => count.value
|
|
return () => count.value
|
|
|
}
|
|
}
|
|
@@ -303,42 +310,32 @@ describe('api: watch', () => {
|
|
|
const root = nodeOps.createElement('div')
|
|
const root = nodeOps.createElement('div')
|
|
|
render(h(Comp), root)
|
|
render(h(Comp), root)
|
|
|
expect(assertion).toHaveBeenCalledTimes(1)
|
|
expect(assertion).toHaveBeenCalledTimes(1)
|
|
|
- expect(result).toBe(true)
|
|
|
|
|
|
|
+ expect(result1).toBe(true)
|
|
|
|
|
+ expect(result2).toBe(true)
|
|
|
|
|
|
|
|
count.value++
|
|
count.value++
|
|
|
|
|
+ count2.value++
|
|
|
await nextTick()
|
|
await nextTick()
|
|
|
|
|
+ // two mutations should result in 1 callback execution
|
|
|
expect(assertion).toHaveBeenCalledTimes(2)
|
|
expect(assertion).toHaveBeenCalledTimes(2)
|
|
|
- expect(result).toBe(true)
|
|
|
|
|
|
|
+ expect(result1).toBe(true)
|
|
|
|
|
+ expect(result2).toBe(true)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- it('flush timing: pre', async () => {
|
|
|
|
|
|
|
+ it('flush timing: post', async () => {
|
|
|
const count = ref(0)
|
|
const count = ref(0)
|
|
|
- const count2 = ref(0)
|
|
|
|
|
-
|
|
|
|
|
- let callCount = 0
|
|
|
|
|
- let result1
|
|
|
|
|
- let result2
|
|
|
|
|
- const assertion = jest.fn((count, count2Value) => {
|
|
|
|
|
- callCount++
|
|
|
|
|
- // on mount, the watcher callback should be called before DOM render
|
|
|
|
|
- // on update, should be called before the count is updated
|
|
|
|
|
- const expectedDOM = callCount === 1 ? `` : `${count - 1}`
|
|
|
|
|
- result1 = serializeInner(root) === expectedDOM
|
|
|
|
|
-
|
|
|
|
|
- // in a pre-flush callback, all state should have been updated
|
|
|
|
|
- const expectedState = callCount - 1
|
|
|
|
|
- result2 = count === expectedState && count2Value === expectedState
|
|
|
|
|
|
|
+ let result
|
|
|
|
|
+ const assertion = jest.fn(count => {
|
|
|
|
|
+ result = serializeInner(root) === `${count}`
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
const Comp = {
|
|
const Comp = {
|
|
|
setup() {
|
|
setup() {
|
|
|
watchEffect(
|
|
watchEffect(
|
|
|
() => {
|
|
() => {
|
|
|
- assertion(count.value, count2.value)
|
|
|
|
|
|
|
+ assertion(count.value)
|
|
|
},
|
|
},
|
|
|
- {
|
|
|
|
|
- flush: 'pre'
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ { flush: 'post' }
|
|
|
)
|
|
)
|
|
|
return () => count.value
|
|
return () => count.value
|
|
|
}
|
|
}
|
|
@@ -346,16 +343,12 @@ describe('api: watch', () => {
|
|
|
const root = nodeOps.createElement('div')
|
|
const root = nodeOps.createElement('div')
|
|
|
render(h(Comp), root)
|
|
render(h(Comp), root)
|
|
|
expect(assertion).toHaveBeenCalledTimes(1)
|
|
expect(assertion).toHaveBeenCalledTimes(1)
|
|
|
- expect(result1).toBe(true)
|
|
|
|
|
- expect(result2).toBe(true)
|
|
|
|
|
|
|
+ expect(result).toBe(true)
|
|
|
|
|
|
|
|
count.value++
|
|
count.value++
|
|
|
- count2.value++
|
|
|
|
|
await nextTick()
|
|
await nextTick()
|
|
|
- // two mutations should result in 1 callback execution
|
|
|
|
|
expect(assertion).toHaveBeenCalledTimes(2)
|
|
expect(assertion).toHaveBeenCalledTimes(2)
|
|
|
- expect(result1).toBe(true)
|
|
|
|
|
- expect(result2).toBe(true)
|
|
|
|
|
|
|
+ expect(result).toBe(true)
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
it('flush timing: sync', async () => {
|
|
it('flush timing: sync', async () => {
|
|
@@ -410,7 +403,7 @@ describe('api: watch', () => {
|
|
|
const cb = jest.fn()
|
|
const cb = jest.fn()
|
|
|
const Comp = {
|
|
const Comp = {
|
|
|
setup() {
|
|
setup() {
|
|
|
- watch(toggle, cb)
|
|
|
|
|
|
|
+ watch(toggle, cb, { flush: 'post' })
|
|
|
},
|
|
},
|
|
|
render() {}
|
|
render() {}
|
|
|
}
|
|
}
|