@@ -663,23 +663,74 @@ void main() {
663663 });
664664
665665 test ('Arrays should not be cleared when calling clearUnsavedChanges() '
666- 'after fetching/querying without save' , () {
667- // arrange - Simulate a fetch/query response (no prior save operation)
668- dietPlansObject.fromJson ({
669- keyArray: [1 , 2 , 3 ],
670- "objectId" : "someId" ,
671- }); // assume this coming from the server via fetch/query
666+ 'after receiving response from fetch/query (issue #1038)' , () async {
667+ // arrange - First, save an object with an array to create a _ParseArray
668+ dietPlansObject.setAdd (keyArray, 1 );
669+ dietPlansObject.setAdd (keyArray, 2 );
672670
673- // Simulate what happens in _handleSingleResult for fetch/query
674- // (calls _notifyChildrenAboutSave without prior _notifyChildrenAboutSaving)
675- // This reproduces the bug where arrays were incorrectly cleared
676- dietPlansObject._notifyChildrenAboutSave ();
671+ when (
672+ client.post (any, options: anyNamed ("options" ), data: anyNamed ('data' )),
673+ ).thenAnswer (
674+ (_) async => ParseNetworkResponse (
675+ statusCode: 200 ,
676+ data: jsonEncode ({
677+ "objectId" : "Mn1iJTkWTE" ,
678+ "createdAt" : "2023-03-05T00:25:31.466Z" ,
679+ }),
680+ ),
681+ );
677682
678- // act - this should NOT clear the arrays
679- dietPlansObject.clearUnsavedChanges ();
683+ await dietPlansObject
684+ .save (); // This creates the _ParseArray and calls onSaving/onSaved
685+
686+ // Now set up a mock fetch response that returns updated array data
687+ final getPath = Uri .parse (
688+ '$serverUrl $keyEndPointClasses ${dietPlansObject .parseClassName }/${dietPlansObject .objectId }' ,
689+ ).toString ();
690+
691+ const resultsFromServer = {
692+ "results" : [
693+ {
694+ "objectId" : "Mn1iJTkWTE" ,
695+ keyArray: [
696+ 1 ,
697+ 2 ,
698+ 3 ,
699+ ], // Server now has an updated array with one more item
700+ "createdAt" : "2023-03-05T00:25:31.466Z" ,
701+ "updatedAt" : "2023-03-05T00:25:31.466Z" ,
702+ },
703+ ],
704+ };
680705
681- // assert - arrays should still have their values
682- final listValue = dietPlansObject.get (keyArray);
706+ when (
707+ client.get (
708+ getPath,
709+ options: anyNamed ("options" ),
710+ onReceiveProgress: anyNamed ("onReceiveProgress" ),
711+ ),
712+ ).thenAnswer (
713+ (_) async => ParseNetworkResponse (
714+ statusCode: 200 ,
715+ data: jsonEncode (resultsFromServer),
716+ ),
717+ );
718+
719+ // act - Fetch the object from server to get the updated array
720+ // This simulates the scenario from issue #1038 where after fetching/querying
721+ // an object (e.g., via getUpdatedUser() or fetch()), calling clearUnsavedChanges()
722+ // would incorrectly clear the arrays.
723+ ParseObject fetchedObject = await dietPlansObject.fetch ();
724+
725+ // Verify array is populated correctly from the fetch response
726+ expect (fetchedObject.get (keyArray), orderedEquals ([1 , 2 , 3 ]));
727+
728+ // Now clear unsaved changes - this should NOT clear the arrays
729+ // Before the fix (PR #1039), this would set arrays to empty
730+ fetchedObject.clearUnsavedChanges ();
731+
732+ // assert - Arrays should still have their values from the server
733+ final listValue = fetchedObject.get (keyArray);
683734 expect (listValue, orderedEquals ([1 , 2 , 3 ]));
684735 });
685736
0 commit comments