firebase-mock icon indicating copy to clipboard operation
firebase-mock copied to clipboard

Realtime db Ref returns root value rather than the child node

Open jamescrowley opened this issue 5 years ago • 3 comments

Hi folks, I've got some code that works as expected with the 'real' firebase, but not the mock. Is this a known issue/limitation?

  const mockRealtimeDb = new firebaseMock.MockFirebase();
  mockRealtimeDb.autoFlush(true);
  const mockSdk = new firebaseMock.MockFirebaseSdk(
    () => mockRealtimeDb
  );
  const firebase = mockSdk.initializeApp({});
  await firebase
    .database()
    .ref()
    .update({ "devices/xyz": 123 });
  const savedValue = await firebase
    .database()
    .ref(`devices/xyz`)
    .once('value');
  expect(savedValue.val()).toStrictEqual(123);

however, savedValue.val() returns {"devices": {"xyz": 123}}

jamescrowley avatar Apr 16 '20 03:04 jamescrowley

It seems FireMock is "unfolding" the field path when using the / symbol. I did a quick search and couldn't find any documentation about it for RTDB, but in Firestore there are constraints in fields that won't let you do that.

I'm guessing FireMock is using the same code to process and unfold the fields from RTDB and Firestore type of calls.

lesmo avatar May 04 '20 15:05 lesmo

@lesmo the fields don't have a / in - it's just updating multiple paths, as a 'merge' essentially. the update syntax (at least in Firebase rather than firestore) allows you to do

.update({
   "devices/xyz": 123,
   "someOtherDoc/abc": 123,
});

and so on, but when doing a 'ref' of devices/xyz for the value, I'd expect just the value of the 'xyz' key, not the full document from root.

jamescrowley avatar May 05 '20 00:05 jamescrowley

Oh, you're right!

I've never used the path "shortcut", I'm always calling .collection().doc() (on Firestore, though) to get to my document so it's possible I never faced this issue. I'm guessing that could be a workaround?

lesmo avatar May 05 '20 14:05 lesmo