File tree 2 files changed +80
-1
lines changed
2 files changed +80
-1
lines changed Original file line number Diff line number Diff line change @@ -137,4 +137,83 @@ describe('renderer: component', () => {
137
137
await nextTick ( )
138
138
expect ( serializeInner ( root ) ) . toBe ( `<div>1</div><div>1</div>` )
139
139
} )
140
+
141
+ // #2170
142
+ test ( 'should have access to instance’s “$el” property in watcher when setting instance data' , async ( ) => {
143
+ function returnThis ( this : any ) {
144
+ return this
145
+ }
146
+ const dataWatchSpy = jest . fn ( returnThis )
147
+ let instance : any
148
+ const Comp = {
149
+ data ( ) {
150
+ return {
151
+ testData : undefined
152
+ }
153
+ } ,
154
+
155
+ watch : {
156
+ testData ( ) {
157
+ // @ts -ignore
158
+ dataWatchSpy ( this . $el )
159
+ }
160
+ } ,
161
+
162
+ created ( ) {
163
+ instance = this
164
+ } ,
165
+
166
+ render ( ) {
167
+ return h ( 'div' )
168
+ }
169
+ }
170
+
171
+ const root = nodeOps . createElement ( 'div' )
172
+ render ( h ( Comp ) , root )
173
+
174
+ expect ( dataWatchSpy ) . not . toHaveBeenCalled ( )
175
+ instance . testData = 'data'
176
+
177
+ await nextTick ( )
178
+ expect ( dataWatchSpy ) . toHaveBeenCalledWith ( instance . $el )
179
+ } )
180
+
181
+ // #2170
182
+ test ( 'should have access to instance’s “$el” property in watcher when rendereing with watched prop' , async ( ) => {
183
+ function returnThis ( this : any ) {
184
+ return this
185
+ }
186
+ const propWatchSpy = jest . fn ( returnThis )
187
+ let instance : any
188
+ const Comp = {
189
+ props : {
190
+ testProp : String
191
+ } ,
192
+
193
+ watch : {
194
+ testProp ( ) {
195
+ // @ts -ignore
196
+ propWatchSpy ( this . $el )
197
+ }
198
+ } ,
199
+
200
+ created ( ) {
201
+ instance = this
202
+ } ,
203
+
204
+ render ( ) {
205
+ return h ( 'div' )
206
+ }
207
+ }
208
+
209
+ const root = nodeOps . createElement ( 'div' )
210
+
211
+ render ( h ( Comp ) , root )
212
+ await nextTick ( )
213
+ expect ( propWatchSpy ) . not . toHaveBeenCalled ( )
214
+
215
+ render ( h ( Comp , { testProp : 'prop ' } ) , root )
216
+ await nextTick ( )
217
+ expect ( propWatchSpy ) . toHaveBeenCalledWith ( instance . $el )
218
+ } )
140
219
} )
Original file line number Diff line number Diff line change @@ -1425,11 +1425,11 @@ function baseCreateRenderer(
1425
1425
}
1426
1426
1427
1427
if ( next ) {
1428
+ next . el = vnode . el
1428
1429
updateComponentPreRender ( instance , next , optimized )
1429
1430
} else {
1430
1431
next = vnode
1431
1432
}
1432
- next . el = vnode . el
1433
1433
1434
1434
// beforeUpdate hook
1435
1435
if ( bu ) {
You can’t perform that action at this time.
0 commit comments