2019-11-15 00:50:13 +08:00
|
|
|
import { parse } from '../src'
|
|
|
|
import { mockWarn } from '@vue/runtime-test'
|
2019-12-23 08:44:21 +08:00
|
|
|
import { baseParse, baseCompile } from '@vue/compiler-core'
|
2019-11-15 00:50:13 +08:00
|
|
|
|
|
|
|
describe('compiler:sfc', () => {
|
|
|
|
mockWarn()
|
2019-11-19 22:10:59 +08:00
|
|
|
|
2019-11-29 04:21:02 +08:00
|
|
|
describe('source map', () => {
|
|
|
|
test('style block', () => {
|
|
|
|
const style = parse(`<style>\n.color {\n color: red;\n }\n</style>\n`)
|
2019-12-23 08:44:21 +08:00
|
|
|
.descriptor.styles[0]
|
2019-11-29 04:22:30 +08:00
|
|
|
// TODO need to actually test this with SourceMapConsumer
|
2019-11-29 04:21:02 +08:00
|
|
|
expect(style.map).not.toBeUndefined()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('script block', () => {
|
2019-12-23 08:44:21 +08:00
|
|
|
const script = parse(`<script>\nconsole.log(1)\n }\n</script>\n`)
|
|
|
|
.descriptor.script
|
2019-11-29 04:22:30 +08:00
|
|
|
// TODO need to actually test this with SourceMapConsumer
|
2019-11-29 04:21:02 +08:00
|
|
|
expect(script!.map).not.toBeUndefined()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-12-02 23:43:30 +08:00
|
|
|
test('pad content', () => {
|
|
|
|
const content = `
|
|
|
|
<template>
|
|
|
|
<div></div>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
export default {}
|
|
|
|
</script>
|
|
|
|
<style>
|
|
|
|
h1 { color: red }
|
|
|
|
</style>`
|
2019-12-23 08:44:21 +08:00
|
|
|
const padFalse = parse(content.trim(), { pad: false }).descriptor
|
2019-12-02 23:43:30 +08:00
|
|
|
expect(padFalse.template!.content).toBe('\n<div></div>\n')
|
|
|
|
expect(padFalse.script!.content).toBe('\nexport default {}\n')
|
|
|
|
expect(padFalse.styles[0].content).toBe('\nh1 { color: red }\n')
|
|
|
|
|
2019-12-23 08:44:21 +08:00
|
|
|
const padTrue = parse(content.trim(), { pad: true }).descriptor
|
2019-12-02 23:43:30 +08:00
|
|
|
expect(padTrue.script!.content).toBe(
|
|
|
|
Array(3 + 1).join('//\n') + '\nexport default {}\n'
|
|
|
|
)
|
|
|
|
expect(padTrue.styles[0].content).toBe(
|
|
|
|
Array(6 + 1).join('\n') + '\nh1 { color: red }\n'
|
|
|
|
)
|
|
|
|
|
2019-12-23 08:44:21 +08:00
|
|
|
const padLine = parse(content.trim(), { pad: 'line' }).descriptor
|
2019-12-02 23:43:30 +08:00
|
|
|
expect(padLine.script!.content).toBe(
|
|
|
|
Array(3 + 1).join('//\n') + '\nexport default {}\n'
|
|
|
|
)
|
|
|
|
expect(padLine.styles[0].content).toBe(
|
|
|
|
Array(6 + 1).join('\n') + '\nh1 { color: red }\n'
|
|
|
|
)
|
|
|
|
|
2019-12-23 08:44:21 +08:00
|
|
|
const padSpace = parse(content.trim(), { pad: 'space' }).descriptor
|
2019-12-02 23:43:30 +08:00
|
|
|
expect(padSpace.script!.content).toBe(
|
|
|
|
`<template>\n<div></div>\n</template>\n<script>`.replace(/./g, ' ') +
|
|
|
|
'\nexport default {}\n'
|
|
|
|
)
|
|
|
|
expect(padSpace.styles[0].content).toBe(
|
|
|
|
`<template>\n<div></div>\n</template>\n<script>\nexport default {}\n</script>\n<style>`.replace(
|
|
|
|
/./g,
|
|
|
|
' '
|
|
|
|
) + '\nh1 { color: red }\n'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2019-11-19 22:10:59 +08:00
|
|
|
test('should ignore nodes with no content', () => {
|
2019-12-23 08:44:21 +08:00
|
|
|
expect(parse(`<template/>`).descriptor.template).toBe(null)
|
|
|
|
expect(parse(`<script/>`).descriptor.script).toBe(null)
|
|
|
|
expect(parse(`<style/>`).descriptor.styles.length).toBe(0)
|
|
|
|
expect(parse(`<custom/>`).descriptor.customBlocks.length).toBe(0)
|
2019-11-19 22:10:59 +08:00
|
|
|
})
|
|
|
|
|
2019-12-23 08:44:21 +08:00
|
|
|
test('nested templates', () => {
|
|
|
|
const content = `
|
|
|
|
<template v-if="ok">ok</template>
|
|
|
|
<div><div></div></div>
|
|
|
|
`
|
|
|
|
const sfc = parse(`<template>${content}</template>`).descriptor
|
|
|
|
expect(sfc.template!.content).toBe(content)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('error tolerance', () => {
|
|
|
|
const { errors } = parse(`<template>`)
|
|
|
|
expect(errors.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should parse as DOM by default', () => {
|
|
|
|
const { errors } = parse(`<template><input></template>`)
|
|
|
|
expect(errors.length).toBe(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('custom compiler', () => {
|
|
|
|
const { errors } = parse(`<template><input></template>`, {
|
|
|
|
compiler: {
|
|
|
|
parse: baseParse,
|
|
|
|
compile: baseCompile
|
|
|
|
}
|
|
|
|
})
|
|
|
|
expect(errors.length).toBe(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('warnings', () => {
|
2019-11-15 00:50:13 +08:00
|
|
|
test('should only allow single template element', () => {
|
|
|
|
parse(`<template><div/></template><template><div/></template>`)
|
|
|
|
expect(
|
|
|
|
`Single file component can contain only one template element`
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should only allow single script element', () => {
|
|
|
|
parse(`<script>console.log(1)</script><script>console.log(1)</script>`)
|
|
|
|
expect(
|
|
|
|
`Single file component can contain only one script element`
|
|
|
|
).toHaveBeenWarned()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|