fake_cloud_firestore icon indicating copy to clipboard operation
fake_cloud_firestore copied to clipboard

FieldValue.arrayUnion doesn't work properly with arrays of maps

Open juria opened this issue 4 years ago • 0 comments

Version: fake_cloud_firestore: ^1.1.3

Adding a map to an array using FieldValue.arrayUnion doesn't work properly. The following test should complete successfully, but it doesn't:

test('add map to array if it does not exist already', () async {
  final firestore = FakeFirebaseFirestore();
  await firestore.collection('users').doc('u1').set({'friends' : [
    {'name': 'Harry'},
    {'name': 'Ron'}
  ]});

  await firestore.collection('user').doc('u1').set({'friends' : FieldValue.arrayUnion([
    {'name': 'Harry'},
    {'name': 'Hermione'}
  ])}, SetOptions(merge: true));

  final rawUser = await firestore.collection('users').doc('u1').get();
  expect(rawUser.data(), {'friends': [
    {'name': 'Harry'},
    {'name': 'Ron'},
    {'name': 'Hermione'}
  ]});
});

This is the output:

Expected: {'friends': [{'name': 'Harry'}, {'name': 'Ron'}, {'name': 'Hermione'}]}
Actual: {'friends': [{'name': 'Harry'}, {'name': 'Ron'}, {'name': 'Harry'}, {'name': 'Hermione'}]}

As the output shows, the maps are added to the array friends, but the "union part" didn't work. {'name': 'Harry'} was added again, even if an equal map exists already. This would not be the case using the "real" Firestore.

With other data types like String it works like the "real" one, so no issues here:

test('add map to array if it does not exist already', () async {
  final firestore = FakeFirebaseFirestore();
  await firestore.collection('users').doc('u1').set({'friends' : ['Harry', 'Ron']});

  await firestore.collection('user').doc('u1').set(
    {'friends' : FieldValue.arrayUnion(['Harry', 'Hermione'])},
    SetOptions(merge: true),
  );

  final rawUser = await firestore.collection('users').doc('u1').get();
  expect(rawUser.data(), {'friends': ['Harry', 'Ron', 'Hermione']});
});

juria avatar Nov 08 '21 15:11 juria