dmurvihill / firebase-mock

Firebase mock library for writing unit tests
55 stars 20 forks source link

Realtime db Ref returns root value rather than the child node #61

Open jamescrowley opened 4 years ago

jamescrowley commented 4 years ago

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}}

lesmo commented 4 years ago

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.

jamescrowley commented 4 years ago

@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.

lesmo commented 4 years ago

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?