fake_cloud_firestore icon indicating copy to clipboard operation
fake_cloud_firestore copied to clipboard

`docChanges` reports incorrect changes.

Open ccrusius opened this issue 2 years ago • 0 comments

The following test fails and shows other incorrect behavior: docChanges is not reporting changes but the new contents of the collection. Nothing is ever deleted, and elements are added multiple times.

The correct sequence of events is "add 1" "add 2" "delete 2", but what is seen is "add 1" "add 1" "add 2" "add 1".

This behavior makes applications that use docChanges not testable with the fake.

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fake_cloud_firestore/fake_cloud_firestore.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  test('Deleting should trigger a delete doc change', () async {
    final Set<String> ids = {};
    final db = FakeFirebaseFirestore();
    final sub = db.collection('docs').snapshots().listen((querySnapshot) {
      for (final change in querySnapshot.docChanges) {
        debugPrint('${change.type} ${change.doc.id}');
        switch (change.type) {
          case DocumentChangeType.removed:
            ids.remove(change.doc.id);
            break;
          default:
            ids.add(change.doc.id);
            break;
        }
      }
    });
    addTearDown(sub.cancel);

    await db.collection('docs').doc('1').set(<String, dynamic>{});
    await pumpEventQueue();
    expect(ids, {'1'});

    await db.collection('docs').doc('2').set(<String, dynamic>{});
    await pumpEventQueue();
    expect(ids, {'1', '2'});

    await db.collection('docs').doc('2').delete();
    await pumpEventQueue();
    expect(ids, {'1'});
  });
}

ccrusius avatar Apr 16 '22 00:04 ccrusius