@@ -16,7 +16,8 @@ describe("Model instance", function() {
16
16
name : String ,
17
17
age : { type : 'integer' , required : false } ,
18
18
height : { type : 'integer' , required : false } ,
19
- weight : { type : 'number' , required : false }
19
+ weight : { type : 'number' , required : false } ,
20
+ data : { type : 'object' , required : false }
20
21
} , {
21
22
cache : false ,
22
23
validations : {
@@ -144,6 +145,115 @@ describe("Model instance", function() {
144
145
} ) ;
145
146
} ) ;
146
147
148
+ describe ( "#set" , function ( ) {
149
+ var person = null ;
150
+ var data = null ;
151
+
152
+ function clone ( obj ) { return JSON . parse ( JSON . stringify ( obj ) ) } ;
153
+
154
+ beforeEach ( function ( done ) {
155
+ data = {
156
+ a : {
157
+ b : {
158
+ c : 3 ,
159
+ d : 4
160
+ }
161
+ } ,
162
+ e : 5
163
+ } ;
164
+ Person . create ( { name : 'Dilbert' , data : data } , function ( err , p ) {
165
+ if ( err ) return done ( err ) ;
166
+
167
+ person = p ;
168
+ done ( ) ;
169
+ } ) ;
170
+ } ) ;
171
+
172
+ it ( "should do nothing with flat paths when setting to same value" , function ( ) {
173
+ should . equal ( person . saved ( ) , true ) ;
174
+ person . set ( 'name' , 'Dilbert' ) ;
175
+ should . equal ( person . name , 'Dilbert' ) ;
176
+ should . equal ( person . saved ( ) , true ) ;
177
+ } ) ;
178
+
179
+ it ( "should mark as dirty with flat paths when setting to different value" , function ( ) {
180
+ should . equal ( person . saved ( ) , true ) ;
181
+ person . set ( 'name' , 'Dogbert' ) ;
182
+ should . equal ( person . name , 'Dogbert' ) ;
183
+ should . equal ( person . saved ( ) , false ) ;
184
+ should . equal ( person . __opts . changes . join ( ',' ) , 'name' ) ;
185
+ } ) ;
186
+
187
+ it ( "should do nothin with deep paths when setting to same value" , function ( ) {
188
+ should . equal ( person . saved ( ) , true ) ;
189
+ person . set ( 'data.e' , 5 ) ;
190
+
191
+ var expected = clone ( data ) ;
192
+ expected . e = 5 ;
193
+
194
+ should . equal ( JSON . stringify ( person . data ) , JSON . stringify ( expected ) ) ;
195
+ should . equal ( person . saved ( ) , true ) ;
196
+ } ) ;
197
+
198
+ it ( "should mark as dirty with deep paths when setting to different value" , function ( ) {
199
+ should . equal ( person . saved ( ) , true ) ;
200
+ person . set ( 'data.e' , 6 ) ;
201
+
202
+ var expected = clone ( data ) ;
203
+ expected . e = 6 ;
204
+
205
+ should . equal ( JSON . stringify ( person . data ) , JSON . stringify ( expected ) ) ;
206
+ should . equal ( person . saved ( ) , false ) ;
207
+ should . equal ( person . __opts . changes . join ( ',' ) , 'data' ) ;
208
+ } ) ;
209
+
210
+ it ( "should do nothing with deeper paths when setting to same value" , function ( ) {
211
+ should . equal ( person . saved ( ) , true ) ;
212
+ person . set ( 'data.a.b.d' , 4 ) ;
213
+
214
+ var expected = clone ( data ) ;
215
+ expected . a . b . d = 4 ;
216
+
217
+ should . equal ( JSON . stringify ( person . data ) , JSON . stringify ( expected ) ) ;
218
+ should . equal ( person . saved ( ) , true ) ;
219
+ } ) ;
220
+
221
+ it ( "should mark as dirty with deeper paths when setting to different value" , function ( ) {
222
+ should . equal ( person . saved ( ) , true ) ;
223
+ person . set ( 'data.a.b.d' , 6 ) ;
224
+
225
+ var expected = clone ( data ) ;
226
+ expected . a . b . d = 6 ;
227
+
228
+ should . equal ( JSON . stringify ( person . data ) , JSON . stringify ( expected ) ) ;
229
+ should . equal ( person . saved ( ) , false ) ;
230
+ should . equal ( person . __opts . changes . join ( ',' ) , 'data' ) ;
231
+ } ) ;
232
+
233
+ it ( "should mark as dirty with array path when setting to different value" , function ( ) {
234
+ should . equal ( person . saved ( ) , true ) ;
235
+ person . set ( [ 'data' , 'a' , 'b' , 'd' ] , 6 ) ;
236
+
237
+ var expected = clone ( data ) ;
238
+ expected . a . b . d = 6 ;
239
+
240
+ should . equal ( JSON . stringify ( person . data ) , JSON . stringify ( expected ) ) ;
241
+ should . equal ( person . saved ( ) , false ) ;
242
+ should . equal ( person . __opts . changes . join ( ',' ) , 'data' ) ;
243
+ } ) ;
244
+
245
+ it ( "should do nothing with invalid paths" , function ( ) {
246
+ should . equal ( person . saved ( ) , true ) ;
247
+ person . set ( 'data.a.b.d.y.z' , 1 ) ;
248
+ person . set ( 'data.y.z' , 1 ) ;
249
+ person . set ( 'z' , 1 ) ;
250
+ person . set ( 4 , 1 ) ;
251
+ person . set ( null , 1 ) ;
252
+ person . set ( undefined , 1 ) ;
253
+ should . equal ( person . saved ( ) , true ) ;
254
+ } ) ;
255
+ } ) ;
256
+
147
257
describe ( "#isShell" , function ( ) {
148
258
it ( "should return true for shell models" , function ( ) {
149
259
should . equal ( Person ( 4 ) . isShell ( ) , true ) ;
0 commit comments