Skip to content

docChanges() does not include removed items #18

@wSedlacek

Description

@wSedlacek

First I would like to thank you Gustavo for all the hard work you have put into mocking Firebase.
I have been attempting to build out proper unit test when using Angular Fire and your mock of Firebase has been instrumental in building out proper mocks.

I am running into an issue with your mock of Firestore though. That is that it is only sending me the added docs but not removed or modified.

This is the section of code that I am getting stuck and and not getting past the short circuit.

if (this === this.query.lastSnapshot || !this.query.lastSnapshot) {
// Short circuit: this is the first snapshot, so all items were added.
return this.docs.map((doc, newIndex) => ({
type: "added",
oldIndex: -1,
newIndex,
doc,
}));
}

This is where the lastSnapshot seems like it should be set to prevent that short circuit

public async emitChange() {
if (!this.emitter.hasListeners(QUERY_SNAPSHOT_NEXT_EVENT)) {
return;
}
// TODO: this emits even if there wasn't an actual change with the current filters
const snapshot = await this.get();
this.lastSnapshot = snapshot;
this.emitter.emit(QUERY_SNAPSHOT_NEXT_EVENT, [snapshot]);
}
setNoInitialSnapshot(): this {
this.noInitialSnapshot = true;
return this;
}

Code I am using

      const subject = new Subject();
      this.app
        .firestore()
        .collection(path)
        .onSnapshot({
          next: (collection) => {
            subject.next(
              collection.docChanges().map(({ type, doc }) => ({ type, payload: { doc } }))
            );
          },
          error: (err) => subject.error(err),
          complete: () => subject.complete(),
        });

      subject.subscribe(console.log);

I have played around with it for a bit but can't seem to figure out the exact cause. My guess is either (a) lastSnapshot isn't being set OR (b) the current snapshot and lastSnapshot are always in sync, or at least at the time when docChanges() runs from within a onSnapshot()

Again thank you so much for your work. Let me know if you need any more information or have any work arounds.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions