test: fix some tests

This commit is contained in:
Evan You 2018-11-08 12:54:11 -05:00
parent 157971b3cb
commit bd1b06f1cc
5 changed files with 25 additions and 43 deletions

View File

@ -12,7 +12,8 @@ import {
nextTick, nextTick,
resetOps, resetOps,
dumpOps, dumpOps,
NodeOpTypes NodeOpTypes,
renderInstance
} from '@vue/runtime-test' } from '@vue/runtime-test'
describe('Fragments', () => { describe('Fragments', () => {
@ -23,7 +24,7 @@ describe('Fragments', () => {
} }
} }
const root = nodeOps.createElement('div') const root = nodeOps.createElement('div')
await render(h(App), root) await renderInstance(App)
expect(serialize(root)).toBe(`<div><div>one</div>two</div>`) expect(serialize(root)).toBe(`<div><div>one</div>two</div>`)
expect(root.children.length).toBe(2) expect(root.children.length).toBe(2)
expect(root.children[0]).toMatchObject({ expect(root.children[0]).toMatchObject({

View File

@ -1,30 +1,8 @@
import { withHooks, useState, h, nextTick, useEffect, Component } from '../src' import { useState, h, nextTick, useEffect, Component } from '../src'
import { renderInstance, serialize, triggerEvent } from '@vue/runtime-test' import { renderInstance, serialize, triggerEvent } from '@vue/runtime-test'
describe('hooks', () => { describe('hooks', () => {
it('useState', async () => { it('useState', async () => {
const Counter = withHooks(() => {
const [count, setCount] = useState(0)
return h(
'div',
{
onClick: () => {
setCount(count + 1)
}
},
count
)
})
const counter = await renderInstance(Counter)
expect(serialize(counter.$el)).toBe(`<div>0</div>`)
triggerEvent(counter.$el, 'click')
await nextTick()
expect(serialize(counter.$el)).toBe(`<div>1</div>`)
})
it('should be usable inside class', async () => {
class Counter extends Component { class Counter extends Component {
render() { render() {
const [count, setCount] = useState(0) const [count, setCount] = useState(0)
@ -82,21 +60,23 @@ describe('hooks', () => {
it('useEffect', async () => { it('useEffect', async () => {
let effect = -1 let effect = -1
const Counter = withHooks(() => { class Counter extends Component {
const [count, setCount] = useState(0) render() {
useEffect(() => { const [count, setCount] = useState(0)
effect = count useEffect(() => {
}) effect = count
return h( })
'div', return h(
{ 'div',
onClick: () => { {
setCount(count + 1) onClick: () => {
} setCount(count + 1)
}, }
count },
) count
}) )
}
}
const counter = await renderInstance(Counter) const counter = await renderInstance(Counter)
expect(effect).toBe(0) expect(effect).toBe(0)

View File

@ -3,7 +3,7 @@ import { isFunction, isReservedKey } from '@vue/shared'
import { warn } from './warning' import { warn } from './warning'
import { isRendering } from './componentUtils' import { isRendering } from './componentUtils'
import { isObservable } from '@vue/observer' import { isObservable } from '@vue/observer'
import { reservedMethods } from '@vue/runtime-dom' import { reservedMethods } from './componentOptions'
const bindCache = new WeakMap() const bindCache = new WeakMap()

View File

@ -171,6 +171,7 @@ function patchJob(job: Job) {
if (job.ops.length === 0) { if (job.ops.length === 0) {
setCurrentOps(job.ops) setCurrentOps(job.ops)
job() job()
setCurrentOps(null)
commitQueue.push(job) commitQueue.push(job)
} }
} }

View File

@ -5,9 +5,9 @@ import { nodeOps as testNodeOps } from '../../runtime-test/src/nodeOps'
export type Op = [Function, ...any[]] export type Op = [Function, ...any[]]
let currentOps: Op[] let currentOps: Op[] | null = null
export function setCurrentOps(ops: Op[]) { export function setCurrentOps(ops: Op[] | null) {
currentOps = ops currentOps = ops
} }