File tree 5 files changed +81
-7
lines changed
test/unit/specs/mount/Wrapper
5 files changed +81
-7
lines changed Original file line number Diff line number Diff line change @@ -4,17 +4,26 @@ import Wrapper from './wrapper'
4
4
import addSlots from '../lib/add-slots'
5
5
import cloneDeep from 'lodash/cloneDeep'
6
6
7
- function update ( ) {
7
+ function update ( changedData ) {
8
8
// the only component made by mount()
9
9
if ( this . $_originalSlots ) {
10
10
this . $slots = cloneDeep ( this . $_originalSlots )
11
11
}
12
12
if ( this . $_mountingOptionsSlots ) {
13
13
addSlots ( this , this . $_mountingOptionsSlots )
14
14
}
15
- this . _watchers . forEach ( watcher => {
16
- watcher . run ( )
17
- } )
15
+ if ( changedData ) {
16
+ Object . keys ( changedData ) . forEach ( ( key ) => {
17
+ // $FlowIgnore : Problem with possibly null this.vm
18
+ this . _watchers . forEach ( ( watcher ) => {
19
+ if ( watcher . expression === key ) { watcher . run ( ) }
20
+ } )
21
+ } )
22
+ } else {
23
+ this . _watchers . forEach ( watcher => {
24
+ watcher . run ( )
25
+ } )
26
+ }
18
27
const vnodes = this . _render ( )
19
28
this . _update ( vnodes )
20
29
this . $children . forEach ( child => update . call ( child ) )
Original file line number Diff line number Diff line change @@ -377,7 +377,7 @@ export default class Wrapper implements BaseWrapper {
377
377
this . vm . $set ( this . vm , [ key ] , data [ key ] )
378
378
} )
379
379
380
- this . update ( )
380
+ this . update ( data )
381
381
}
382
382
383
383
/**
@@ -452,18 +452,23 @@ export default class Wrapper implements BaseWrapper {
452
452
if ( ! this . isVueComponent || ! this . vm ) {
453
453
throwError ( 'wrapper.setProps() can only be called on a Vue instance' )
454
454
}
455
-
455
+ if ( ! this . vm . $options . propsData ) {
456
+ this . vm . $options . propsData = { }
457
+ }
456
458
Object . keys ( data ) . forEach ( ( key ) => {
457
459
// $FlowIgnore : Problem with possibly null this.vm
458
460
if ( this . vm . _props ) {
459
461
this . vm . _props [ key ] = data [ key ]
462
+ this . vm . $props [ key ] = data [ key ]
463
+ this . vm . $options . propsData [ key ] = data [ key ]
460
464
} else {
461
465
// $FlowIgnore : Problem with possibly null this.vm
462
466
this . vm [ key ] = data [ key ]
467
+ this . vm . $options . propsData [ key ] = data [ key ]
463
468
}
464
469
} )
465
470
466
- this . update ( )
471
+ this . update ( data )
467
472
// $FlowIgnore : Problem with possibly null this.vm
468
473
this . vnode = this . vm . _vnode
469
474
}
Original file line number Diff line number Diff line change @@ -18,6 +18,20 @@ describe('props', () => {
18
18
expect ( wrapper . props ( ) ) . to . eql ( { } )
19
19
} )
20
20
21
+ it ( 'should update after setProps' , ( ) => {
22
+ const prop1 = { }
23
+ const prop2 = 'val1'
24
+ const wrapper = mount ( ComponentWithProps , {
25
+ propsData : { prop1, prop2 }
26
+ } )
27
+
28
+ expect ( wrapper . props ( ) ) . to . eql ( { prop1 : { } , prop2 : 'val1' } )
29
+ // setProps
30
+ wrapper . setProps ( { prop2 : 'val2' } )
31
+ expect ( wrapper . vm . prop2 ) . to . eql ( 'val2' ) // pass
32
+ expect ( wrapper . props ( ) ) . to . eql ( { prop1 : { } , prop2 : 'val2' } ) // fail
33
+ } )
34
+
21
35
it ( 'throws an error if called on a non vm wrapper' , ( ) => {
22
36
const compiled = compileToFunctions ( '<div><p /></div>' )
23
37
const p = mount ( compiled ) . findAll ( 'p' ) . at ( 0 )
Original file line number Diff line number Diff line change @@ -58,4 +58,26 @@ describe('setData', () => {
58
58
const p = wrapper . find ( 'p' )
59
59
expect ( ( ) => p . setData ( { ready : true } ) ) . throw ( Error , message )
60
60
} )
61
+
62
+ it ( 'should not run watchers if data updated is null' , ( ) => {
63
+ const TestComponent = {
64
+ template : `
65
+ <div>
66
+ <div v-if="!message">There is no message yet</div>
67
+ <div v-else>{{ reversedMessage }}</div>
68
+ </div>
69
+ ` ,
70
+ data : ( ) => ( {
71
+ message : 'egassem'
72
+ } ) ,
73
+ computed : {
74
+ reversedMessage : function ( ) {
75
+ return this . message . split ( '' ) . reverse ( ) . join ( '' )
76
+ }
77
+ }
78
+ }
79
+ const wrapper = mount ( TestComponent )
80
+ wrapper . setData ( { message : null } )
81
+ expect ( wrapper . text ( ) ) . to . equal ( 'There is no message yet' )
82
+ } )
61
83
} )
Original file line number Diff line number Diff line change @@ -47,6 +47,30 @@ describe('setProps', () => {
47
47
expect ( info . args [ 0 ] [ 0 ] ) . to . equal ( prop1 )
48
48
} )
49
49
50
+ it ( 'should not run watchers if prop updated is null' , ( ) => {
51
+ const TestComponent = {
52
+ template : `
53
+ <div>
54
+ <div v-if="!message">There is no message yet</div>
55
+ <div v-else>{{ reversedMessage }}</div>
56
+ </div>
57
+ ` ,
58
+ computed : {
59
+ reversedMessage : function ( ) {
60
+ return this . message . split ( '' ) . reverse ( ) . join ( '' )
61
+ }
62
+ } ,
63
+ props : [ 'message' ]
64
+ }
65
+ const wrapper = mount ( TestComponent , {
66
+ propsData : {
67
+ message : 'message'
68
+ }
69
+ } )
70
+ wrapper . setProps ( { message : null } )
71
+ expect ( wrapper . text ( ) ) . to . equal ( 'There is no message yet' )
72
+ } )
73
+
50
74
it ( 'throws an error if node is not a Vue instance' , ( ) => {
51
75
const message = 'wrapper.setProps() can only be called on a Vue instance'
52
76
const compiled = compileToFunctions ( '<div><p></p></div>' )
You can’t perform that action at this time.
0 commit comments