@@ -49,7 +49,7 @@ const bar = 1
49
49
},` )
50
50
} )
51
51
52
- test ( 'defineEmit()' , ( ) => {
52
+ test ( 'defineEmit() (deprecated) ' , ( ) => {
53
53
const { content, bindings } = compile ( `
54
54
<script setup>
55
55
import { defineEmit } from 'vue'
@@ -61,7 +61,28 @@ const myEmit = defineEmit(['foo', 'bar'])
61
61
myEmit : BindingTypes . SETUP_CONST
62
62
} )
63
63
// should remove defineOptions import and call
64
- expect ( content ) . not . toMatch ( 'defineEmit' )
64
+ expect ( content ) . not . toMatch ( / d e f i n e E m i t s ? / )
65
+ // should generate correct setup signature
66
+ expect ( content ) . toMatch ( `setup(__props, { emit: myEmit }) {` )
67
+ // should include context options in default export
68
+ expect ( content ) . toMatch ( `export default {
69
+ expose: [],
70
+ emits: ['foo', 'bar'],` )
71
+ } )
72
+
73
+ test ( 'defineEmits()' , ( ) => {
74
+ const { content, bindings } = compile ( `
75
+ <script setup>
76
+ import { defineEmits } from 'vue'
77
+ const myEmit = defineEmits(['foo', 'bar'])
78
+ </script>
79
+ ` )
80
+ assertCode ( content )
81
+ expect ( bindings ) . toStrictEqual ( {
82
+ myEmit : BindingTypes . SETUP_CONST
83
+ } )
84
+ // should remove defineOptions import and call
85
+ expect ( content ) . not . toMatch ( 'defineEmits' )
65
86
// should generate correct setup signature
66
87
expect ( content ) . toMatch ( `setup(__props, { emit: myEmit }) {` )
67
88
// should include context options in default export
@@ -145,9 +166,9 @@ const myEmit = defineEmit(['foo', 'bar'])
145
166
test ( 'should allow defineProps/Emit at the start of imports' , ( ) => {
146
167
assertCode (
147
168
compile ( `<script setup>
148
- import { defineProps, defineEmit , ref } from 'vue'
169
+ import { defineProps, defineEmits , ref } from 'vue'
149
170
defineProps(['foo'])
150
- defineEmit (['bar'])
171
+ defineEmits (['bar'])
151
172
const r = ref(0)
152
173
</script>` ) . content
153
174
)
@@ -450,9 +471,9 @@ const myEmit = defineEmit(['foo', 'bar'])
450
471
test ( 'defineProps/Emit w/ runtime options' , ( ) => {
451
472
const { content } = compile ( `
452
473
<script setup lang="ts">
453
- import { defineProps, defineEmit } from 'vue'
474
+ import { defineProps, defineEmits } from 'vue'
454
475
const props = defineProps({ foo: String })
455
- const emit = defineEmit (['a', 'b'])
476
+ const emit = defineEmits (['a', 'b'])
456
477
</script>
457
478
` )
458
479
assertCode ( content )
@@ -549,36 +570,36 @@ const emit = defineEmit(['a', 'b'])
549
570
} )
550
571
} )
551
572
552
- test ( 'defineEmit w/ type' , ( ) => {
573
+ test ( 'defineEmits w/ type' , ( ) => {
553
574
const { content } = compile ( `
554
575
<script setup lang="ts">
555
- import { defineEmit } from 'vue'
556
- const emit = defineEmit <(e: 'foo' | 'bar') => void>()
576
+ import { defineEmits } from 'vue'
577
+ const emit = defineEmits <(e: 'foo' | 'bar') => void>()
557
578
</script>
558
579
` )
559
580
assertCode ( content )
560
581
expect ( content ) . toMatch ( `emit: ((e: 'foo' | 'bar') => void),` )
561
582
expect ( content ) . toMatch ( `emits: ["foo", "bar"] as unknown as undefined` )
562
583
} )
563
584
564
- test ( 'defineEmit w/ type (union)' , ( ) => {
585
+ test ( 'defineEmits w/ type (union)' , ( ) => {
565
586
const type = `((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void)`
566
587
expect ( ( ) =>
567
588
compile ( `
568
589
<script setup lang="ts">
569
- import { defineEmit } from 'vue'
570
- const emit = defineEmit <${ type } >()
590
+ import { defineEmits } from 'vue'
591
+ const emit = defineEmits <${ type } >()
571
592
</script>
572
593
` )
573
594
) . toThrow ( )
574
595
} )
575
596
576
- test ( 'defineEmit w/ type (type literal w/ call signatures)' , ( ) => {
597
+ test ( 'defineEmits w/ type (type literal w/ call signatures)' , ( ) => {
577
598
const type = `{(e: 'foo' | 'bar'): void; (e: 'baz', id: number): void;}`
578
599
const { content } = compile ( `
579
600
<script setup lang="ts">
580
- import { defineEmit } from 'vue'
581
- const emit = defineEmit <${ type } >()
601
+ import { defineEmits } from 'vue'
602
+ const emit = defineEmits <${ type } >()
582
603
</script>
583
604
` )
584
605
assertCode ( content )
@@ -906,8 +927,8 @@ const emit = defineEmit(['a', 'b'])
906
927
907
928
expect ( ( ) => {
908
929
compile ( `<script setup lang="ts">
909
- import { defineEmit } from 'vue'
910
- defineEmit <{}>({})
930
+ import { defineEmits } from 'vue'
931
+ defineEmits <{}>({})
911
932
</script>` )
912
933
} ) . toThrow ( `cannot accept both type and non-type arguments` )
913
934
} )
@@ -927,9 +948,9 @@ const emit = defineEmit(['a', 'b'])
927
948
928
949
expect ( ( ) =>
929
950
compile ( `<script setup>
930
- import { defineEmit } from 'vue'
951
+ import { defineEmits } from 'vue'
931
952
const bar = 'hello'
932
- defineEmit ([bar])
953
+ defineEmits ([bar])
933
954
</script>` )
934
955
) . toThrow ( `cannot reference locally declared variables` )
935
956
} )
@@ -947,9 +968,9 @@ const emit = defineEmit(['a', 'b'])
947
968
948
969
expect ( ( ) =>
949
970
compile ( `<script setup>
950
- import { defineEmit } from 'vue'
971
+ import { defineEmits } from 'vue'
951
972
ref: bar = 1
952
- defineEmit ({
973
+ defineEmits ({
953
974
bar
954
975
})
955
976
</script>` )
@@ -959,14 +980,14 @@ const emit = defineEmit(['a', 'b'])
959
980
test ( 'should allow defineProps/Emit() referencing scope var' , ( ) => {
960
981
assertCode (
961
982
compile ( `<script setup>
962
- import { defineProps, defineEmit } from 'vue'
983
+ import { defineProps, defineEmits } from 'vue'
963
984
const bar = 1
964
985
defineProps({
965
986
foo: {
966
987
default: bar => bar + 1
967
988
}
968
989
})
969
- defineEmit ({
990
+ defineEmits ({
970
991
foo: bar => bar > 1
971
992
})
972
993
</script>` ) . content
@@ -976,14 +997,14 @@ const emit = defineEmit(['a', 'b'])
976
997
test ( 'should allow defineProps/Emit() referencing imported binding' , ( ) => {
977
998
assertCode (
978
999
compile ( `<script setup>
979
- import { defineProps, defineEmit } from 'vue'
1000
+ import { defineProps, defineEmits } from 'vue'
980
1001
import { bar } from './bar'
981
1002
defineProps({
982
1003
foo: {
983
1004
default: () => bar
984
1005
}
985
1006
})
986
- defineEmit ({
1007
+ defineEmits ({
987
1008
foo: () => bar > 1
988
1009
})
989
1010
</script>` ) . content
0 commit comments