8
8
VNode ,
9
9
provide ,
10
10
inject ,
11
- Ref
11
+ Ref ,
12
+ watch ,
13
+ SetupContext
12
14
} from '@vue/runtime-test'
13
15
14
16
describe ( 'renderer: component' , ( ) => {
@@ -139,23 +141,21 @@ describe('renderer: component', () => {
139
141
} )
140
142
141
143
// #2170
142
- test ( 'should have access to instance’s “$el” property in watcher when setting instance data ' , async ( ) => {
144
+ test ( 'should have access to instance’s “$el” property in watcher when rendereing with watched prop ' , async ( ) => {
143
145
function returnThis ( this : any ) {
144
146
return this
145
147
}
146
- const dataWatchSpy = jest . fn ( returnThis )
148
+ const propWatchSpy = jest . fn ( returnThis )
147
149
let instance : any
148
150
const Comp = {
149
- data ( ) {
150
- return {
151
- testData : undefined
152
- }
151
+ props : {
152
+ testProp : String
153
153
} ,
154
154
155
155
watch : {
156
- testData ( ) {
156
+ testProp ( ) {
157
157
// @ts -ignore
158
- dataWatchSpy ( this . $el )
158
+ propWatchSpy ( this . $el )
159
159
}
160
160
} ,
161
161
@@ -170,50 +170,50 @@ describe('renderer: component', () => {
170
170
171
171
const root = nodeOps . createElement ( 'div' )
172
172
render ( h ( Comp ) , root )
173
+ await nextTick ( )
174
+ expect ( propWatchSpy ) . not . toHaveBeenCalled ( )
173
175
174
- expect ( dataWatchSpy ) . not . toHaveBeenCalled ( )
175
- instance . testData = 'data'
176
-
176
+ render ( h ( Comp , { testProp : 'prop ' } ) , root )
177
177
await nextTick ( )
178
- expect ( dataWatchSpy ) . toHaveBeenCalledWith ( instance . $el )
178
+ expect ( propWatchSpy ) . toHaveBeenCalledWith ( instance . $el )
179
179
} )
180
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
- } ,
181
+ // #2200
182
+ test ( 'component child updating parent state in pre-flush should trigger parent re-render' , async ( ) => {
183
+ const outer = ref ( 0 )
184
+ const App = {
185
+ setup ( ) {
186
+ const inner = ref ( 0 )
192
187
193
- watch : {
194
- testProp ( ) {
195
- // @ts -ignore
196
- propWatchSpy ( this . $el )
188
+ return ( ) => {
189
+ return [
190
+ h ( 'div' , inner . value ) ,
191
+ h ( Child , {
192
+ value : outer . value ,
193
+ onUpdate : ( val : number ) => ( inner . value = val )
194
+ } )
195
+ ]
197
196
}
198
- } ,
197
+ }
198
+ }
199
199
200
- created ( ) {
201
- instance = this
202
- } ,
200
+ const Child = {
201
+ props : [ 'value' ] ,
202
+ setup ( props : any , { emit } : SetupContext ) {
203
+ watch ( ( ) => props . value , ( val : number ) => emit ( 'update' , val ) )
203
204
204
- render ( ) {
205
- return h ( 'div' )
205
+ return ( ) => {
206
+ return h ( 'div' , props . value )
207
+ }
206
208
}
207
209
}
208
210
209
211
const root = nodeOps . createElement ( 'div' )
212
+ render ( h ( App ) , root )
213
+ expect ( serializeInner ( root ) ) . toBe ( `<div>0</div><div>0</div>` )
210
214
211
- render ( h ( Comp ) , root )
212
- await nextTick ( )
213
- expect ( propWatchSpy ) . not . toHaveBeenCalled ( )
214
-
215
- render ( h ( Comp , { testProp : 'prop ' } ) , root )
215
+ outer . value ++
216
216
await nextTick ( )
217
- expect ( propWatchSpy ) . toHaveBeenCalledWith ( instance . $el )
217
+ expect ( serializeInner ( root ) ) . toBe ( `<div>1</div><div>1</div>` )
218
218
} )
219
219
} )
0 commit comments