Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snapshot Listeners of converted documents don't pick up on changes to the same document if it's not also converted #254

Open
timcreatedit opened this issue Nov 29, 2022 · 1 comment

Comments

@timcreatedit
Copy link

timcreatedit commented Nov 29, 2022

Since the typedSnapshotStreamKey depends on the type of a MockDocumentReference, there can be two separate snapshot stream keys for the same document reference if you obtain one from a converted collection and the other one from a non-converted one. This leads to changes not being picked up if you listen to the same document from a converted reference but modify it from a non-converted one.

I wrote a test to make it more understandable. The second test times out because the snapshot listener never emits the change

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

class Model {
  const Model(this.name);

  factory Model.fromJson(Map<String, dynamic> data) => Model(data["name"]);

  final String name;

  Map<String, dynamic> toJson() => {"name": name};

  @override
  int get hashCode => Object.hash(runtimeType, name);

  @override
  operator ==(other) {
    return identical(this, other) || other is Model && other.name == name;
  }
}

void main() {
  late FakeFirebaseFirestore sut;
  late CollectionReference<Model> convertedCollection;
  late CollectionReference<Map<String, dynamic>> unconvertedCollection;

  const a = Model("a");
  const b = Model("b");

  setUp(() {
    sut = FakeFirebaseFirestore();
    convertedCollection = sut.collection("models").withConverter<Model>(
          fromFirestore: (snap, _) => Model.fromJson(snap.data()!),
          toFirestore: (value, _) => value.toJson(),
        );
    unconvertedCollection = sut.collection("models");
  });

  group('Incorrect snapshot listener behavior', () {
    test("this works", () async {
      await convertedCollection.doc("model").set(a);
      final stream = convertedCollection
          .doc("model")
          .snapshots()
          .map((event) => event.data());
      expectLater(stream, emitsInOrder([a, b]));
      convertedCollection.doc("model").set(b);
    });
    test("this fails", () async {
      await convertedCollection.doc("model").set(a);
      final stream = convertedCollection
          .doc("model")
          .snapshots()
          .map((event) => event.data());
      expectLater(stream, emitsInOrder([a, b]));
      unconvertedCollection.doc("model").set(b.toJson());
    });
  });
}
``
@LucaDillenburg
Copy link

Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants