import { ElementNode, ErrorCodes } from '@vue/compiler-core'
import { compile } from '../src'
import { CompilerOptions } from '../src/options'
import { assert } from './testUtils'
function parseWithVOn(template: string, options: CompilerOptions = {}) {
const { ast } = compile(template, {
generatorOpts: {
concise: true,
},
...options,
})
return {
root: ast,
node: ast.children[0] as ElementNode,
}
}
describe('compiler: transform v-on', () => {
test('basic', () => {
assert(
``,
``,
`(_ctx, _cache) => {
return { a: _o(_ctx.onClick) }
}`
)
})
test('dynamic arg', () => {
//
})
test('dynamic arg with prefixing', () => {
//
})
test('dynamic arg with complex exp prefixing', () => {
//
})
test('should wrap as function if expression is inline statement', () => {
assert(
``,
``,
`(_ctx, _cache) => {
return { a: _o($event => _ctx.i++) }
}`
)
})
test('should handle multiple inline statement', () => {
assert(
``,
``,
`(_ctx, _cache) => {
return { a: _o($event => { _ctx.foo(); _ctx.bar(); }) }
}`
)
})
test('should handle multi-line statement', () => {
assert(
``,
``,
`(_ctx, _cache) => {
with (_ctx) {
const { o: _o } = _Vue
return { a: _o($event => { foo(); bar(); }) }
}
}`,
{ prefixIdentifiers: false, mode: 'function' }
)
})
test('inline statement w/ prefixIdentifiers: true', () => {
assert(
``,
``,
`(_ctx, _cache) => {
return { a: _o($event => _ctx.foo($event)) }
}`
)
})
test('multiple inline statements w/ prefixIdentifiers: true', () => {
assert(
``,
``,
`(_ctx, _cache) => {
return { a: _o($event => { _ctx.foo($event); _ctx.bar(); }) }
}`
)
})
test('should NOT wrap as function if expression is already function expression', () => {
assert(
` foo($event)"/>`,
``,
`(_ctx, _cache) => {
return { a: _o($event => _ctx.foo($event)) }
}`
)
})
test('should NOT wrap as function if expression is already function expression (with newlines)', () => {
assert(
` {
foo($event)
}
"/>`,
``,
`(_ctx, _cache) => {
return { a: _o($event => { _ctx.foo($event); }) }
}`
)
})
test('should NOT wrap as function if expression is already function expression (with newlines + function keyword)', () => {
assert(
``,
``,
`(_ctx, _cache) => {
return { a: _o(function ($event) { _ctx.foo($event); }) }
}`
)
})
test('should NOT wrap as function if expression is complex member expression', () => {
assert(
``,
``,
`(_ctx, _cache) => {
with (_ctx) {
const { o: _o } = _Vue
return { a: _o(a['b' + c]) }
}
}`,
{
prefixIdentifiers: false,
mode: 'function',
}
)
})
test('complex member expression w/ prefixIdentifiers: true', () => {
assert(
``,
``,
`(_ctx, _cache) => {
return { a: _o(_ctx.a['b' + _ctx.c]) }
}`
)
})
test('function expression w/ prefixIdentifiers: true', () => {
assert(
` foo(e)"/>`,
``,
`(_ctx, _cache) => {
return { a: _o(e => _ctx.foo(e)) }
}`
)
})
test('should error if no expression AND no modifier', () => {
const onError = jest.fn()
parseWithVOn(``, { onError })
expect(onError.mock.calls[0][0]).toMatchObject({
code: ErrorCodes.X_V_ON_NO_EXPRESSION,
loc: {
start: {
line: 1,
column: 6,
},
end: {
line: 1,
column: 16,
},
},
})
})
test('should NOT error if no expression but has modifier', () => {
const onError = jest.fn()
parseWithVOn(``, { onError })
expect(onError).not.toHaveBeenCalled()
})
test('case conversion for kebab-case events', () => {
assert(
``,
``,
`(_ctx, _cache) => {
return { a: _o(_ctx.onMount) }
}`
)
})
test('case conversion for vnode hooks', () => {
assert(
``,
``,
`(_ctx, _cache) => {
return { a: _o(_ctx.onMount) }
}`
)
})
describe('cacheHandler', () => {
test('empty handler', () => {
assert(
``,
``,
`(_ctx, _cache) => {
return { a: _o(() => {}) }
}`
)
})
test('member expression handler', () => {
//
})
test('compound member expression handler', () => {
//
})
test('bail on component member expression handler', () => {
//
})
test('should not be cached inside v-once', () => {
//
})
test('inline function expression handler', () => {
//
})
test('inline async arrow function expression handler', () => {
//
})
test('inline async function expression handler', () => {
//
})
test('inline statement handler', () => {
//
})
})
})