@@ -196,6 +196,53 @@ describe('ObjectStateMutations', () => {
196196 } ) ;
197197 } ) ;
198198
199+ it ( 'can estimate attributes for nested array documents' , ( ) => {
200+ // Test without initial value
201+ let serverData = { _id : 'someId' , className : 'bug' } ;
202+ let pendingOps = [ { 'items.0.count' : new ParseOps . IncrementOp ( 1 ) } ] ;
203+ expect (
204+ ObjectStateMutations . estimateAttributes ( serverData , pendingOps , 'someClass' , 'someId' )
205+ ) . toEqual ( {
206+ _id : 'someId' ,
207+ items : [ { count : 1 } ] ,
208+ className : 'bug' ,
209+ } ) ;
210+
211+ // Test one level nested
212+ serverData = {
213+ _id : 'someId' ,
214+ items : [ { value : 'a' , count : 5 } , { value : 'b' , count : 1 } ] ,
215+ className : 'bug' ,
216+ number : 2
217+ }
218+ pendingOps = [ { 'items.0.count' : new ParseOps . IncrementOp ( 1 ) } ] ;
219+ expect (
220+ ObjectStateMutations . estimateAttributes ( serverData , pendingOps , 'someClass' , 'someId' )
221+ ) . toEqual ( {
222+ _id : 'someId' ,
223+ items : [ { value : 'a' , count : 6 } , { value : 'b' , count : 1 } ] ,
224+ className : 'bug' ,
225+ number : 2
226+ } ) ;
227+
228+ // Test multiple level nested fields
229+ serverData = {
230+ _id : 'someId' ,
231+ items : [ { value : { count : 54 } , count : 5 } , { value : 'b' , count : 1 } ] ,
232+ className : 'bug' ,
233+ number : 2
234+ }
235+ pendingOps = [ { 'items.0.value.count' : new ParseOps . IncrementOp ( 6 ) } ] ;
236+ expect (
237+ ObjectStateMutations . estimateAttributes ( serverData , pendingOps , 'someClass' , 'someId' )
238+ ) . toEqual ( {
239+ _id : 'someId' ,
240+ items : [ { value : { count : 60 } , count : 5 } , { value : 'b' , count : 1 } ] ,
241+ className : 'bug' ,
242+ number : 2
243+ } ) ;
244+ } ) ;
245+
199246 it ( 'can commit changes from the server' , ( ) => {
200247 const serverData = { } ;
201248 const objectCache = { } ;
@@ -218,6 +265,34 @@ describe('ObjectStateMutations', () => {
218265 expect ( objectCache ) . toEqual ( { data : '{"count":5}' } ) ;
219266 } ) ;
220267
268+ it ( 'can commit dot notation array changes from the server' , ( ) => {
269+ const serverData = { items : [ { value : 'a' , count : 5 } , { value : 'b' , count : 1 } ] } ;
270+ ObjectStateMutations . commitServerChanges ( serverData , { } , {
271+ 'items.0.count' : 15 ,
272+ 'items.1.count' : 4 ,
273+ } ) ;
274+ expect ( serverData ) . toEqual ( { items : [ { value : 'a' , count : 15 } , { value : 'b' , count : 4 } ] } ) ;
275+ } ) ;
276+
277+ it ( 'can commit dot notation array changes from the server to empty serverData' , ( ) => {
278+ const serverData = { } ;
279+ ObjectStateMutations . commitServerChanges ( serverData , { } , {
280+ 'items.0.count' : 15 ,
281+ 'items.1.count' : 4 ,
282+ } ) ;
283+ expect ( serverData ) . toEqual ( { items : [ { count : 15 } , { count : 4 } ] } ) ;
284+ } ) ;
285+
286+ it ( 'can commit nested json array changes from the server to empty serverData' , ( ) => {
287+ const serverData = { } ;
288+ const objectCache = { } ;
289+ ObjectStateMutations . commitServerChanges ( serverData , objectCache , {
290+ items : { '0' : { count : 20 } , '1' : { count : 5 } }
291+ } ) ;
292+ expect ( serverData ) . toEqual ( { items : [ { count : 20 } , { count : 5 } ] } ) ;
293+ expect ( objectCache ) . toEqual ( { items : '[{"count":20},{"count":5}]' } ) ;
294+ } ) ;
295+
221296 it ( 'can generate a default state for implementations' , ( ) => {
222297 expect ( ObjectStateMutations . defaultState ( ) ) . toEqual ( {
223298 serverData : { } ,
0 commit comments