katowulf / mockfirebase

Firebase mock library for writing unit tests (experimental)
157 stars 40 forks source link

child_added triggered with partial data #101

Open nvdbleek opened 8 years ago

nvdbleek commented 8 years ago

The child_added event is triggered with not the complete data tree when you set data on a child that doesn't yet exists (all but the first key of the object is not present yet, and you gat a value event for the rest of the keys). My use case is to use transaction() to make sure that the child is only added when it doesn't yet exists, to make sure that only one client will add the node at that particular key.

If you run the following example:

var Firebase = require('mockfirebase');

var ref = new Firebase.MockFirebase('child_added://');
ref.child('foo').on('child_added', function (snaphot) {
  console.log(snaphot.key() + ' = ' + JSON.stringify(snaphot.val()))
});

ref.child('foo').child('a1').set({ foo: ['baz'], qux: 'norf'} );
ref.flush();

ref.child('foo').child('a2').transaction(function (current) {
   if (current === null) {
     return { foo: ['baz'], qux: 'norf'};
    }
  },
  function (error, committed, snaphot) {
    // console.log(snaphot.key() + ' = ' + JSON.stringify(snaphot.val()))
});
ref.flush();

You get the following output:

a1 = {"foo":{"0":"baz"}}
a2 = {"foo":{"0":"baz"}}

I would have expected to get:

a1 = {"foo":["baz"],"qux":"norf"}
a2 = {"foo":["baz"],"qux":"norf"}

Note the missing qux key in the output.

I tried to delay the sending of the child_added event, because it is send after that the first entry is added. But my attempt breaks a lot of other tests.

Is it possible to point me in the correct direction to make a fix for this issue.

My attempt is pushed as commit https://github.com/nvdbleek/mockfirebase/commit/9c1f099a7db0c7afb51dfb6a74ca7871e8fbb520

Thanks in advance