| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import { BindingTypes, CompilerOptions, RootNode } from '@vue/compiler-dom'
- // TODO remove it
- import { format } from 'prettier'
- import { compile as _compile } from '../src'
- async function compile(
- template: string | RootNode,
- options: CompilerOptions = {},
- ) {
- let { code } = _compile(template, options)
- code = await format(code, {
- parser: 'babel',
- printWidth: 999999,
- singleQuote: true,
- })
- return code
- }
- describe('compile', () => {
- test('static template', async () => {
- const code = await compile(
- `<div>
- <p>hello</p>
- <input />
- <span />
- </div>`,
- )
- expect(code).matchSnapshot()
- })
- test('dynamic root', async () => {
- const code = await compile(`{{ 1 }}{{ 2 }}`)
- expect(code).matchSnapshot()
- })
- test('dynamic root nodes and interpolation', async () => {
- const code = await compile(
- `<button @click="handleClick" :id="count">{{count}}foo{{count}}foo{{count}} </button>`,
- )
- expect(code).matchSnapshot()
- })
- test('static + dynamic root', async () => {
- const code = await compile(
- `{{ 1 }}{{ 2 }}3{{ 4 }}{{ 5 }}6{{ 7 }}{{ 8 }}9{{ 'A' }}{{ 'B' }}`,
- )
- expect(code).matchSnapshot()
- })
- test('fragment', async () => {
- const code = await compile(`<p/><span/><div/>`)
- expect(code).matchSnapshot()
- })
- test('bindings', async () => {
- const code = await compile(`<div>count is {{ count }}.</div>`, {
- bindingMetadata: {
- count: BindingTypes.SETUP_REF,
- },
- })
- expect(code).matchSnapshot()
- })
- describe('directives', () => {
- describe('v-bind', () => {
- test('simple expression', async () => {
- const code = await compile(`<div :id="id"></div>`, {
- bindingMetadata: {
- id: BindingTypes.SETUP_REF,
- },
- })
- expect(code).matchSnapshot()
- })
- })
- describe('v-on', () => {
- test('simple expression', async () => {
- const code = await compile(`<div @click="handleClick"></div>`, {
- bindingMetadata: {
- handleClick: BindingTypes.SETUP_CONST,
- },
- })
- expect(code).matchSnapshot()
- })
- })
- describe('v-html', () => {
- test('simple expression', async () => {
- const code = await compile(`<div v-html="code"></div>`, {
- bindingMetadata: {
- code: BindingTypes.SETUP_REF,
- },
- })
- expect(code).matchSnapshot()
- })
- test('no expression', async () => {
- const code = await compile(`<div v-html></div>`)
- expect(code).matchSnapshot()
- })
- })
- describe('v-text', () => {
- test('simple expression', async () => {
- const code = await compile(`<div v-text="str"></div>`, {
- bindingMetadata: {
- str: BindingTypes.SETUP_REF,
- },
- })
- expect(code).matchSnapshot()
- })
- test('no expression', async () => {
- const code = await compile(`<div v-text></div>`)
- expect(code).matchSnapshot()
- })
- })
- describe('v-once', () => {
- test('basic', async () => {
- const code = await compile(
- `<div v-once>
- {{ msg }}
- <span :class="clz" />
- </div>`,
- {
- bindingMetadata: {
- msg: BindingTypes.SETUP_REF,
- clz: BindingTypes.SETUP_REF,
- },
- },
- )
- expect(code).matchSnapshot()
- })
- test.fails('as root node', async () => {
- const code = await compile(`<div :id="foo" v-once />`)
- expect(code).toMatchSnapshot()
- expect(code).not.contains('effect')
- })
- })
- })
- })
|