@@ -1066,6 +1066,188 @@ describe('api: options', () => {
1066
1066
)
1067
1067
} )
1068
1068
1069
+ describe ( 'options merge strategies' , ( ) => {
1070
+ test ( 'this.$options.data' , ( ) => {
1071
+ const mixin = {
1072
+ data ( ) {
1073
+ return { foo : 1 , bar : 2 }
1074
+ }
1075
+ }
1076
+ createApp ( {
1077
+ mixins : [ mixin ] ,
1078
+ data ( ) {
1079
+ return {
1080
+ foo : 3 ,
1081
+ baz : 4
1082
+ }
1083
+ } ,
1084
+ created ( ) {
1085
+ expect ( this . $options . data ) . toBeInstanceOf ( Function )
1086
+ expect ( this . $options . data ( ) ) . toEqual ( {
1087
+ foo : 3 ,
1088
+ bar : 2 ,
1089
+ baz : 4
1090
+ } )
1091
+ } ,
1092
+ render : ( ) => null
1093
+ } ) . mount ( nodeOps . createElement ( 'div' ) )
1094
+ } )
1095
+
1096
+ test ( 'this.$options.inject' , ( ) => {
1097
+ const mixin = {
1098
+ inject : [ 'a' ]
1099
+ }
1100
+ const app = createApp ( {
1101
+ mixins : [ mixin ] ,
1102
+ inject : { b : 'b' , c : { from : 'd' } } ,
1103
+ created ( ) {
1104
+ expect ( this . $options . inject . a ) . toEqual ( 'a' )
1105
+ expect ( this . $options . inject . b ) . toEqual ( 'b' )
1106
+ expect ( this . $options . inject . c ) . toEqual ( { from : 'd' } )
1107
+ expect ( this . a ) . toBe ( 1 )
1108
+ expect ( this . b ) . toBe ( 2 )
1109
+ expect ( this . c ) . toBe ( 3 )
1110
+ } ,
1111
+ render : ( ) => null
1112
+ } )
1113
+
1114
+ app . provide ( 'a' , 1 )
1115
+ app . provide ( 'b' , 2 )
1116
+ app . provide ( 'd' , 3 )
1117
+ app . mount ( nodeOps . createElement ( 'div' ) )
1118
+ } )
1119
+
1120
+ test ( 'this.$options.provide' , ( ) => {
1121
+ const mixin = {
1122
+ provide : {
1123
+ a : 1
1124
+ }
1125
+ }
1126
+ createApp ( {
1127
+ mixins : [ mixin ] ,
1128
+ provide ( ) {
1129
+ return {
1130
+ b : 2
1131
+ }
1132
+ } ,
1133
+ created ( ) {
1134
+ expect ( this . $options . provide ) . toBeInstanceOf ( Function )
1135
+ expect ( this . $options . provide ( ) ) . toEqual ( { a : 1 , b : 2 } )
1136
+ } ,
1137
+ render : ( ) => null
1138
+ } ) . mount ( nodeOps . createElement ( 'div' ) )
1139
+ } )
1140
+
1141
+ test ( 'this.$options[lifecycle-name]' , ( ) => {
1142
+ const mixin = {
1143
+ mounted ( ) { }
1144
+ }
1145
+ createApp ( {
1146
+ mixins : [ mixin ] ,
1147
+ mounted ( ) { } ,
1148
+ created ( ) {
1149
+ expect ( this . $options . mounted ) . toBeInstanceOf ( Array )
1150
+ expect ( this . $options . mounted . length ) . toBe ( 2 )
1151
+ } ,
1152
+ render : ( ) => null
1153
+ } ) . mount ( nodeOps . createElement ( 'div' ) )
1154
+ } )
1155
+
1156
+ test ( 'this.$options[asset-name]' , ( ) => {
1157
+ const mixin = {
1158
+ components : {
1159
+ a : { }
1160
+ } ,
1161
+ directives : {
1162
+ d1 : { }
1163
+ }
1164
+ }
1165
+ createApp ( {
1166
+ mixins : [ mixin ] ,
1167
+ components : {
1168
+ b : { }
1169
+ } ,
1170
+ directives : {
1171
+ d2 : { }
1172
+ } ,
1173
+ created ( ) {
1174
+ expect ( 'a' in this . $options . components ) . toBe ( true )
1175
+ expect ( 'b' in this . $options . components ) . toBe ( true )
1176
+ expect ( 'd1' in this . $options . directives ) . toBe ( true )
1177
+ expect ( 'd2' in this . $options . directives ) . toBe ( true )
1178
+ } ,
1179
+ render : ( ) => null
1180
+ } ) . mount ( nodeOps . createElement ( 'div' ) )
1181
+ } )
1182
+
1183
+ test ( 'this.$options.methods' , ( ) => {
1184
+ const mixin = {
1185
+ methods : {
1186
+ fn1 ( ) { }
1187
+ }
1188
+ }
1189
+ createApp ( {
1190
+ mixins : [ mixin ] ,
1191
+ methods : {
1192
+ fn2 ( ) { }
1193
+ } ,
1194
+ created ( ) {
1195
+ expect ( this . $options . methods . fn1 ) . toBeInstanceOf ( Function )
1196
+ expect ( this . $options . methods . fn2 ) . toBeInstanceOf ( Function )
1197
+ } ,
1198
+ render : ( ) => null
1199
+ } ) . mount ( nodeOps . createElement ( 'div' ) )
1200
+ } )
1201
+
1202
+ test ( 'this.$options.computed' , ( ) => {
1203
+ const mixin = {
1204
+ computed : {
1205
+ c1 ( ) { }
1206
+ }
1207
+ }
1208
+ createApp ( {
1209
+ mixins : [ mixin ] ,
1210
+ computed : {
1211
+ c2 ( ) { }
1212
+ } ,
1213
+ created ( ) {
1214
+ expect ( this . $options . computed . c1 ) . toBeInstanceOf ( Function )
1215
+ expect ( this . $options . computed . c2 ) . toBeInstanceOf ( Function )
1216
+ } ,
1217
+ render : ( ) => null
1218
+ } ) . mount ( nodeOps . createElement ( 'div' ) )
1219
+ } )
1220
+
1221
+ // #2791
1222
+ test ( 'modify $options in the beforeCreate hook' , async ( ) => {
1223
+ const count = ref ( 0 )
1224
+ const mixin = {
1225
+ data ( ) {
1226
+ return { foo : 1 }
1227
+ } ,
1228
+ beforeCreate ( this : any ) {
1229
+ if ( ! this . $options . computed ) {
1230
+ this . $options . computed = { }
1231
+ }
1232
+ this . $options . computed . value = ( ) => count . value
1233
+ }
1234
+ }
1235
+ const root = nodeOps . createElement ( 'div' )
1236
+ createApp ( {
1237
+ mixins : [ mixin ] ,
1238
+ render ( this : any ) {
1239
+ return this . value
1240
+ }
1241
+ } ) . mount ( root )
1242
+
1243
+ expect ( serializeInner ( root ) ) . toBe ( '0' )
1244
+
1245
+ count . value ++
1246
+ await nextTick ( )
1247
+ expect ( serializeInner ( root ) ) . toBe ( '1' )
1248
+ } )
1249
+ } )
1250
+
1069
1251
describe ( 'warnings' , ( ) => {
1070
1252
test ( 'Expected a function as watch handler' , ( ) => {
1071
1253
const Comp = {
0 commit comments