import { BindingTypes } from '@vue/compiler-core' import { compileSFCScript as compile, assertCode } from './utils' describe('SFC compile `) assertCode(content) expect(content).toMatch('return { a, b, c, d, x }') }) test('defineProps()', () => { const { content, bindings } = compile(` `) // should generate working code assertCode(content) // should anayze bindings expect(bindings).toStrictEqual({ foo: BindingTypes.PROPS, bar: BindingTypes.SETUP_CONST, props: BindingTypes.SETUP_CONST }) // should remove defineOptions import and call expect(content).not.toMatch('defineProps') // should generate correct setup signature expect(content).toMatch(`setup(__props, { expose }) {`) // should assign user identifier to it expect(content).toMatch(`const props = __props`) // should include context options in default export expect(content).toMatch(`export default { props: { foo: String },`) }) test('defineProps w/ external definition', () => { const { content } = compile(` `) assertCode(content) expect(content).toMatch(`export default { props: propsModel,`) }) test('defineEmits()', () => { const { content, bindings } = compile(` `) assertCode(content) expect(bindings).toStrictEqual({ myEmit: BindingTypes.SETUP_CONST }) // should remove defineOptions import and call expect(content).not.toMatch('defineEmits') // should generate correct setup signature expect(content).toMatch(`setup(__props, { expose, emit: myEmit }) {`) // should include context options in default export expect(content).toMatch(`export default { emits: ['foo', 'bar'],`) }) test('defineProps/defineEmits in multi-variable decalration', () => { const { content } = compile(` `) assertCode(content) expect(content).toMatch(`const a = 1;`) // test correct removal expect(content).toMatch(`props: ['item'],`) expect(content).toMatch(`emits: ['a'],`) }) test('defineProps/defineEmits in multi-variable decalration (full removal)', () => { const { content } = compile(` `) assertCode(content) expect(content).toMatch(`props: ['item'],`) expect(content).toMatch(`emits: ['a'],`) }) test('defineExpose()', () => { const { content } = compile(` `) assertCode(content) // should remove defineOptions import and call expect(content).not.toMatch('defineExpose') // should generate correct setup signature expect(content).toMatch(`setup(__props, { expose }) {`) // should replace callee expect(content).toMatch(/\bexpose\(\{ foo: 123 \}\)/) }) describe(' `) assertCode(content) }) test('script setup first', () => { const { content } = compile(` `) assertCode(content) }) }) describe('imports', () => { test('should hoist and expose imports', () => { assertCode( compile(``).content ) }) test('should extract comment for import or type declarations', () => { assertCode( compile(` `).content ) }) // #2740 test('should allow defineProps/Emit at the start of imports', () => { assertCode( compile(``).content ) }) test('dedupe between user & helper', () => { const { content } = compile( ` `, { refSugar: true } ) assertCode(content) expect(content).toMatch(`import { ref } from 'vue'`) }) test('import dedupe between `) assertCode(content) expect(content.indexOf(`import { x }`)).toEqual( content.lastIndexOf(`import { x }`) ) }) test('imports not used in