Skip to content

Commit 45e76e9

Browse files
committed
test
1 parent 4b88a2f commit 45e76e9

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

packages/dart/test/src/objects/parse_object/parse_object_array_test.dart

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -664,26 +664,29 @@ void main() {
664664

665665
test('Arrays should not be cleared when calling clearUnsavedChanges() '
666666
'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);
670-
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-
);
667+
// Steps to reproduce from issue #1038:
668+
// 1. Query/fetch a user object with arrays from server
669+
// 2. Call clearUnsavedChanges() - arrays should NOT become empty
670+
// 3. Call getUpdatedUser()/fetch() again
671+
// 4. Check that savedArray and estimatedArray both have values (not empty)
672+
673+
// Step 1: Simulate getting an object from server (e.g., via query or getUpdatedUser)
674+
// This is like getting a user from cache or initial fetch
675+
dietPlansObject.fromJson({
676+
keyArray: [1, 2, 3],
677+
"objectId": "Mn1iJTkWTE",
678+
"createdAt": "2023-03-05T00:25:31.466Z",
679+
});
680+
681+
// Step 2: Call clearUnsavedChanges() - this should NOT clear the arrays
682+
// Before the fix, if savedArray was empty, this would make arrays empty
683+
// because onClearUnsaved() does: estimatedArray = savedArray
684+
dietPlansObject.clearUnsavedChanges();
682685

683-
await dietPlansObject
684-
.save(); // This creates the _ParseArray and calls onSaving/onSaved
686+
// Verify arrays are NOT empty after clearUnsavedChanges
687+
expect(dietPlansObject.get(keyArray), orderedEquals([1, 2, 3]));
685688

686-
// Now set up a mock fetch response that returns updated array data
689+
// Step 3: Set up mock for getUpdatedUser()/fetch()
687690
final getPath = Uri.parse(
688691
'$serverUrl$keyEndPointClasses${dietPlansObject.parseClassName}/${dietPlansObject.objectId}',
689692
).toString();
@@ -692,11 +695,7 @@ void main() {
692695
"results": [
693696
{
694697
"objectId": "Mn1iJTkWTE",
695-
keyArray: [
696-
1,
697-
2,
698-
3,
699-
], // Server now has an updated array with one more item
698+
keyArray: [1, 2, 3],
700699
"createdAt": "2023-03-05T00:25:31.466Z",
701700
"updatedAt": "2023-03-05T00:25:31.466Z",
702701
},
@@ -716,22 +715,20 @@ void main() {
716715
),
717716
);
718717

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]));
718+
// Fetch the object (simulating getUpdatedUser)
719+
// NOTE: fetch() might update dietPlansObject in place OR return a new object
720+
await dietPlansObject.fetch();
727721

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();
722+
// Step 4: Verify the ORIGINAL object still has array values
723+
// The issue reported that after fetch, arrays would be cleared
724+
final arrayAfterFetch = dietPlansObject.get(keyArray);
725+
expect(arrayAfterFetch, orderedEquals([1, 2, 3]));
731726

732-
// assert - Arrays should still have their values from the server
733-
final listValue = fetchedObject.get(keyArray);
734-
expect(listValue, orderedEquals([1, 2, 3]));
727+
// Additional check: call clearUnsavedChanges() on the original object
728+
// and verify arrays persist. This is the exact scenario from issue #1038.
729+
dietPlansObject.clearUnsavedChanges();
730+
final arrayAfterSecondClear = dietPlansObject.get(keyArray);
731+
expect(arrayAfterSecondClear, orderedEquals([1, 2, 3]));
735732
});
736733

737734
test('The list value and the value for api request should be identical '

0 commit comments

Comments
 (0)