@@ -46,10 +46,12 @@ describe('Observer', function () {
46
46
// on object
47
47
var obj = { }
48
48
var val = 0
49
+ var getCount = 0
49
50
Object . defineProperty ( obj , 'a' , {
50
51
configurable : true ,
51
52
enumerable : true ,
52
53
get : function ( ) {
54
+ getCount ++
53
55
return val
54
56
} ,
55
57
set : function ( v ) {
@@ -62,6 +64,13 @@ describe('Observer', function () {
62
64
expect ( ob . value ) . toBe ( obj )
63
65
expect ( obj . __ob__ ) . toBe ( ob )
64
66
67
+ getCount = 0
68
+ // Each read of 'a' should result in only one get underlying get call
69
+ obj . a
70
+ expect ( getCount ) . toBe ( 1 )
71
+ obj . a
72
+ expect ( getCount ) . toBe ( 2 )
73
+
65
74
// should return existing ob on already observed objects
66
75
var ob2 = Observer . create ( obj )
67
76
expect ( ob2 ) . toBe ( ob )
@@ -73,6 +82,95 @@ describe('Observer', function () {
73
82
config . convertAllProperties = previousConvertAllProperties
74
83
} )
75
84
85
+ it ( 'create on property with only getter' , function ( ) {
86
+ var previousConvertAllProperties = config . convertAllProperties
87
+ config . convertAllProperties = true
88
+
89
+ // on object
90
+ var obj = { }
91
+ Object . defineProperty ( obj , 'a' , {
92
+ configurable : true ,
93
+ enumerable : true ,
94
+ get : function ( ) {
95
+ return 123
96
+ }
97
+ } )
98
+
99
+ var ob = Observer . create ( obj )
100
+ expect ( ob instanceof Observer ) . toBe ( true )
101
+ expect ( ob . value ) . toBe ( obj )
102
+ expect ( obj . __ob__ ) . toBe ( ob )
103
+
104
+ // should be able to read
105
+ expect ( obj . a ) . toBe ( 123 )
106
+
107
+ // should return existing ob on already observed objects
108
+ var ob2 = Observer . create ( obj )
109
+ expect ( ob2 ) . toBe ( ob )
110
+
111
+ // since there is no setter, you shouldn't be able to write to it
112
+ expect ( function ( ) {
113
+ obj . a = 101
114
+ } ) . toThrow ( )
115
+ expect ( obj . a ) . toBe ( 123 )
116
+
117
+ config . convertAllProperties = previousConvertAllProperties
118
+ } )
119
+
120
+ it ( 'create on property with only setter' , function ( ) {
121
+ var previousConvertAllProperties = config . convertAllProperties
122
+ config . convertAllProperties = true
123
+
124
+ // on object
125
+ var obj = { }
126
+ var val = 10
127
+ Object . defineProperty ( obj , 'a' , { // eslint-disable-line accessor-pairs
128
+ configurable : true ,
129
+ enumerable : true ,
130
+ set : function ( v ) {
131
+ val = v
132
+ }
133
+ } )
134
+
135
+ var ob = Observer . create ( obj )
136
+ expect ( ob instanceof Observer ) . toBe ( true )
137
+ expect ( ob . value ) . toBe ( obj )
138
+ expect ( obj . __ob__ ) . toBe ( ob )
139
+
140
+ // reads should return undefined
141
+ expect ( obj . a ) . toBe ( undefined )
142
+
143
+ // should return existing ob on already observed objects
144
+ var ob2 = Observer . create ( obj )
145
+ expect ( ob2 ) . toBe ( ob )
146
+
147
+ // writes should call the set function
148
+ obj . a = 100
149
+ expect ( val ) . toBe ( 100 )
150
+
151
+ config . convertAllProperties = previousConvertAllProperties
152
+ } )
153
+
154
+ it ( 'create on property which is marked not configurable' , function ( ) {
155
+ var previousConvertAllProperties = config . convertAllProperties
156
+ config . convertAllProperties = true
157
+
158
+ // on object
159
+ var obj = { }
160
+ Object . defineProperty ( obj , 'a' , {
161
+ configurable : false ,
162
+ enumerable : true ,
163
+ val : 10
164
+ } )
165
+
166
+ var ob = Observer . create ( obj )
167
+ expect ( ob instanceof Observer ) . toBe ( true )
168
+ expect ( ob . value ) . toBe ( obj )
169
+ expect ( obj . __ob__ ) . toBe ( ob )
170
+
171
+ config . convertAllProperties = previousConvertAllProperties
172
+ } )
173
+
76
174
it ( 'create on array' , function ( ) {
77
175
// on object
78
176
var arr = [ { } , { } ]
0 commit comments